#include <iostream> #include <algorithm> using namespace std; struct Range { int from; int to; }; bool compare (Range x, Range y){ if (x.from != y.from){ return x.from < y.from; }else{ return x.to < y.to; } } Range r[101]; int main () { int L, M; while (cin >> L >> M){ for (int i = 0; i < M; i++){ cin >> r[i].from >> r[i].to; } sort (r, r + M, compare); int tree = L + 1; int begin = r[0].from; int end = r[0].to; for (int i = 0; i < M; i++){ if (i == M - 1){ tree -= end - begin + 1; }else{ if (r[i + 1].from <= end){ if (r[i + 1].to > end){ end = r[i + 1].to; } }else{ tree -= end - begin + 1; begin = r[i + 1].from; end = r[i + 1].to; } } } cout << tree << endl; } return 0; }
自己写的一直出错
参照的答案