JWRuixi

P5063 [Ynoi2014] 置身天上之森

Oct 31st, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | Source Code | 0 0
  1. #ifdef LOCAL
  2. #include "stdafx.h"
  3. #else
  4. #include <bits/stdc++.h>
  5. #define IL inline
  6. #define LL long long
  7. #define eb emplace_back
  8. #define sz(v) ((int) (v).size())
  9. #define me(f, x) memset(f, x, sizeof(f))
  10. #define mc(f, g) memcpy(f, g, sizeof(g))
  11. #define L(i, j, k) for (int i = (j); i <= (k); ++i)
  12. #define R(i, j, k) for (int i = (j); i >= (k); --i)
  13. #define FIO(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout)
  14. using namespace std;
  15.  
  16. using vi = vector<int>;
  17. #endif
  18.  
  19. constexpr int N = 1e5 + 9;
  20. constexpr int sqn = 350;
  21.  
  22. struct _6th_island {
  23.   int n, m, B, cnt;
  24.  
  25.   struct Rakishu {
  26.     int l, r;
  27.     LL v;
  28.     bool operator < (const Rakishu &rhs) const {
  29.       return v < rhs.v;
  30.     }
  31.   } a[N];
  32.  
  33.   struct island {
  34.     int L, R, s, t;
  35.     LL x;
  36.     bool operator < (const island &rhs) const {
  37.       return t < rhs.t;
  38.     }
  39.   } b[sqn];
  40.  
  41.   void init () {
  42.     B = sqrt(n);
  43.     cnt = (n + B - 1) / B;
  44.     L (i, 1, cnt) {
  45.       b[i].L = b[i - 1].R + 1;
  46.       b[i].R = min(n, i * B);
  47.       b[i].s = a[b[i].L].l;
  48.       b[i].t = a[b[i].R].r;
  49.     }
  50.   }
  51.  
  52.   void bf_mdy (int p, int l, int r, int x) {
  53.     L (i, b[p].L, b[p].R) {
  54.       if (a[i].r < l || a[i].l > r) {
  55.         continue;
  56.       }
  57.       a[i].v += (LL)x * (min(r, a[i].r) - max(l, a[i].l) + 1);
  58.     }
  59.     stable_sort(a + b[p].L, a + b[p].R + 1);
  60.   }
  61.  
  62.   int bf_qry (int p, int l, int r, LL x) {
  63.     x -= b[p].x;
  64.     int ret = 0;
  65.     L (i, b[p].L, b[p].R) {
  66.       ret += (l <= a[i].l && a[i].r <= r && a[i].v <= x);
  67.     }
  68.     return ret;
  69.   }
  70.  
  71.   int qry_all (int p, LL x) {
  72.     x -= b[p].x;
  73.     return x >= a[b[p].R].v ? b[p].R - b[p].L + 1 : (x < a[b[p].L].v ? 0 : upper_bound(a + b[p].L, a + b[p].R + 1, (Rakishu){0, 0, x}) - a - b[p].L);
  74.   }
  75.  
  76.   void mdy (int l, int r, int x) {
  77.     int p = lower_bound(b + 1, b + cnt + 1, (island){0, 0, 0, l, 0}) - b;
  78.     if (b[p].s <= l && r <= b[p].t) {
  79.       if (b[p].s == l && b[p].t == r) {
  80.         b[p].x += x * (LL)m;
  81.         return;
  82.       }
  83.       bf_mdy(p, l, r, x);
  84.       return;
  85.     }
  86.     if (b[p].s < l) {
  87.       bf_mdy(p, l, r, x);
  88.       p += 1;
  89.     }
  90.     while (p <= cnt && b[p].t <= r) {
  91.       b[p].x += x * (LL)m;
  92.       ++p;
  93.     }
  94.     if (p <= cnt && b[p].s <= r) {
  95.       bf_mdy(p, l, r, x);
  96.     }
  97.   }
  98.  
  99.   int qry (int l, int r, LL x) {
  100.     int p = lower_bound(b + 1, b + cnt + 1, (island){0, 0, 0, l, 0}) - b;
  101.     if (p > cnt) {
  102.       return 0;
  103.     }
  104.     if (b[p].s <= l && r <= b[p].t) {
  105.       if (b[p].s == l && b[p].t == r) {
  106.         return qry_all(p, x);
  107.       }
  108.       return bf_qry(p, l, r, x);
  109.     }
  110.     int ret = 0;
  111.     if (b[p].s < l) {
  112.       ret += bf_qry(p, l, r, x);
  113.       p += 1;
  114.     }
  115.     while (p <= cnt && b[p].t <= r) {
  116.       ret += qry_all(p, x);
  117.       ++p;
  118.     }
  119.     if (p <= cnt && b[p].s <= r) {
  120.       ret += bf_qry(p, l, r, x);
  121.     }
  122.     return ret;
  123.   }
  124. } kk[36];
  125.  
  126. int n, m, mp[N], tot;
  127.  
  128. void bld (int l, int r) {
  129.   int x = r - l + 1;
  130.   if (!mp[x]) {
  131.     mp[x] = ++tot;
  132.     kk[tot].m = x;
  133.   }
  134.   int id = mp[x];
  135.   int y = ++kk[id].n;
  136.   kk[id].a[y] = (_6th_island::Rakishu){l, r, 0};
  137.   if (l == r) {
  138.     return;
  139.   }
  140.   int mid = (l + r) / 2;
  141.   bld(l, mid);
  142.   bld(mid + 1, r);
  143. }
  144.  
  145. int main () {
  146.   ios::sync_with_stdio(0), cin.tie(0);
  147.   cin >> n >> m;
  148.   bld(1, n);
  149.   L (i, 1, tot) {
  150.     kk[i].init();
  151.   }
  152.   while (m--) {
  153.     int o, l, r, x;
  154.     cin >> o >> l >> r >> x;
  155.     if (o == 1) {
  156.       L (i, 1, tot) {
  157.         kk[i].mdy(l, r, x);
  158.       }
  159.     } else {
  160.       int s = 0;
  161.       L (i, 1, tot) {
  162.         if (r - l + 1 >= kk[i].m) {
  163.           s += kk[i].qry(l, r, x);
  164.         }
  165.       }
  166.       cout << s << '\n';
  167.     }
  168.   }
  169. }
  170. // I love WHQ!
Advertisement
Add Comment
Please, Sign In to add comment