niyaznigmatullin

Untitled

Jan 24th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int const N = 222222;
  6. int const B = 678;
  7. int const C = N / B;
  8.  
  9. vector<int> blocks[C];
  10. int a[N];
  11.  
  12. void recalc() {
  13.   int cn = 0;
  14.   for (int i = 0; !blocks[i].empty(); i++) {
  15.     for (int j : blocks[i])
  16.       a[cn++] = j;
  17.     blocks[i].clear();
  18.   }
  19.   for (int i = 0, cur = 0; cur < cn; i++) {
  20.     while (cur < cn && (int) blocks[i].size() < B) {
  21.       blocks[i].push_back(a[cur++]);
  22.     }
  23.   }
  24. }
  25.  
  26. int main() {
  27.   freopen("river.in", "r", stdin);
  28.   freopen("river.out", "w", stdout);
  29.   int n, noneed;
  30.   scanf("%d%d", &n, &noneed);
  31.   long long ans = 0;
  32.   for (int i = 0; i < n; i++) {
  33.     int x;
  34.     scanf("%d", &x);
  35.     ans += (long long) x * x;
  36.     blocks[0].push_back(x);
  37.   }  
  38.   recalc();
  39.   int q;
  40.   scanf("%d", &q);
  41.   printf("%I64d\n", ans);
  42.   for (int cq = 0; cq < q; cq++) {
  43.     int type, id;
  44.     scanf("%d%d", &type, &id);
  45.     --id;
  46.     if (type == 1) {
  47.       int b = 0;
  48.       int w = id;
  49.       while ((int) blocks[b].size() <= w) {
  50.         w -= blocks[b].size();
  51.         b++;
  52.       }
  53.       int size = blocks[b][w];
  54.       ans -= (long long) size * size;
  55.       blocks[b].erase(blocks[b].begin() + w);
  56.       int nb = b;
  57.       int nw = w;
  58.       while (nb + 1 < C && nw == (int) blocks[nb].size()) {
  59.         nw = 0;
  60.         nb++;
  61.       }
  62.       int pb = b;
  63.       int pw = w - 1;
  64.       while (pb > 0 && pw < 0) {
  65.         --pb;
  66.         pw = (int) blocks[pb].size() - 1;
  67.       }
  68.       bool hasNext = nw < (int) blocks[nb].size();
  69.       bool hasPrev = pw >= 0;
  70.      
  71.       if (hasNext) ans -= (long long) blocks[nb][nw] * blocks[nb][nw];
  72.       if (hasPrev) ans -= (long long) blocks[pb][pw] * blocks[pb][pw];
  73.      
  74.       if (hasNext && hasPrev) {
  75.         blocks[pb][pw] += size / 2;
  76.         blocks[nb][nw] += (size + 1) / 2;
  77.       } else if (hasNext)
  78.         blocks[nb][nw] += size;
  79.       else
  80.         blocks[pb][pw] += size;
  81.        
  82.       if (hasNext) ans += (long long) blocks[nb][nw] * blocks[nb][nw];
  83.       if (hasPrev) ans += (long long) blocks[pb][pw] * blocks[pb][pw];
  84.     } else {
  85.       int b = 0;
  86.       int w = id;
  87.       while ((int) blocks[b].size() <= w) {
  88.         w -= blocks[b].size();
  89.         b++;
  90.       }
  91.       int half = blocks[b][w] / 2;
  92.       ans -= (long long) blocks[b][w] * blocks[b][w];
  93.       blocks[b][w] -= half;
  94.       ans += (long long) blocks[b][w] * blocks[b][w];
  95.       blocks[b].insert(blocks[b].begin() + w, half);
  96.       ans += (long long) blocks[b][w] * blocks[b][w];
  97.     }
  98.     if (cq % (15 * B) == 0) recalc();
  99.     printf("%I64d\n", ans);
  100.   }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment