Advertisement
PlanttPastes

Светилките на Дедо Мраз

Apr 15th, 2024 (edited)
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #define ONLINE_JUDGE // online judge
  2. #include <iostream>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. const int MXS = 300000;
  7. int sgt[MXS], sgtl[MXS], sgtr[MXS], sgtlz[MXS];
  8. int n, s;
  9.  
  10. void sgtupdate(int node) {
  11.     if (!sgtlz[node]) return;
  12.     if (sgtlz[node] == 1) sgt[node] = sgtr[node] - sgtl[node] + 1;
  13.     else sgt[node] = 0;
  14.     if (node < s) {
  15.         sgtlz[node * 2 + 1] = sgtlz[node * 2 + 2] = sgtlz[node];
  16.     }
  17.     sgtlz[node] = 0;
  18.     /* while (node) {
  19.         node = node - 1 >> 1;
  20.         sgt[node] = sgt[2 * node + 1] + sgt[2 * node + 2];
  21.     } */
  22. }
  23. int sgtset(const int &node, const int &l, const int &r, const bool &v) {
  24.     if (sgtr[node] < l || sgtl[node] > r) return 0;
  25.     sgtupdate(node);
  26.     if (l <= sgtl[node] && sgtr[node] <= r) {
  27.         sgtlz[node] = v ? 1 : 2;
  28.         int oldVal = sgt[node];
  29.         sgtupdate(node);
  30.         // cerr << "update " << node << " from " << oldVal << " to " << sgt[node] << endl;
  31.         return sgt[node] - oldVal;
  32.     }
  33.     int delta = sgtset(node * 2 + 1, l, r, v) + sgtset(node * 2 + 2, l, r, v);
  34.     sgt[node] += delta;
  35.     return delta;
  36. }
  37. int sgtget(const int &node, const int &l, const int &r) {
  38.     if (sgtr[node] < l || sgtl[node] > r) return 0;
  39.     sgtupdate(node);
  40.     if (l <= sgtl[node] && sgtr[node] <= r) {
  41.         // cerr << "get " << node << ": " << sgt[node] << endl;
  42.         return sgt[node];
  43.     }
  44.     return sgtget(node * 2 + 1, l, r) + sgtget(node * 2 + 2, l, r);
  45. }
  46.  
  47. signed main() {
  48.     #ifdef ONLINE_JUDGE
  49.     ios::sync_with_stdio(0);
  50.     cin.tie(0); cout.tie(0);
  51.     #endif
  52.  
  53.     int q, a, b;
  54.     char c;
  55.     cin >> n >> q;
  56.     s = (1 << int(ceil(log2(n)))) - 1;
  57.     for (int i = 0; i < n; i++) {
  58.         cin >> sgt[s + i];
  59.     }
  60.     for (int i = s; i <= s * 2; i++) {
  61.         sgtl[i] = sgtr[i] = i - s;
  62.     }
  63.     for (int i = s - 1; i >= 0; i--) {
  64.         sgt[i] = sgt[2 * i + 1] + sgt[2 * i + 2];
  65.         sgtl[i] = sgtl[2 * i + 1];
  66.         sgtr[i] = sgtr[2 * i + 2];
  67.     }
  68.     while (q--) {
  69.         cin >> c >> a >> b; a--; b--;
  70.         if (c == '+') sgtset(0, a, b, 1);
  71.         if (c == '-') sgtset(0, a, b, 0);
  72.         if (c == '?') cout << sgtget(0, a, b) << '\n';
  73.     }
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement