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 78 79 80 81 82 83 84 85
| #include<bits/stdc++.h> #define int long long using namespace std;
int read(); #define r1 read()
const int maxn = 2e5+10, N = 1; int n, m, k; int v[maxn]; int b[maxn]; int root[maxn]; struct Node{ int sum,l,r; }tree[maxn * 20];
int tot = 0; void update(int last,int &now,int l,int r,int x) { tree[++tot] = tree[last]; now = tot; tree[now].sum++; if(l == r) return; int mid = (l + r) >> 1; if(x <= mid) { update(tree[last].l,tree[now].l,l,mid,x); } else { update(tree[last].r,tree[now].r,mid + 1,r,x); } } int query(int x,int y,int l,int r,int k) { if(l == r) return l; int mid = (l + r) >> 1; int sum = tree[tree[y].l].sum - tree[tree[x].l].sum; if(sum >= k) { return query(tree[x].l,tree[y].l,l,mid,k); } else { return query(tree[x].r,tree[y].r,mid + 1,r,k-sum); } } int build(int l, int r) { int pos = ++tot; tree[pos].sum = 0; if(l == r) return l; int mid = (l + r) >> 1; tree[pos].l = build(l,mid); tree[pos].r = build(mid + 1,r); return pos; } signed main() { int i, j; n = r1;m = r1; for(i = 1;i <= n; i++) { v[i] = r1; b[i] = v[i]; } sort(b+1,b+n+1); int len = unique(b+1,b+n+1) - (b+1); root[0] = build(1,len); for(i = 1;i <= n;i++) { int x = lower_bound(b+1,b+len+1,v[i])-b; update(root[i-1],root[i],1,len,x); } for(i = 1; i <= m; i ++) { int l,r,k; l = r1,r = r1,k = r1; int id = query(root[l - 1],root[r],1,len,k); printf("%d\n",b[id]); } return 0; } int read() { char c = getchar(); int x = 0,f = 1; for(; !isdigit(c);c = getchar()) if(c == '-') f = -1; for(; isdigit(c);c = getchar()) x = (x << 1) + (x << 3) + (c ^48); return f * x; }
|