Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 29149 | Accepted: 8799 | |
Case Time Limit: 2000MS |
Description
Input
Output
Sample Input
7 3 1 5 2 6 3 7 4 2 5 3 4 4 1 1 7 3
Sample Output
5 6 3
Hint
Source
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #define lson l,m,lv+1 #define rson m+1,r,lv+1 using namespace std; #define N 100002 struct node { int num; int v; }; int rc[N]; node st[20][N]; void build(int l,int r,int lv) { if(l==r) return ; int m=(l+r)>>1; int md=rc[m],ll=l,rr=m+1; for(int i=l;i<=r;i++) if(st[lv][i].v<=md) { i==l?st[lv][i].num=1:st[lv][i].num=st[lv][i-1].num+1; st[lv+1][ll++].v=st[lv][i].v; } else { i==l?st[lv][i].num=0:st[lv][i].num=st[lv][i-1].num; st[lv+1][rr++].v=st[lv][i].v; } build(lson); build(rson); } void query(int ql,int qr,int qk,int l,int r,int lv) { if(l==r) { printf("%d\n",st[lv][l].v); return ; } int m=(l+r)>>1; int md=rc[m],lva=st[lv][l+ql-1].num,rva=st[lv][l+qr-1].num; int num=rva-lva; if(st[lv][l+ql-1].v<=md) num++; if(num>=qk) { if(st[lv][l+ql-1].v<=md) query(lva,rva,qk,lson); else query(lva+1,rva,qk,lson); } else { lva=ql-lva; if(st[lv][l+ql-1].v<=md) lva++; rva=qr-rva; query(lva,rva,qk-num,rson); } } int main() { int n,m; int l,r,k; while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) { scanf("%d",rc+i);st[0][i].v=rc[i];} sort(rc+1,rc+n+1); build(1,n,0); while(m--) { scanf("%d %d %d",&l,&r,&k); query(l,r,k,1,n,0); } } return 0; }