Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 57543 | Accepted: 19893 | |
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
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> #include<set> using namespace std; #define PI acos(-1.0) typedef long long ll; typedef pair<int,int> P; const int maxn=5e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7; const ll INF=1e13+7; inline int get_int() { int num=0; char ch; while((ch=getchar())!=‘ ‘&&ch!=‘\n‘) num=num*10+(ch-‘0‘); return num; } /****************************/ struct edge { int from,to; int cost; }; edge es[maxm]; struct node { int num; int k; }; node sign[maxn]; int a[maxn]; int cmp(node x,node y) { return x.num<y.num; } vector<node>tree[maxn]; void build(int l,int r,int pos) { if(l==r) return; int mid=(l+r)/2; for(int i=0; i<tree[pos].size(); i++) { int k=tree[pos][i].k; if(l<=k&&k<=mid) tree[pos<<1].push_back(tree[pos][i]); else tree[pos<<1|1].push_back(tree[pos][i]); } build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); } int query(int L,int R,int w,int l,int r,int pos) { ///cout<<l<<" "<<r<<endl; if(L<=l&&r<=R) { int s=0,e=tree[pos].size()-1; int cou=-1; while(s<=e) { int md=(s+e)/2; if(tree[pos][md].num<=w) s=md+1,cou=md; else e=md-1; } ///cout<<cou+1<<endl; return cou+1; } int mid=(l+r)/2; int ans=0; if(L<=mid) ans+=query(L,R,w,l,mid,pos<<1); if(R>mid) ans+=query(L,R,w,mid+1,r,pos<<1|1); return ans; } int main() { int n,q; scanf("%d%d",&n,&q); for(int i=0; i<n; i++) { scanf("%d",&a[i]); sign[i].num=a[i],sign[i].k=i+1; } sort(sign,sign+n,cmp); for(int i=0; i<n; i++) tree[1].push_back(sign[i]); build(1,n,1); while(q--) { int l,r,k; scanf("%d%d%d",&l,&r,&k); int L=0,R=n-1; int ans=-1; while(L<=R) { int mid=(L+R)/2; int w=sign[mid].num; ///cout<<"x="<<w<<endl; if((query(l,r,w,1,n,1))>=k) R=mid-1,ans=mid; else L=mid+1; } cout<<sign[ans].num<<endl; } return 0; }
POJ 2014.K-th Number 区间第k大 (归并树)
原文:http://www.cnblogs.com/GeekZRF/p/7152755.html