Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int LEN = 444;
- int n, p;
- vector <vll> q;
- vll a;
- ll ANS;
- pii find_pos(int pos)
- {
- int bl = 0;
- for (int x = 0; x < q.size(); ++x)
- {
- if (pos >= q[x].size())
- pos -= q[x].size(), ++bl;
- else
- return mk(bl, pos);
- }
- }
- void add_block_build(int pos)
- {
- q.inb(a);
- if (pos)
- {
- ll x = q[pos - 1].back();
- q[pos - 1].pop_back();
- q[pos].inb(x);
- }
- }
- void add_block(int pos)
- {
- vll sw;
- int sz = q[pos - 1].size();
- for (int i = sz - 1; i >= sz / 2; --i)
- {
- sw.inb(q[pos - 1].back());
- q[pos - 1].pop_back();
- }
- reverse(all(sw));
- q.insert(q.begin() + pos, sw);
- }
- void add_x(ll x)
- {
- q.back().inb(x);
- ANS += x * x;
- if (q.back().size() == LEN)
- add_block_build(q.size());
- }
- void delete_block(int pos)
- {
- q.erase(q.begin() + pos);
- }
- void delete_inc(pii npos)
- {
- int bl = npos.X, pos = npos.Y;
- ll x = q[bl][pos];
- ll xl = x >> 1ll;
- ll xr = x - xl;
- ANS -= x * x;
- q[bl].erase(q[bl].begin() + pos);
- if (q[bl].empty())
- {
- delete_block(bl), pos = 0;
- if (bl == q.size())
- --bl, pos = q[bl].size();
- }
- if (!bl && !pos)
- ANS -= q[0][0] * q[0][0], q[0][0] += x, ANS += q[0][0] * q[0][0];
- else
- {
- int lind = pos - 1;
- int rind = pos;
- int rb = bl;
- int lb = bl;
- if (rind == q[rb].size() && rb == q.size() - 1)
- {
- ANS -= q[lb][lind] * q[lb][lind];
- q[lb][lind] += x;
- ANS += q[lb][lind] * q[lb][lind];
- }
- else
- {
- if (lind == -1)
- {
- --lb;
- lind = q[lb].size() - 1;
- }
- if (rind == q[rb].size())
- ++rb, rind = 0;
- ANS -= q[lb][lind] * q[lb][lind];
- ANS -= q[rb][rind] * q[rb][rind];
- q[lb][lind] += xl;
- q[rb][rind] += xr;
- ANS += q[lb][lind] * q[lb][lind];
- ANS += q[rb][rind] * q[rb][rind];
- }
- }
- }
- void split_inc(pii npos)
- {
- int bl = npos.X, pos = npos.Y;
- ll x = q[bl][pos];
- ANS -= x * x;
- ll xl = x >> 1ll;
- ll xr = x - xl;
- q[bl][pos] = xl;
- q[bl].insert(q[bl].begin() + pos + 1, xr);
- ANS += xl * xl + xr * xr;
- if (q[bl].size() == LEN)
- add_block(bl + 1);
- }
- vll mysol, rsol;
- int solve()
- {
- add_block_build(0);
- scanf("%d %d", &n, &p);
- forn(i, n)
- {
- ll x;
- scanf("%lld", &x);
- add_x(x);
- }
- int Q;
- scanf("%d", &Q);
- printf("%lld\n", ANS);
- forn(i, Q)
- {
- int cmd, pos;
- scanf("%d %d", &cmd, &pos);
- --pos, --cmd;
- pii npos = find_pos(pos);
- if (!cmd)
- delete_inc(npos);
- else
- split_inc(npos);
- printf("%lld\n", ANS);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement