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
| #include <bits/stdc++.h> #define MAX_N 100000 #define mid ((s+t)>>1) using namespace std; typedef long long lnt; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, sz, val[MAX_N+5], ans[MAX_N+5]; struct juice {int d, p, v;} a[MAX_N+5]; struct query {int id; lnt w, v;} q[MAX_N+5], tq[MAX_N+5]; struct node {lnt p, v;} tr[MAX_N<<2]; bool mrk[MAX_N+5]; bool cmpd(const juice &a, const juice &b) {return a.d > b.d;} bool cmpp(const juice &a, const juice &b) {return a.p < b.p;} void update(int v) { tr[v].p = tr[v<<1].p+tr[v<<1|1].p; tr[v].v = tr[v<<1].v+tr[v<<1|1].v; } void modify(int v, int s, int t, int p, int x) { if (s == t) {tr[v].p += 1LL*p*x, tr[v].v += x; return;} if (p <= mid) modify(v<<1, s, mid, p, x), update(v); else modify(v<<1|1, mid+1, t, p, x), update(v); } lnt query(int v, int s, int t, lnt x) { if (s == t) return 1LL*s*x; if (x <= tr[v<<1].v) return query(v<<1, s, mid, x); return tr[v<<1].p+query(v<<1|1, mid+1, t, x-tr[v<<1].v); } void bi_solve(int l, int r, int s, int t) { if (s > t) return; if (s == t) { for (int i = l; i <= r; i++) ans[q[i].id] = a[s].d; return; } int lsz = 0; for (int i = s; i <= mid; i++) modify(1, 1, MAX_N, a[i].p, a[i].v); for (int i = l; i <= r; i++) if (tr[1].v < q[i].v) mrk[i] = false; else { lnt tot = query(1, 1, MAX_N, q[i].v); mrk[i] = q[i].w >= tot, lsz += mrk[i]; } for (int i = l, p1 = l, p2 = l+lsz; i <= r; i++) if (mrk[i]) tq[p1++] = q[i]; else tq[p2++] = q[i]; for (int i = l; i <= r; i++) q[i] = tq[i]; bi_solve(l+lsz, r, mid+1, t); for (int i = s; i <= mid; i++) modify(1, 1, MAX_N, a[i].p, -a[i].v); bi_solve(l, l+lsz-1, s, mid); } int main() { read(n), read(m); for (int i = 1; i <= n; i++) read(a[i].d), read(a[i].p), read(a[i].v); for (int i = 1; i <= m; i++) q[i].id = i, read(q[i].w), read(q[i].v); sort(a+1, a+n+1, cmpd), a[n+1].d = -1; bi_solve(1, m, 1, n+1); for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
|