Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int MAXN = 1E5+100; struct Query { int l,r; int h; int id; }Q[MAXN]; pair<int,int>p[MAXN]; int F[MAXN]; int n,m; void update(int x,int val) { while(x<=n) { F[x]+=val; x+=x&-x; } } int query(int x) { int res=0; while(x>0) { res+=F[x]; x-=x&-x; } return res; } bool cmp(const Query& aa,const Query& bb) { return aa.h<bb.h; } int ans[MAXN]; int main() { int T; int iCase=0; scanf("%d",&T); while(T--) { iCase++; scanf("%d%d",&n,&m); memset(F,0,sizeof(F)); for(int i=1;i<=n;i++) { scanf("%d",&p[i].first); p[i].second=i; } for(int i=1;i<=m;i++) { scanf("%d%d%d",&Q[i].l,&Q[i].r,&Q[i].h); Q[i].id=i,Q[i].l++,Q[i].r++; } sort(Q+1,Q+m+1,cmp); sort(p+1,p+n+1); int i=1,j=1; while(j<=m) { while(i<=n) { if(p[i].first>Q[j].h) break; update(p[i].second,1); i++; } while(j<=m) { if(i<=n&&Q[j].h>=p[i].first) break; ans[Q[j].id]=query(Q[j].r)-query(Q[j].l-1); j++; } } printf("Case %d:\n",iCase); for(int i=1;i<=m;i++) printf("%d\n",ans[i]); } return 0; }
HDU 4417 Super Mario (树状数组/线段树)
原文:http://www.cnblogs.com/wangdongkai/p/5745119.html