Luogu _P1276 The tree outside the school gate (enhanced version) (Shang Xian)

Title Portal

First of all, let me mention a few easy-to-ignore pits that lead to errors
1.

How many saplings are left outside the school gate?

Did you directly output the number of trees at the school gate without seeing it from the beginning (in fact, the title says that there were trees at the beginning, and the seedlings were planted later) (then you will get 80 or Good score of 20 points), maybe I am the only one who misunderstood

2.

How many saplings are planted and planted by tree planters?

There are fewer people who may be wrong. It means that every time a tree-cutter cuts a sapling, he cuts ans ++, not ans ++ only once in a tree pit.

3.

The road outside the school gate originally numbered 0 to L, and there is a tree at each numbered position.

I ’m not the only one who read the wrong number (trying to comfort myself)

As mentioned in the end, the questions must be reviewed carefully, otherwise the completely correct program will also explode (I will not tell you that I provided Luo Gu with a page of submission records in order to find out these pits)

Then form a good habit
to look at the size of the data before doing the question

The data for this question is L 1 < = L < = 10000 with N 1 < = N < = 100 L(1 <= L <= 10000)和 N(1 <= N <= 100)
Hey, the simulation of O (n ^ 2) seems to be able to pass *∗?

Come on, write a simulation (don't write a line segment tree if you can write a simulation, the code size of the line segment tree can't be complimented)

#include<bits/stdc++.h>
#define ll long long//随手long long好习惯

using namespace std;

const ll FFF=10000+5;

ll l,n;
bool opt;//判断是砍树还是种树
ll a,b;
ll flag[FFF];//flag=1时是树(就是一开始就存在的),flag=2时是树苗(就是后来种下的)
			 //flag=0时是空气(空坑)
ll ans_1,ans_2;//ans_1记录最终校门外留下的树苗棵数,ans_2记录植树者种上又被砍掉的树苗棵数

int main()
{
	freopen("cpp.in", "r", stdin);
	freopen("cpp.out", "w", stdout);
    ios::sync_with_stdio(false);//关闭流同步,让cin、cout变得和scanf、printf一样快
    cin>>l>>n;
    for(int i=0;i<=l;++i)//记住编号从0开始,到l结束
    {
        flag[i]=1;//初始化,把树都种上
    }
    for(int i=0;i<n;++i)
    {
        cin>>opt>>a>>b;
        if(opt==0)//如果是砍树
        {
            for(int j=a;j<=b;++j)//那就砍呗,从a到b
            {
                if(flag[j]==2)//如果砍的是树苗
                {
                    ans_2+=1;//ans_2++
                }
                flag[j]=0;//让flag=0,即记为空坑
            }
        }
        else//如果是种树
        {
            for(int j=a;j<=b;++j)
            {
                if(flag[j]==0)//如果遇到一个空坑
                {
                    flag[j]=2;//就把树苗种上,把flag变为2(注意,是树苗了,不能把flag变成1)
                }
            }
        }
    }
    for(int i=0;i<=l;++i)//最后处理
    {
        if(flag[i]==2)//如果校门口有树苗的话
        {
            ans_1+=1;//ans_1++
        }
    }
    cout<<ans_1<<endl<<ans_2;//输出,完结撒花
    return 0;
}

My AC code

#include <iostream>
#include <cstdio>
#define SIZE 100 + 10
#define SIZE2 (int)1e4 + 10
using namespace std;
int tree[SIZE2];

void print(const int *, const int &);

int main() {
	freopen("cpp.in", "r", stdin);
	freopen("cpp.out", "w", stdout);
	int n, L, ans2 = 0, ans = 0;
	scanf("%d%d", &L, &n);
	for (int i = 1; i <= n; ++i) {
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		if (x == 0) {
			for (int i = y; i <= z; ++i) {
				if (tree[i] == 2) {
					++ans2;
				}
				tree[i] = 1;
			}
		} else {
			for (int i = y; i <= z; ++i) {
				if (tree[i] == 1) {
					tree[i] = 2;
				}
			}
		}
	}
	for (int i = 0; i <= L; ++i) {
		if (tree[i] == 2) {
			++ans;
		}
	}
	printf("%d\n%d\n", ans, ans2);
	return 0;
}
Posted 33 original articles · liked 0 · visits 167

Guess you like

Origin blog.csdn.net/weixin_42790071/article/details/105565515