标签:images struct 技术 har sts pac not tree ring
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<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<string> #include<cmath> #include<ctime> #include<cctype> using namespace std; const int N=100010; int rank[N],root[N]; int n,m,tot; struct node { int x,id; }a[N]; inline bool operator<(node a,node b) { return a.x<b.x; } struct tree { int l; int r; int sum; }tr[N*20]; void build(int num,int &x,int l,int r) { tr[tot++]=tr[x],x=tot-1; tr[x].sum++; if(l==r) return; int mid=(l+r)/2; if(num<=mid) build(num,tr[x].l,l,mid); else build(num,tr[x].r,mid+1,r); } int query(int i,int j,int k,int l,int r) { if(l==r) return l; int mid=(l+r)/2; int T=tr[tr[j].l].sum-tr[tr[i].l].sum; if(k<=T) return query(tr[i].l,tr[j].l,k,l,mid); else return query(tr[i].r,tr[j].r,k-T,mid+1,r); } int main() { //freopen("a.in","r",stdin); int d,b,c; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) { scanf("%d",&a[i].x); a[i].id=i; } sort(a+1,a+n+1); for(int i=1;i<=n;i++) rank[a[i].id]=i; tot=1; for(int i=1;i<=n;i++) { root[i]=root[i-1]; build(rank[i],root[i],1,n); } while(m--) { scanf("%d%d%d",&d,&b,&c); cout<<a[query(root[d-1],root[b],c,1,n)].x<<endl; } } return 0; }
标签:images struct 技术 har sts pac not tree ring
原文地址:http://www.cnblogs.com/AseanA/p/7156269.html