题解 | #剩下的树#Vector/Iterator用法
剩下的树
https://www.nowcoder.com/practice/f5787c69f5cf41499ba4706bc93700a2
#include <cstdio> #include<iostream> #include<vector> using namespace std; int main() { int L, M; scanf("%d%d", &L, &M); vector<int> vec(L+1); //0~L vector vec(0)有树 vec(1)无树 for (int i = 0; i < M; ++i) { int left, right; scanf("%d%d", &left, &right); for (int j = left; j <= right; ++j) { vec[j] = 1; } } int treenum; vector<int>::iterator it; for (it = vec.begin(); it != vec.end(); ++it) { if (*it == 0) treenum++; } printf("%d", treenum); return 0; }