Advertisement
cosenza987

Untitled

Feb 10th, 2023
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 KB | None | 0 0
  1. //Слава Україні, Героям слава
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. const int N = 1e5 + 7, L = 20;
  8. const int M = 4 * N * L;
  9. const int INF = 1e9 + 7;
  10.  
  11. namespace dseg {
  12.     long long seg[M];
  13.     int L[M], R[M], cnt = 2;
  14.     void insert(int x, long long val, int p = 1, int l = 1, int r = INF) {
  15.         seg[p] += val;
  16.         if(l == r) {
  17.             return;
  18.         }
  19.         int mid = (l + r) >> 1;
  20.         if(x <= mid) {
  21.             if(!L[p]) {
  22.                 L[p] = cnt++;
  23.             }
  24.             insert(x, val, L[p], l, mid);
  25.         } else {
  26.             if(!R[p]) {
  27.                 R[p] = cnt++;
  28.             }
  29.             insert(x, val, R[p], mid + 1, r);
  30.         }
  31.     }
  32.     long long sum(int i, int j, int p = 1, int l = 1, int r = INF) {
  33.         if(i > r or j < l) {
  34.             return 0;
  35.         }
  36.         if(i <= l and r <= j) {
  37.             return seg[p];
  38.         }
  39.         int mid = (l + r) >> 1;
  40.         return sum(i, j, L[p], l, mid) + sum(i, j, R[p], mid + 1, r);
  41.     }
  42. };
  43.  
  44. int main() {
  45.     ios_base::sync_with_stdio(false);
  46.     cin.tie(nullptr);
  47.     int n, m, q;
  48.     cin >> n >> m >> q;
  49.     set<pair<long long, long long>> s;
  50.     vector<int> v(m);
  51.     for(int i = 0; i < m; i++) {
  52.         cin >> v[i];
  53.     }
  54.     sort(v.begin(), v.end());
  55.     for(int i = 0; i < m; i++) {
  56.         int x = v[i];
  57.         if(s.empty()) {
  58.             s.insert({x, x});
  59.             continue;
  60.         }
  61.         auto itr = s.end(); itr--;
  62.         if(itr->second + 1 == x) {
  63.             int l = itr->first;
  64.             s.erase(itr);
  65.             s.insert({l, x});
  66.         } else {
  67.             s.insert({x, x});
  68.         }
  69.     }
  70.     for(auto e : s) {
  71.         dseg::insert(e.first, (e.second - e.first + 1) * (e.second - e.first + 1));
  72.     }
  73.     while(q--) {
  74.         string ss;
  75.         cin >> ss;
  76.         if(ss == "go") {
  77.             int l, r;
  78.             cin >> l >> r;
  79.             if(l > r) {
  80.                 swap(l, r);
  81.             }
  82.             cout << dseg::sum(l, r) << "\n";
  83.         } else if(ss == "add") {
  84.             int x;
  85.             cin >> x;
  86.             auto itr = s.upper_bound({x, 0});
  87.             itr--;
  88.             auto tmp = *itr;
  89.             if(itr->first < x and itr->second > x) {
  90.                 s.erase(itr);
  91.                 dseg::insert(tmp.first, -1ll * (tmp.second - tmp.first + 1) * (tmp.second - tmp.first + 1));
  92.                 s.insert({tmp.first, x - 1});
  93.                 dseg::insert(tmp.first, (x - 1 - tmp.first + 1) * (x - 1 - tmp.first + 1));
  94.                 s.insert({x + 1, tmp.second});
  95.                 dseg::insert(x + 1, (tmp.second - x - 1 + 1) * (tmp.second - x - 1 + 1));
  96.             } else if(itr->first == x and itr->second > x) {
  97.                 s.erase(itr);
  98.                 dseg::insert(tmp.first, -1ll * (tmp.second - tmp.first + 1) * (tmp.second - tmp.first + 1));
  99.                 s.insert({x + 1, tmp.second});  
  100.                 dseg::insert(x + 1, (tmp.second - x - 1 + 1) * (tmp.second - x - 1 + 1));
  101.             } else if(itr->second == x and itr->first < x) {
  102.                 s.erase(itr);
  103.                 dseg::insert(tmp.first, -1ll * (tmp.second - tmp.first + 1) * (tmp.second - tmp.first + 1));
  104.                 s.insert({tmp.first, x - 1});
  105.                 dseg::insert(tmp.first, (x - 1 - tmp.first + 1) * (x - 1 - tmp.first + 1));
  106.             } else {
  107.                 s.erase(itr);
  108.                 dseg::insert(tmp.first, -1 * (tmp.second - tmp.first + 1) * (tmp.second - tmp.first + 1));
  109.             }
  110.         } else if(ss == "rem") {
  111.             int x;
  112.             cin >> x;
  113.             auto itr = s.lower_bound({x, 0});
  114.             bool j = false;
  115.             if(itr != s.end()) {
  116.                 if(itr->first == x + 1) {
  117.                     j = true;
  118.                     auto tmp = *itr;
  119.                     s.erase(itr);
  120.                     dseg::insert(tmp.first, -1ll * (tmp.second - tmp.first + 1) * (tmp.second - tmp.first + 1));
  121.                     s.insert({x, tmp.second});
  122.                     dseg::insert(x, (tmp.second - x + 1) * (tmp.second - x + 1));
  123.                 }
  124.             }
  125.             itr = s.lower_bound({x, 0});
  126.             if(itr != s.begin()) {
  127.                 itr--;
  128.                 if(itr->second == x - 1) {
  129.                     if(j) {
  130.                         long long l = itr->first;
  131.                         itr++;
  132.                         long long r = itr->second;
  133.                         auto tmp = itr;
  134.                         itr--;
  135.                         dseg::insert(itr->first, -1ll * (itr->second - itr->first + 1) * (itr->second - itr->first + 1));
  136.                         dseg::insert(tmp->first, -1ll * (tmp->second - tmp->first + 1) * (tmp->second - tmp->first + 1));
  137.                         s.erase(itr);
  138.                         s.erase(tmp);
  139.                         s.insert({l, r});
  140.                         dseg::insert(l, (r - l + 1) * (r - l + 1));
  141.                     } else {
  142.                         j = true;
  143.                         auto tmp = *itr;
  144.                         dseg::insert(itr->first, -1ll * (itr->second - itr->first + 1) * (itr->second - itr->first + 1));
  145.                         s.erase(itr);
  146.                         s.insert({tmp.first, x});
  147.                         dseg::insert(tmp.first, (x - tmp.first + 1) * (x - tmp.first + 1));
  148.                     }
  149.                 }
  150.             }
  151.             if(!j) {
  152.                 s.insert({x, x});
  153.                 dseg::insert(x, 1);
  154.             }
  155.         }
  156.     }
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement