标签:root ace nes getch memory stat i++ hash follow
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 57427 | Accepted: 19856 | |
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
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 const int MAXN=10000001; 9 void read(int &n) 10 { 11 char c=‘+‘;int x=0;bool flag=0; 12 while(c<‘0‘||c>‘9‘) 13 {c=getchar();if(c==‘-‘)flag=1;} 14 while(c>=‘0‘&&c<=‘9‘) 15 {x=x*10+c-48,c=getchar();} 16 flag==1?n=-x:n=x; 17 } 18 int n,m; 19 int a[MAXN]; 20 int hash[MAXN]; 21 int root[MAXN/2]; 22 int now,tot;// 所有节点的总出现次数 23 int ls[MAXN],rs[MAXN],cnt[MAXN]; 24 void build(int &cur,int l,int r) 25 { 26 cur=tot++;// 总的节点数量 27 cnt[cur]=0; 28 if(l!=r) 29 { 30 int mid=(l+r)/2; 31 build(ls[cur],l,mid); 32 build(rs[cur],mid+1,r); 33 } 34 } 35 void update(int pre,int pos,int &cur,int l,int r) 36 { 37 cur=tot++; 38 cnt[cur]=cnt[pre]+1; 39 ls[cur]=ls[pre];rs[cur]=rs[pre]; 40 if(l==r) 41 return ; 42 int mid=(l+r)/2; 43 if(pos<=mid) 44 update(ls[pre],pos,ls[cur],l,mid); 45 else 46 update(rs[pre],pos,rs[cur],mid+1,r); 47 } 48 int query(int lt,int rt,int l,int r,int k) 49 { 50 if(l==r) 51 return l; 52 int now=cnt[ls[rt]]-cnt[ls[lt]]; 53 int mid=(l+r)/2; 54 if(k<=now) 55 return query(ls[lt],ls[rt],l,mid,k); 56 else 57 return query(rs[lt],rs[rt],mid+1,r,k-now); 58 } 59 int main() 60 { 61 while(scanf("%d%d",&n,&m)==2) 62 { 63 for(int i=1;i<=n;i++) 64 { 65 read(a[i]); 66 hash[i]=a[i]; 67 } 68 sort(hash+1,hash+n+1); 69 int size=unique(hash+1,hash+n+1)-hash-1; 70 for(int i=1;i<=n;i++) 71 a[i]=lower_bound(hash+1,hash+size+1,a[i])-hash;// 排序+离散化 72 73 tot=0; 74 build(root[0],1,size); 75 76 for(int i=1;i<=n;i++) 77 update(root[i-1],a[i],root[i],1,size);// 建出所有的树 78 79 for(int i=1;i<=m;i++) 80 { 81 int x,y,z; 82 read(x);read(y);read(z); 83 printf("%d\n",hash[query(root[x-1],root[y],1,size,z)]); 84 } // 查询 85 } 86 87 return 0; 88 }
标签:root ace nes getch memory stat i++ hash follow
原文地址:http://www.cnblogs.com/zwfymqz/p/7137467.html