Advertisement
K_Y_M_bl_C

Untitled

Jan 7th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. const int bal = 450;
  2.  
  3. int n, p;
  4. vector <vll> q;
  5. vll qrs;
  6. vll a;
  7.  
  8. pii find_pos(int pos)
  9. {
  10.     int bl = 0;
  11.     for (auto x : q)
  12.     {
  13.         if (pos >= x.size())
  14.             pos -= x.size(), ++bl;
  15.         else
  16.             return mk(bl, pos);
  17.     }
  18. }
  19.  
  20. void add_block(int pos)
  21. {
  22.     q.insert(q.begin() + pos, a);
  23.     qrs.insert(qrs.begin() + pos, 0);
  24.     if (pos)
  25.     {
  26.         ll x = q[pos - 1].back();
  27.         q[pos - 1].pop_back();
  28.         q[pos].inb(x);
  29.         qrs[pos - 1] -= x * x;
  30.         qrs[pos] += x * x;
  31.     }
  32. }
  33.  
  34. void add_x(ll x)
  35. {
  36.     q.back().inb(x);
  37.     qrs.back() += x * x;
  38.     if (q.back().size() == bal)
  39.         add_block(q.size());
  40. }
  41.  
  42. void delete_block(int pos)
  43. {
  44.     q.erase(q.begin() + pos);
  45.     qrs.erase(qrs.begin() + pos);
  46. }
  47.  
  48. void delete_inc(pii npos)
  49. {
  50.     int bl = npos.X, pos = npos.Y;
  51.     ll x = q[bl][pos];
  52.     ll xl = x >> 1ll;
  53.     ll xr = x - xl;
  54.     qrs[bl] -= x * x;
  55.     q[bl].erase(q[bl].begin() + pos);
  56.     if (q[bl].empty())
  57.     {
  58.         delete_block(bl), pos = 0;
  59.         if (bl == q.size())
  60.             --bl, pos = q[bl].size() - 1;
  61.     }
  62.     if (!bl && !pos)
  63.         qrs[0] -= q[0][0] * q[0][0], q[0][0] += x, qrs[0] += q[0][0] * q[0][0];
  64.     else
  65.     {
  66.         int lind = pos - 1;
  67.         int rind = pos;
  68.         int rb = bl;
  69.         int lb = bl;
  70.         rind = min(rind, (int)q[rb].size() - 1);
  71.         if (lind == -1)
  72.         {
  73.             --lb;
  74.             lind = q[lb].size() - 1;
  75.         }
  76.         if (lind != rind)
  77.         {
  78.             qrs[lb] -= q[lb][lind] * q[lb][lind];
  79.             qrs[rb] -= q[rb][rind] * q[rb][rind];
  80.             q[lb][lind] += xl;
  81.             q[rb][rind] += xr;
  82.             qrs[lb] += q[lb][lind] * q[lb][lind];
  83.             qrs[rb] += q[rb][rind] * q[rb][rind];
  84.         }
  85.         else
  86.         {
  87.             qrs[lb] -= q[lb][lind] * q[lb][lind];
  88.             q[lb][lind] += x;
  89.             qrs[lb] += q[lb][lind] * q[lb][lind];
  90.         }
  91.     }
  92. }
  93.  
  94. void split_inc(pii npos)
  95. {
  96.     int bl = npos.X, pos = npos.Y;
  97.     ll x = q[bl][pos];
  98.     qrs[bl] -= x * x;
  99.     ll xl = x >> 1ll;
  100.     ll xr = x - xl;
  101.     q[bl][pos] = xl;
  102.     q[bl].insert(q[bl].begin() + pos + 1, xr);
  103.     qrs[bl] += xl * xl + xr * xr;
  104.     if (q[bl].size() == bal)
  105.         add_block(pos + 1);
  106. }
  107.  
  108. ll get()
  109. {
  110.     ll ans = 0;
  111.     for (auto x : qrs)
  112.         ans += x;
  113.     return ans;
  114. }
  115.  
  116. int solve()
  117. {
  118.     add_block(0);
  119.     scanf("%d %d", &n, &p);
  120.     forn(i, n)
  121.     {
  122.         ll x;
  123.         scanf("%lld", &x);
  124.         add_x(x);
  125.     }
  126.     int Q;
  127.     scanf("%d", &Q);
  128.     printf("%lld\n", get());
  129.     forn(i, Q)
  130.     {
  131.         int cmd, pos;
  132.         scanf("%d %d", &cmd, &pos);
  133.         --pos, --cmd;
  134.         pii npos = find_pos(pos);
  135.         if (!cmd)
  136.             delete_inc(npos);
  137.         else
  138.             split_inc(npos);
  139.         printf("%lld\n", get());
  140.     }
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement