Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int bal = 450;
- int n, p;
- vector <vll> q;
- vll qrs;
- vll a;
- pii find_pos(int pos)
- {
- int bl = 0;
- for (auto x : q)
- {
- if (pos >= x.size())
- pos -= x.size(), ++bl;
- else
- return mk(bl, pos);
- }
- }
- void add_block(int pos)
- {
- q.insert(q.begin() + pos, a);
- qrs.insert(qrs.begin() + pos, 0);
- if (pos)
- {
- ll x = q[pos - 1].back();
- q[pos - 1].pop_back();
- q[pos].inb(x);
- qrs[pos - 1] -= x * x;
- qrs[pos] += x * x;
- }
- }
- void add_x(ll x)
- {
- q.back().inb(x);
- qrs.back() += x * x;
- if (q.back().size() == bal)
- add_block(q.size());
- }
- void delete_block(int pos)
- {
- q.erase(q.begin() + pos);
- qrs.erase(qrs.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;
- qrs[bl] -= 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() - 1;
- }
- if (!bl && !pos)
- qrs[0] -= q[0][0] * q[0][0], q[0][0] += x, qrs[0] += q[0][0] * q[0][0];
- else
- {
- int lind = pos - 1;
- int rind = pos;
- int rb = bl;
- int lb = bl;
- rind = min(rind, (int)q[rb].size() - 1);
- if (lind == -1)
- {
- --lb;
- lind = q[lb].size() - 1;
- }
- if (lind != rind)
- {
- qrs[lb] -= q[lb][lind] * q[lb][lind];
- qrs[rb] -= q[rb][rind] * q[rb][rind];
- q[lb][lind] += xl;
- q[rb][rind] += xr;
- qrs[lb] += q[lb][lind] * q[lb][lind];
- qrs[rb] += q[rb][rind] * q[rb][rind];
- }
- else
- {
- qrs[lb] -= q[lb][lind] * q[lb][lind];
- q[lb][lind] += x;
- qrs[lb] += q[lb][lind] * q[lb][lind];
- }
- }
- }
- void split_inc(pii npos)
- {
- int bl = npos.X, pos = npos.Y;
- ll x = q[bl][pos];
- qrs[bl] -= x * x;
- ll xl = x >> 1ll;
- ll xr = x - xl;
- q[bl][pos] = xl;
- q[bl].insert(q[bl].begin() + pos + 1, xr);
- qrs[bl] += xl * xl + xr * xr;
- if (q[bl].size() == bal)
- add_block(pos + 1);
- }
- ll get()
- {
- ll ans = 0;
- for (auto x : qrs)
- ans += x;
- return ans;
- }
- int solve()
- {
- add_block(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", get());
- 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", get());
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement