Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #ifndef __DEBUG__
- #define dbg(...) 42
- #endif
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using vl = vector<ll>;
- using vi = vector<int>;
- int main(int argc, char **argv)
- {
- cin.tie(nullptr)->sync_with_stdio(false);
- int n, k, q, x, y;
- cin >> n >> k >> q;
- ll acc = 0;
- using a2i = array<int, 2>;
- set<a2i> ms;
- for (int i = 0; i < n; ++i)
- ms.insert({0, i});
- auto bar = ms.end();
- for (int i = 0; i < k; ++i)
- bar = prev(bar);
- vi a(n + 1);
- for (int i = 0; i < q; ++i) {
- cin >> x >> y;
- if (k != n) {
- a2i old_v = {a[x], x}, new_v = {y, x};
- a[x] = y;
- if (x == 1)
- assert(distance(bar, ms.end()) == 2), assert(bar->at(1) == 3);
- dbg(x, old_v[0], new_v[0]);
- dbg(*bar);
- if (old_v >= *bar)
- bar = prev(bar), acc = acc - old_v[0] + bar->at(0);
- ms.erase(old_v);
- dbg(*bar);
- ms.insert(new_v);
- dbg(*bar);
- if (new_v > *bar)
- acc = acc + new_v[0] - bar->at(0), bar = next(bar);
- dbg(*bar);
- } else {
- acc = acc + y - a[x];
- a[x] = y;
- }
- cout << acc << endl;
- }
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement