标签:blog style enter represent nbsp lsp include typedef code
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 52651 | Accepted: 18091 | |
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 <cstring> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #define met(a,b) memset(a,b,sizeof a) #define pb push_back #define lson(x) ((x<<1)) #define rson(x) ((x<<1)+1) using namespace std; typedef long long ll; const int N=1e5+50; const int M=N*N+10; struct P_Tree{ int n,order[N]; int valu[20][N],num[20][N]; ll sum[N],lsum[20][N],isum; void build(int l,int r,int ind){ if(l==r)return; int mid=(l+r)/2; int same=mid-l+1,ln=l,rn=mid+1; for(int i=l;i<=r;i++){ int flag=0; if((valu[ind][i]<order[mid])||(valu[ind][i]==order[mid]&&same)){ flag=1; valu[ind+1][ln++]=valu[ind][i]; lsum[ind][i]=lsum[ind][i-1]+valu[ind][i]; if(valu[ind][i]==order[mid])same--; } else { valu[ind+1][rn++]=valu[ind][i]; lsum[ind][i]=lsum[ind][i-1]+valu[ind][i]; } num[ind][i]=num[ind][i-1]+flag; } build(l,mid,ind+1); build(mid+1,r,ind+1); } void init(int len){ n=len;sum[0]=0; for(int i=0;i<20;i++)valu[i][0]=num[i][0]=lsum[i][0]=0; for(int i=1;i<=n;i++){ scanf("%d",&order[i]); valu[0][i]=order[i]; sum[i]=sum[i-1]+order[i]; } sort(order+1,order+n+1); build(1,n,0); } int query(int s,int t,int k,int l,int r,int ind){ if(l==r)return valu[ind][l]; int mid=(l+r)/2; int lx=num[ind][s-1]-num[ind][l-1]; int ly=num[ind][t]-num[ind][s-1]; int rx=s-1-l+1-lx; int ry=t-s+1-ly; if(ly>=k)return query(l+lx,l+lx+ly-1,k,l,mid,ind+1); else { isum+=lsum[ind][t]-lsum[ind][s-1]; s=mid+1+rx; t=mid+1+rx+ry-1; return query(s,t,k-ly,mid+1,r,ind+1); } } }tree; int main() { int n,m; while(~scanf("%d%d",&n,&m)){ tree.init(n); while(m--){ int u,v,w; scanf("%d%d%d",&u,&v,&w); printf("%d\n",tree.query(u,v,w,1,n,0)); } } }
标签:blog style enter represent nbsp lsp include typedef code
原文地址:http://www.cnblogs.com/jianrenfang/p/6363393.html