Advertisement
K_Y_M_bl_C

Untitled

Jan 7th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. const int LEN = 375;
  2.  
  3. int n, p;
  4. vector <vll> q;
  5. vll a;
  6. ll ANS;
  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_build(int pos)
  21. {
  22.     q.inb(a);
  23.     if (pos)
  24.     {
  25.         ll x = q[pos - 1].back();
  26.         q[pos - 1].pop_back();
  27.         q[pos].inb(x);
  28.     }
  29. }
  30.  
  31. void add_block(int pos)
  32. {
  33.     vll sw;
  34.     int sz = q[pos - 1].size();
  35.     for (int i = sz - 1; i >= sz / 2; --i)
  36.     {
  37.         sw.inb(q[pos - 1].back());
  38.         q[pos - 1].pop_back();
  39.     }
  40.     reverse(all(sw));
  41.     q.insert(q.begin() + pos, sw);
  42. }
  43.  
  44. void add_x(ll x)
  45. {
  46.     q.back().inb(x);
  47.     ANS += x * x;
  48.     if (q.back().size() == LEN)
  49.         add_block_build(q.size());
  50. }
  51.  
  52. void delete_block(int pos)
  53. {
  54.     q.erase(q.begin() + pos);
  55. }
  56.  
  57. void delete_inc(pii npos)
  58. {
  59.     int bl = npos.X, pos = npos.Y;
  60.     ll x = q[bl][pos];
  61.     ll xl = x >> 1ll;
  62.     ll xr = x - xl;
  63.     ANS -= x * x;
  64.     q[bl].erase(q[bl].begin() + pos);
  65.     if (q[bl].empty())
  66.     {
  67.         delete_block(bl), pos = 0;
  68.         if (bl == q.size())
  69.             --bl, pos = q[bl].size() - 1;
  70.     }
  71.     if (!bl && !pos)
  72.         ANS -= q[0][0] * q[0][0], q[0][0] += x, ANS += q[0][0] * q[0][0];
  73.     else
  74.     {
  75.         int lind = pos - 1;
  76.         int rind = pos;
  77.         int rb = bl;
  78.         int lb = bl;
  79.         rind = min(rind, (int)q[rb].size() - 1);
  80.         if (lind == -1)
  81.         {
  82.             --lb;
  83.             lind = q[lb].size() - 1;
  84.         }
  85.         if (lind != rind)
  86.         {
  87.             ANS -= q[lb][lind] * q[lb][lind];
  88.             ANS -= q[rb][rind] * q[rb][rind];
  89.             q[lb][lind] += xl;
  90.             q[rb][rind] += xr;
  91.             ANS += q[lb][lind] * q[lb][lind];
  92.             ANS += q[rb][rind] * q[rb][rind];
  93.         }
  94.         else
  95.         {
  96.             ANS -= q[lb][lind] * q[lb][lind];
  97.             q[lb][lind] += x;
  98.             ANS += q[lb][lind] * q[lb][lind];
  99.         }
  100.     }
  101. }
  102.  
  103. void split_inc(pii npos)
  104. {
  105.     int bl = npos.X, pos = npos.Y;
  106.     ll x = q[bl][pos];
  107.     ANS -= x * x;
  108.     ll xl = x >> 1ll;
  109.     ll xr = x - xl;
  110.     q[bl][pos] = xl;
  111.     q[bl].insert(q[bl].begin() + pos + 1, xr);
  112.     ANS += xl * xl + xr * xr;
  113.     if (q[bl].size() == LEN)
  114.         add_block(bl + 1);
  115. }
  116. int solve()
  117. {
  118.     add_block_build(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", ANS);
  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", ANS);
  140.     }
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement