求大老帮调vector的码

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

qwer111t @ 2024-10-30 16:10:58

#include<bits/stdc++.h>
using namespace std;
vector<int> tree;
int main(){
    int l,m,a,b,s = 0;
    cin >> l >> m;
    for(int i = 1;i <= m;i++){
        cin >> a >> b;
        for(int j = a;j <= b;j++){
            tree[j] = true;
        }
    }
    for(int i = 0;i <= l;i++){
        if(tree[i] == false){
            s++;
        }
    }
    cout << s;
    return 0;
}

by konyakest @ 2024-10-30 16:19:42

@qwer111t vector 需要先 resize 才能下标访问


by konyakest @ 2024-10-30 16:20:15

@qwer111t 在输入后加上 tree.resize(m+5) 试试


by qwer111t @ 2024-10-30 16:24:03

@konyakest ,能给个伪代码吗,谢谢

(此蒟蒻是vector的初学者,目前还不会resize。。。)


by qwer111t @ 2024-10-30 16:37:47

@konyakest ,好的,我试试,谢谢大佬解答


|