1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include <bits/stdc++.h> #define lson(u) (node[u].l) #define rson(u) (node[u].r) #define sum(u) (node[u].sum) using namespace std; typedef long long ll;
const int MAXN = 300005;
struct Node { int l, r, sum; }node[MAXN * 42]; int cnt; int root[MAXN], size[MAXN]; vector<ll> ins[MAXN];
int N, M, Q, T;
void modify(int& u, int l, int r, int p, int val) { if (u == 0) u = ++cnt; if (l == r) { sum(u) += val; return; } int mid = (l + r) >> 1; if (p <= mid) modify(lson(u), l, mid, p, val); else modify(rson(u), mid + 1, r, p, val); sum(u) = sum(lson(u)) + sum(rson(u)); }
int query(int u, int l, int r, int k) { if (l == r) return l; int mid = (l + r) >> 1; int lsum = sum(lson(u)); if (l <= T) { lsum += (mid > T) ? T - l + 1 : mid - l + 1; } if (k <= lsum) return query(lson(u), l, mid, k); else return query(rson(u), mid + 1, r, k - lsum); }
int main() { scanf("%d%d%d", &N, &M, &Q); int Case = Q; while (Case--) { int x, y; scanf("%d%d", &x, &y); ll ans = 0, tmp = 0; if (y == M) { T = N; ans = query(root[N + 1], 1, N + Q, x); modify(root[N + 1], 1, N + Q, ans, -1); modify(root[N + 1], 1, N + Q, N + (++size[N + 1]), 1); if (ans <= N) ans = ans * (ll)M; else ans = ins[N + 1][ans - N - 1]; ins[N + 1].push_back(ans); printf("%lld\n", ans); } else { T = M - 1; ans = query(root[x], 1, M + Q, y); modify(root[x], 1, M + Q, ans, -1); modify(root[x], 1, M + Q, M - 1 + (++size[x]), 1); if (ans <= M - 1) ans += (ll)(x - 1) * M; else ans = ins[x][ans - M]; T = N; tmp = query(root[N + 1], 1, N + Q, x); modify(root[N + 1], 1, N + Q, tmp, -1); modify(root[N + 1], 1, N + Q, N + (++size[N + 1]), 1); if (tmp <= N) tmp = tmp * (ll)M; else tmp = ins[N + 1][tmp - N - 1]; ins[N + 1].push_back(ans); ins[x].push_back(tmp); printf("%lld\n", ans); } } return 0; }
|