Advertisement
K_Y_M_bl_C

Untitled

Jan 7th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. const int LEN = 444;
  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 (int x = 0; x < q.size(); ++x)
  12.     {
  13.         if (pos >= q[x].size())
  14.             pos -= q[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();
  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.         if (rind == q[rb].size() && rb == q.size() - 1)
  80.         {
  81.             ANS -= q[lb][lind] * q[lb][lind];
  82.             q[lb][lind] += x;
  83.             ANS += q[lb][lind] * q[lb][lind];
  84.         }
  85.         else
  86.         {
  87.             if (lind == -1)
  88.             {
  89.                 --lb;
  90.                 lind = q[lb].size() - 1;
  91.             }
  92.             if (rind == q[rb].size())
  93.                 ++rb, rind = 0;
  94.             ANS -= q[lb][lind] * q[lb][lind];
  95.             ANS -= q[rb][rind] * q[rb][rind];
  96.             q[lb][lind] += xl;
  97.             q[rb][rind] += xr;
  98.             ANS += q[lb][lind] * q[lb][lind];
  99.             ANS += q[rb][rind] * q[rb][rind];
  100.         }
  101.     }
  102. }
  103.  
  104. void split_inc(pii npos)
  105. {
  106.     int bl = npos.X, pos = npos.Y;
  107.     ll x = q[bl][pos];
  108.     ANS -= x * x;
  109.     ll xl = x >> 1ll;
  110.     ll xr = x - xl;
  111.     q[bl][pos] = xl;
  112.     q[bl].insert(q[bl].begin() + pos + 1, xr);
  113.     ANS += xl * xl + xr * xr;
  114.     if (q[bl].size() == LEN)
  115.         add_block(bl + 1);
  116. }
  117.  
  118. vll mysol, rsol;
  119.  
  120. int solve()
  121. {
  122.     add_block_build(0);
  123.     scanf("%d %d", &n, &p);
  124.     forn(i, n)
  125.     {
  126.         ll x;
  127.         scanf("%lld", &x);
  128.         add_x(x);
  129.     }
  130.     int Q;
  131.     scanf("%d", &Q);
  132.     printf("%lld\n", ANS);
  133.     forn(i, Q)
  134.     {
  135.         int cmd, pos;
  136.         scanf("%d %d", &cmd, &pos);
  137.         --pos, --cmd;
  138.         pii npos = find_pos(pos);
  139.         if (!cmd)
  140.             delete_inc(npos);
  141.         else
  142.             split_inc(npos);
  143.         printf("%lld\n", ANS);
  144.     }
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement