Advertisement
pb_jiang

CSES1736 WA&TLE

Mar 31st, 2023
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.60 KB | None | 0 0
  1. // Problem: Polynomial Queries
  2. // Contest: CSES - CSES Problem Set
  3. // URL: https://cses.fi/problemset/task/1736/
  4. // Memory Limit: 512 MB
  5. // Time Limit: 1000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  13. template <typename... Args> void logger(string vars, Args &&... values)
  14. {
  15.     cerr << vars << " = ";
  16.     string delim = "";
  17.     (..., (cerr << delim << values, delim = ", "));
  18.     cerr << endl;
  19. }
  20.  
  21. namespace atcoder
  22. {
  23.  
  24. template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()>
  25. struct lazy_segtree {
  26.     int ceil_pow2(int n)
  27.     {
  28.         int x = 0;
  29.         while ((1U << x) < (unsigned int) (n))
  30.             x++;
  31.         return x;
  32.     }
  33.  
  34.   public:
  35.     lazy_segtree() : lazy_segtree(0) {}
  36.     explicit lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
  37.     explicit lazy_segtree(const std::vector<S> &v) : _n(int(v.size()))
  38.     {
  39.         log = ceil_pow2(_n);
  40.         size = 1 << log;
  41.         d = std::vector<S>(2 * size, e());
  42.         lz = std::vector<F>(size, id());
  43.         for (int i = 0; i < _n; i++)
  44.             d[size + i] = v[i];
  45.         for (int i = size - 1; i >= 1; i--) {
  46.             update(i);
  47.         }
  48.     }
  49.  
  50.     void set(int p, S x)
  51.     {
  52.         assert(0 <= p && p < _n);
  53.         p += size;
  54.         for (int i = log; i >= 1; i--)
  55.             push(p >> i);
  56.         d[p] = x;
  57.         for (int i = 1; i <= log; i++)
  58.             update(p >> i);
  59.     }
  60.  
  61.     S get(int p)
  62.     {
  63.         assert(0 <= p && p < _n);
  64.         p += size;
  65.         for (int i = log; i >= 1; i--)
  66.             push(p >> i);
  67.         return d[p];
  68.     }
  69.  
  70.     S prod(int l, int r)
  71.     {
  72.         assert(0 <= l && l <= r && r <= _n);
  73.         if (l == r)
  74.             return e();
  75.  
  76.         l += size;
  77.         r += size;
  78.  
  79.         for (int i = log; i >= 1; i--) {
  80.             if (((l >> i) << i) != l)
  81.                 push(l >> i);
  82.             if (((r >> i) << i) != r)
  83.                 push((r - 1) >> i);
  84.         }
  85.  
  86.         S sml = e(), smr = e();
  87.         while (l < r) {
  88.             if (l & 1)
  89.                 sml = op(sml, d[l++]);
  90.             if (r & 1)
  91.                 smr = op(d[--r], smr);
  92.             l >>= 1;
  93.             r >>= 1;
  94.         }
  95.  
  96.         return op(sml, smr);
  97.     }
  98.  
  99.     S all_prod() { return d[1]; }
  100.  
  101.     void apply(int p, F f)
  102.     {
  103.         assert(0 <= p && p < _n);
  104.         p += size;
  105.         for (int i = log; i >= 1; i--)
  106.             push(p >> i);
  107.         d[p] = mapping(f, d[p]);
  108.         for (int i = 1; i <= log; i++)
  109.             update(p >> i);
  110.     }
  111.     void apply(int l, int r, F f)
  112.     {
  113.         assert(0 <= l && l <= r && r <= _n);
  114.         if (l == r)
  115.             return;
  116.  
  117.         l += size;
  118.         r += size;
  119.  
  120.         for (int i = log; i >= 1; i--) {
  121.             if (((l >> i) << i) != l)
  122.                 push(l >> i);
  123.             if (((r >> i) << i) != r)
  124.                 push((r - 1) >> i);
  125.         }
  126.  
  127.         {
  128.             int l2 = l, r2 = r;
  129.             while (l < r) {
  130.                 if (l & 1)
  131.                     all_apply(l++, f);
  132.                 if (r & 1)
  133.                     all_apply(--r, f);
  134.                 l >>= 1;
  135.                 r >>= 1;
  136.             }
  137.             l = l2;
  138.             r = r2;
  139.         }
  140.  
  141.         for (int i = 1; i <= log; i++) {
  142.             if (((l >> i) << i) != l)
  143.                 update(l >> i);
  144.             if (((r >> i) << i) != r)
  145.                 update((r - 1) >> i);
  146.         }
  147.     }
  148.  
  149.     template <bool (*g)(S)> int max_right(int l)
  150.     {
  151.         return max_right(l, [](S x) { return g(x); });
  152.     }
  153.     template <class G> int max_right(int l, G g)
  154.     {
  155.         assert(0 <= l && l <= _n);
  156.         assert(g(e()));
  157.         if (l == _n)
  158.             return _n;
  159.         l += size;
  160.         for (int i = log; i >= 1; i--)
  161.             push(l >> i);
  162.         S sm = e();
  163.         do {
  164.             while (l % 2 == 0)
  165.                 l >>= 1;
  166.             if (!g(op(sm, d[l]))) {
  167.                 while (l < size) {
  168.                     push(l);
  169.                     l = (2 * l);
  170.                     if (g(op(sm, d[l]))) {
  171.                         sm = op(sm, d[l]);
  172.                         l++;
  173.                     }
  174.                 }
  175.                 return l - size;
  176.             }
  177.             sm = op(sm, d[l]);
  178.             l++;
  179.         } while ((l & -l) != l);
  180.         return _n;
  181.     }
  182.  
  183.     template <bool (*g)(S)> int min_left(int r)
  184.     {
  185.         return min_left(r, [](S x) { return g(x); });
  186.     }
  187.     template <class G> int min_left(int r, G g)
  188.     {
  189.         assert(0 <= r && r <= _n);
  190.         assert(g(e()));
  191.         if (r == 0)
  192.             return 0;
  193.         r += size;
  194.         for (int i = log; i >= 1; i--)
  195.             push((r - 1) >> i);
  196.         S sm = e();
  197.         do {
  198.             r--;
  199.             while (r > 1 && (r % 2))
  200.                 r >>= 1;
  201.             if (!g(op(d[r], sm))) {
  202.                 while (r < size) {
  203.                     push(r);
  204.                     r = (2 * r + 1);
  205.                     if (g(op(d[r], sm))) {
  206.                         sm = op(d[r], sm);
  207.                         r--;
  208.                     }
  209.                 }
  210.                 return r + 1 - size;
  211.             }
  212.             sm = op(d[r], sm);
  213.         } while ((r & -r) != r);
  214.         return 0;
  215.     }
  216.  
  217.   private:
  218.     int _n, size, log;
  219.     std::vector<S> d;
  220.     std::vector<F> lz;
  221.  
  222.     void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
  223.     void all_apply(int k, F f)
  224.     {
  225.         d[k] = mapping(f, d[k]);
  226.         if (k < size)
  227.             lz[k] = composition(f, lz[k]);
  228.     }
  229.     void push(int k)
  230.     {
  231.         all_apply(2 * k, lz[k]);
  232.         all_apply(2 * k + 1, lz[k]);
  233.         lz[k] = id();
  234.     }
  235. };
  236.  
  237. } // namespace atcoder
  238.  
  239. template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
  240. template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
  241. template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
  242. template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
  243.  
  244. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  245.  
  246. using ll = long long;
  247. using pii = pair<int, int>;
  248. using vl = vector<ll>;
  249. using vi = vector<int>;
  250. using atcoder::lazy_segtree;
  251. using a3l = array<ll, 3>;
  252.  
  253. struct S {
  254.     ll l, r, sum;
  255. };
  256. S e() { return {0, 0, 0}; }
  257. S op(S a, S b) { return {min(a.l, b.l), max(a.r, b.r), a.sum + b.sum}; }
  258. struct F {
  259.     // op: [beg, end) and delta
  260.     vector<a3l> ops;
  261. };
  262. S mapping(F f, S x)
  263. {
  264.     for (auto [beg, end, delta] : f.ops) {
  265.         if (end <= x.l)
  266.             continue;
  267.         if (beg > x.r)
  268.             break;
  269.         // ll a = max(x.l, beg + 1) - beg, b = min(x.r - 1, end) - beg;
  270.         ll a = max(x.l, beg) - beg + 1, b = min(x.r, end) - beg;
  271.         x.sum += (a + b) * (b - a + 1) * delta / 2;
  272.     }
  273.     return x;
  274. }
  275. F compose(F f, F g)
  276. {
  277.     F ret;
  278.     int i1, i2;
  279.     for (i1 = 0, i2 = 0; i1 < f.ops.size() && i2 < g.ops.size();) {
  280.         auto [fbeg, fend, fdelta] = f.ops[i1];
  281.         auto [gbeg, gend, gdelta] = g.ops[i2];
  282.         if (fend <= gbeg)
  283.             ret.ops.push_back(f.ops[i1++]);
  284.         else if (gend <= fbeg)
  285.             ret.ops.push_back(g.ops[i2++]);
  286.         else {
  287.             ll dt = fdelta + gdelta;
  288.             ll a = max(fbeg, gbeg);
  289.             ll b = min(fend, gend);
  290.             if (fend == gend)
  291.                 ++i1, ++i2;
  292.             else if (fend < gend)
  293.                 ++i1;
  294.             else
  295.                 ++i2;
  296.             ret.ops.push_back(a3l{a, b, dt});
  297.         }
  298.     }
  299.     while (i1 < f.ops.size())
  300.         ret.ops.push_back(f.ops[i1++]);
  301.     while (i2 < g.ops.size())
  302.         ret.ops.push_back(g.ops[i2++]);
  303.     return ret;
  304. }
  305. F id() { return {}; }
  306.  
  307. int main(int argc, char **argv)
  308. {
  309.     int n, q;
  310.     cin >> n >> q;
  311.     vector<ll> acc(n + 1);
  312.     vector<ll> arr(n);
  313.     for (auto &x : arr)
  314.         cin >> x;
  315.     for (int i = 0; i < n; ++i)
  316.         acc[i + 1] = acc[i] + arr[i];
  317.     vector<S> v(n);
  318.     for (int i = 0; i < n; ++i)
  319.         v[i] = {i, i + 1, 0};
  320.     lazy_segtree<S, op, e, F, mapping, compose, id> seg(v);
  321.     for (int i = 0; i < q; ++i) {
  322.         int op, a, b;
  323.         cin >> op >> a >> b;
  324.         if (op == 1) {
  325.             seg.apply(a - 1, b, F{vector<a3l>{a3l{a - 1, b, 1}}});
  326.         } else {
  327.             cout << acc[b] - acc[a - 1] + seg.prod(a - 1, b).sum << endl;
  328.         }
  329.     }
  330.     return 0;
  331. };
  332.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement