Advertisement
Kevin_Zhang

Untitled

Sep 22nd, 2020
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define pb emplace_back
  3. using namespace std;
  4. using ll = long long;
  5. #ifdef KEV
  6. #define DE(i, e) cerr << #i << ' ' << i << e
  7. void debug(auto L, auto R) { while (L < R) cerr << *L << " \n"[L+1==R], ++L; }
  8. #else
  9. #define DE(...) 0
  10. void debug(...) {}
  11. #endif
  12. const int maxn = 200010, maxm = 1000010;
  13. const ll inf = 1ll<<55;
  14. ll dp[maxn];
  15. int cu[maxn], cd[maxn], T;
  16. struct bit {
  17.     int val[maxm];
  18.     void reset() {
  19.         fill(val, val+maxm, 0);
  20.     }
  21.     bool rev = false;
  22.     void REV(){ rev ^= true; }
  23.     void modify(int i, int v) {
  24.         ++i;
  25.         if (rev) i = maxm - i;
  26.         for (;i < maxm;i += i&-i)
  27.             val[i] = max(val[i], v);
  28.     }
  29.     int query(int i) {
  30.         ++i;
  31.         if (rev) i = maxm - i;
  32.         int res = 0;
  33.         for (;i;i ^= i&-i)
  34.             res = max(res, val[i]);
  35.         return res;
  36.     }
  37. }tree;
  38. int n;
  39. pair<int,int> loc[maxn];
  40. vector<int> dep[maxn];
  41. ll area(int a, int b) {
  42.     return (ll)(loc[a].first-loc[b].first) * (loc[a].second-loc[b].second);
  43. }
  44. bool valid(int s, int t) {
  45.     return loc[s].first < loc[t].first && loc[s].second < loc[t].second;
  46. }
  47. ll cal(int s, int t) {
  48.     return valid(s, t) ? dp[s] + area(s, t) : inf;
  49. }
  50. void DC(const vector<int>& from, const vector<int>& to, int l, int r, int sl, int sr) {
  51.     sl = max(sl, 0), sr = min(sr, (int)from.size()-1);
  52.     auto getpos = [&](int x) {
  53.         dp[x] = inf;
  54.         int y = -1;
  55.         for (int i = sl;i <= sr;++i) {
  56.             ll nw = cal(from[i], x);
  57.             if (nw < dp[x])
  58.                dp[x] = nw, y = i;
  59.         }
  60. //      auto [X, Y] = loc[x];
  61. //      DE(X, ' '), DE(Y, ' '), DE(dp[x], '\n');
  62.         return y;  
  63.     };
  64.     if (l > r) return;
  65.     const int BUF = 1000;
  66.     if (r-l+1 <= 500) {
  67.         for (int i = l;i <= r;++i)
  68.             getpos(to[i]);
  69.         return;
  70.     }
  71.  
  72.     if (l == r) {
  73.         getpos(to[l]);
  74.         return;
  75.     }
  76.     int m = l + r >> 1;
  77.     int sm = getpos(to[m]);
  78.     int A = sl, B = min(sr, sm+BUF), C = max(sl, sm-BUF), D = sr;
  79.     DC(from, to, l, m-1, C, D);
  80.     DC(from, to, m+1, r, A, B);
  81. }
  82.  
  83. void ss(vector<int> id) {
  84.     for (int u : id)
  85.         dep[ cu[u] ].pb(u);
  86.     int s = *max_element(cu, cu+n+1);
  87.     debug(cu, cu+n+1);
  88.     for (int i = 2;i <= s;++i) {
  89.         DC(dep[i-1], dep[i], 0, dep[i].size()-1,
  90.                 0, dep[i-1].size()-1);
  91.     }
  92.     cout << dp[n] << '\n';
  93. //  ll res = (ll)T * (ll)T;
  94. //  for  (int u : dep[s])
  95. //      res = min(res, dp[u]);
  96. //  cout << res << '\n';
  97. }
  98. void solve() {
  99.     tree.reset();
  100.     for (int i = 0, x, y;i <= n;++i) {
  101.         tie(x, y) = loc[i];
  102.         DE(x, ' '), DE(y, '\n');
  103.         cu[i] = tree.query(y) + 1;
  104.         tree.modify(y, cu[i]);
  105.     }
  106.     tree.reset();
  107.     tree.REV();
  108.     for (int i = n, x, y;i >= 0;--i) {
  109.         tie(x, y) = loc[i];
  110.         cd[i] = tree.query(y) + 1;
  111.         tree.modify(y, cd[i]);
  112.     }
  113.     int S = *max_element(cu, cu+n+1);
  114.     vector<int> uf;
  115.     DE(S, '\n');
  116.     for (int i = 0;i <= n;++i) {
  117.         if (cu[i] + cd[i] == S+1) {
  118.         //cerr << cu[i] << ' ' << cd[i] << '\n';
  119.             uf.pb(i);
  120.         }
  121.     }
  122.     ss(uf);
  123. }
  124. /*
  125. ID: ckevin31
  126. LANG: C++14
  127. TASK:
  128. */
  129. #ifndef KEV
  130. inline void IO(string name) {
  131.     freopen((name + ".in").c_str(), "r", stdin);
  132.     freopen((name + ".out").c_str(), "w", stdout);
  133. }
  134. #else
  135. inline void IO(string name){}
  136. #endif
  137. signed main(){
  138.     IO("mowing");
  139.     ios_base::sync_with_stdio(0), cin.tie(0);
  140.     cin >> n >> T;
  141.     for (int i = 1;i <= n;++i)
  142.         cin >> loc[i].first >> loc[i].second;
  143.     ++n;
  144.     loc[n] = {T, T};
  145.  
  146.     sort(loc, loc+n);
  147.     solve();
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement