inlinevoidadd(int &k, int l, int r, int x){ if (!k) k = ++ cnt; if (l == r) {++ p[k].sum; return;} int mid = l + r >> 1; if (x <= mid) add(p[k].ls, l, mid, x); elseadd(p[k].rs, mid + 1, r, x); pushup(k); }
inlinevoidupdate(int &k, int l, int r, int x){ if (!k) return; int mid = l + r >> 1; if (mid > x) update(p[k].ls, l, mid, x); else { p[k].sum -= p[p[k].ls].sum; ans += p[p[k].ls].sum; p[k].ls = 0; } if (mid < x) update(p[k].rs, mid + 1, r, x); pushup(k); }
inlineintkth(int k, int l, int r, int x){ if (l == r) return l; int mid = l + r >> 1, rsum = p[p[k].rs].sum; if (rsum >= x) returnkth(p[k].rs, mid + 1, r, x); elsereturnkth(p[k].ls, l, mid, x - rsum); }
intmain(){ scanf("%d%d", &n, &m); char c; int x; while (n --) { cin >> c >> x; if (c == 'I') { if (x < m) continue; add(rt, -UPL, UPL, x - tag); } elseif (c == 'A') tag += x; elseif (c == 'S') { tag -= x; update(rt, -UPL, UPL, m - tag - 1); } else { if (x > p[rt].sum) {printf("-1\n"); continue;} printf("%d\n", kth(rt, -UPL, UPL, x) + tag); } } printf("%d", ans); return0; }
inlinevoidadd(int u, int v){ e[cnt].u = u, e[cnt].v = v; e[cnt].nxt = head[u], head[u] = cnt ++; }
voidupdate(int &k, int l, int r, int x){ if (!k) k = New(); if (l == r) {p[k].tot = (ll)l, ++ p[k].val; return;} int mid = (l + r) >> 1; if (x <= mid) update(p[k].ls, l, mid, x); elseupdate(p[k].rs, mid + 1, r, x); pushup(k); }
intmerge(int k1, int k2, int l, int r){ if (!k1 || !k2) return k1 + k2; if (l == r) { p[k1].val += p[k2].val; recover(k2); return k1; } int mid = (l + r) >> 1; p[k1].ls = merge(p[k1].ls, p[k2].ls, l, mid); p[k1].rs = merge(p[k1].rs, p[k2].rs, mid + 1, r); pushup(k1); recover(k2); return k1; }
voiddfs(int x, int fa){ for (int i = head[x]; i; i = e[i].nxt) { if (e[i].v == fa) continue; dfs(e[i].v, x); rt[x] = merge(rt[x], rt[e[i].v], 1, n); } f[x] = p[rt[x]].tot; }
intmain(){ scanf("%d", &n); for (int i = 1; i <= n; ++ i) { scanf("%d", &c[i]); update(rt[i], 1, n, c[i]); } for (int i = 1; i < n; ++ i) { int u, v; scanf("%d%d", &u, &v); add(u, v); add(v, u); } dfs(1, 0); for (int i = 1; i <= n; ++ i) printf("%lld ", f[i]); return0; }
voidupdate(int &k, int l, int r, ll x){ if (!k) k = ++ cnt; if (l == r) {p[k].sum += x, ++ p[k].tot; return;} int mid = (l + r) >> 1; if (x <= mid) update(p[k].ls, l, mid, x); elseupdate(p[k].rs, mid + 1, r, x); pushup(k); }
intquery(int k, int l, int r, ll s){ if (!k) return0; if (l == r) { if (l == 0) return0; int res = s / l; if (res <= p[k].tot) return res; elsereturn p[k].tot; } int mid = (l + r) >> 1; ll lsum = p[p[k].ls].sum; if (lsum >= s) returnquery(p[k].ls, l, mid, s); elsereturn p[p[k].ls].tot + query(p[k].rs, mid + 1, r, s - lsum); }
intmain(){ scanf("%d", &t); while (t --) { clear(); scanf("%d%lld", &n, &m); for (int i = 1; i <= n; ++ i) scanf("%lld", &a[i]); for (int i = 1; i <= n; ++ i) { printf("%d ", i - 1 - query(rt, 0, m, m - a[i])); update(rt, 0, m, a[i]); } printf("\n"); } return0; }