Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <set>
- #include <utility>
- #include <cstring>
- using namespace std;
- #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
- const int MAX_N = int(2e5) + 20, MAX_M = int(1e5) + 20;
- int N, M;
- pair<int, int> I[MAX_M];
- struct CompareInterval {
- bool operator()(int i, int j) {
- return I[i] < I[j];
- }
- };
- int main() {
- freopen("photo.in", "r", stdin);
- freopen("photo.out", "w", stdout);
- cin >> N >> M;
- REP(i, M) {
- int a, b; cin >> a >> b;
- I[i] = make_pair(a - 1, b);
- }
- static vector<int> ev[MAX_N];
- REP(i, M) {
- ev[I[i].first].push_back(i + 1);
- ev[I[i].second].push_back(-i - 1);
- }
- static int dp[MAX_N];
- memset(dp, -1, sizeof(dp));
- dp[0] = 0;
- set<int, CompareInterval> S;
- int start = -1;
- REP(i, N) {
- REP(j, ev[i].size())
- if (ev[i][j] > 0) S.insert(ev[i][j] - 1);
- else {
- int c = -ev[i][j] - 1;
- S.erase(c);
- start = max(start, I[c].first);
- }
- if (S.empty() || I[*S.rbegin()].first != i)
- dp[i + 1] = dp[i];
- int l = S.empty() ? i : I[*S.begin()].first;
- if (l > start && dp[l] != -1)
- dp[i + 1] = max(dp[i + 1], dp[l] + 1);
- }
- cout << dp[N] << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement