30分救命,想刷水题却被水题刷了

P1047 [NOIP2005 普及组] 校门外的树

Wa年糕Wa @ 2022-08-20 20:20:03

#include<iostream>
using namespace std;
int main(){
    int n,l;
    int a,b;
    int num = 0;
    cin >> n >> l;
    int arr[n];
    for(int i = 0;i <= n;i++){
        arr[i] = 1;
    }
    for(int i = 1;i <= l;i++){
        cin >> a >> b;
        for(int j = a;j <= b;j++){
            if(arr[j] == 1){
                num++;
                arr[j]++;
            }
        }
    }
    cout << n-num << endl;
    return 0;
}

by Hughpig @ 2022-08-20 20:20:52

正解线段树(雾


by JustinXiaoJunyang @ 2022-08-20 20:22:18

@Wa年糕Wa 求关注~

#include <iostream>
using namespace std;

int main()
{
    int l, m;
    cin >> l >> m;
    int start, stop;
    int left = 0;
    bool a[l];
    for (int i = 0; i <= l; i++)
    {
        a[i] = true;
    }
    for (int i = 1; i <= m; i++)
    {
        cin >> start >> stop;
        for (int j = start; j <= stop; j++)
        {
            a[j] = false;
        }
    }
    for (int i = 0; i <= l; i++)
    {
        if (a[i] == true)
        {
            left++;
        }
    }
    cout << left << endl;
    return 0;
}

by bamboo12345 @ 2022-08-20 20:27:16

@Wa年糕Wa 首先你要把数组开大一点,其次把答案加一,因为0~n一共是n+1个数


by Wa年糕Wa @ 2022-08-20 20:31:41

感谢各位大佬,已经会了


|