1 10 10 0 5 2 7 5 4 3 8 7 7 2 8 6 3 5 0 1 3 1 1 9 4 0 1 0 3 5 5 5 5 1 4 6 3 1 5 7 5 7 3
Case 1: 4 0 0 3 1 2 0 1 5 1
#include<algorithm> #include<iostream> #include<string.h> #include<stdio.h> using namespace std; const int INF=0x3f3f3f3f; const int maxn=100010; typedef long long ll; int seg[20][maxn],lnum[20][maxn],sa[maxn]; int n,m; void btree(int L,int R,int d) { int i,ls,rs,lm,mid; if(L==R) return ; mid=(L+R)>>1; ls=L,rs=mid+1; lm=mid-L+1; for(i=L;i<=R;i++) if(seg[d][i]<sa[mid]) lm--; for(i=L;i<=R;i++) { lnum[d][i]=(i==L)?0:lnum[d][i-1]; if(seg[d][i]==sa[mid]) { if(lm>0) { lm--; lnum[d][i]++; seg[d+1][ls++]=seg[d][i]; } else seg[d+1][rs++]=seg[d][i]; } else if(seg[d][i]<sa[mid]) { lnum[d][i]++; seg[d+1][ls++]=seg[d][i]; } else seg[d+1][rs++]=seg[d][i]; } btree(L,mid,d+1); btree(mid+1,R,d+1); } int qu(int L,int R,int l,int r,int d,int k) { int ss,s,bb,b,mid; if(L==R) return seg[d][L]; ss=(l==L)?0:lnum[d][l-1]; s=lnum[d][r]-ss; mid=(L+R)>>1; if(s>=k) return qu(L,mid,L+ss,L+ss+s-1,d+1,k); else { bb=l-L-ss; b=r-l+1-s; return qu(mid+1,R,mid+bb+1,mid+bb+b,d+1,k-s); } } void init() { int i; for(i=1;i<=n;i++) { scanf("%d",&seg[0][i]); sa[i]=seg[0][i]; } sort(sa+1,sa+n+1); btree(1,n,0); } int bin(int L,int R,int h) { int low=1,hi=R-L+1,mid,ans=0; while(low<=hi) { mid=(low+hi)>>1; if(qu(1,n,L,R,0,mid)<=h) ans=mid,low=mid+1; else hi=mid-1; } return ans; } int main() { int L,R,h,t,cas=1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(); printf("Case %d:\n",cas++); while(m--) { scanf("%d%d%d",&L,&R,&h); L++,R++; printf("%d\n",bin(L,R,h)); } } return 0; }
#include<algorithm> #include<iostream> #include<string.h> #include<stdio.h> using namespace std; const int INF=0x3f3f3f3f; const int maxn=100010; typedef long long ll; int C[maxn],ans[maxn],n,m; struct qnode { int l,r,h,id; inline bool operator <(const qnode &tt) const { return h<tt.h; } } qu[maxn]; struct node { int p,h; inline bool operator <(const node &tt) const { return h<tt.h; } } bk[maxn]; void update(int x,int d) { for(int i=x;i<=n;i+=i&-i) C[i]+=d; } int getsum(int x) { int sum=0; for(int i=x;i>0;i-=i&-i) sum+=C[i]; return sum; } int main() { int t,cas=1,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); C[n]=0; for(i=0;i<n;i++) { C[i]=0,bk[i].p=i+1; scanf("%d",&bk[i].h); } for(i=0;i<m;i++) { scanf("%d%d%d",&qu[i].l,&qu[i].r,&qu[i].h); qu[i].l++,qu[i].r++,qu[i].id=i; } sort(bk,bk+n); sort(qu,qu+m); for(i=j=0;i<m;i++) { for(;j<n&&bk[j].h<=qu[i].h;j++) update(bk[j].p,1); ans[qu[i].id]=getsum(qu[i].r)-getsum(qu[i].l-1); } printf("Case %d:\n",cas++); for(i=0;i<m;i++) printf("%d\n",ans[i]); } return 0; }
hdu 4417 Super Mario(离线树状数组|划分树)
原文地址:http://blog.csdn.net/bossup/article/details/39585115