Advertisement
nickel2halide

photo

Apr 10th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. #include <vector>
  5. #include <set>
  6.  
  7. #include <utility>
  8. #include <cstring>
  9.  
  10. using namespace std;
  11.  
  12. #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
  13.  
  14. const int MAX_N = int(2e5) + 20, MAX_M = int(1e5) + 20;
  15.  
  16. int N, M;
  17. pair<int, int> I[MAX_M];
  18.  
  19. struct CompareInterval {
  20.     bool operator()(int i, int j) {
  21.         return I[i] < I[j];
  22.     }
  23. };
  24.  
  25. int main() {
  26.     freopen("photo.in", "r", stdin);
  27.     freopen("photo.out", "w", stdout);
  28.  
  29.     cin >> N >> M;
  30.     REP(i, M) {
  31.         int a, b; cin >> a >> b;
  32.         I[i] = make_pair(a - 1, b);
  33.     }
  34.  
  35.     static vector<int> ev[MAX_N];
  36.     REP(i, M) {
  37.         ev[I[i].first].push_back(i + 1);
  38.         ev[I[i].second].push_back(-i - 1);
  39.     }
  40.  
  41.     static int dp[MAX_N];
  42.     memset(dp, -1, sizeof(dp));
  43.  
  44.     dp[0] = 0;
  45.     set<int, CompareInterval> S;
  46.     int start = -1;
  47.     REP(i, N) {
  48.         REP(j, ev[i].size())
  49.             if (ev[i][j] > 0) S.insert(ev[i][j] - 1);
  50.             else {
  51.                 int c = -ev[i][j] - 1;
  52.                 S.erase(c);
  53.                 start = max(start, I[c].first);
  54.             }
  55.         if (S.empty() || I[*S.rbegin()].first != i)
  56.             dp[i + 1] = dp[i];
  57.         int l = S.empty() ? i : I[*S.begin()].first;
  58.         if (l > start && dp[l] != -1)
  59.             dp[i + 1] = max(dp[i + 1], dp[l] + 1);
  60.     }
  61.  
  62.     cout << dp[N] << "\n";
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement