标签:des style io color ar os java for sp
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 <iostream> #include <algorithm> #include <cstdio> using namespace std; const int maxn = 100050; struct node { int l,r,len; int *sub; } a[maxn*4]; int n,m,b[maxn]; void build(int l,int r,int k) { a[k].l=l,a[k].r=r; a[k].sub = new int[r-l+3]; int cnt=0; for(int i=l; i<=r; i++) a[k].sub[cnt++]=b[i]; a[k].len=cnt; sort(a[k].sub,a[k].sub+cnt); if(l!=r) { int mid=(l+r)>>1; build(l,mid,2*k); build(mid+1,r,2*k+1); } } int query(int l,int r,int h,int k) { if(a[k].l==l && a[k].r==r) { int num=upper_bound(a[k].sub,a[k].sub+a[k].len,h)-a[k].sub; return num; } else { int mid=(a[k].l+a[k].r)>>1; if(r<=mid) return query(l,r,h,2*k); else if(l>mid) return query(l,r,h,2*k+1); else return query(l,mid,h,2*k)+query(mid+1,r,h,2*k+1); } } void Delete(int l,int r,int k) { delete[] a[k].sub; if(l!=r) { int mid=(l+r)>>1; Delete(l,mid,2*k); Delete(mid+1,r,2*k+1); } } int main() { int T,x,y,h; scanf("%d",&T); for(int co=1; co<=T; co++) { scanf("%d %d",&n,&m); for(int i=1; i<=n; i++) scanf("%d",&b[i]); build(1,n,1); printf("Case %d:\n",co); for(int i=1; i<=m; i++) { scanf("%d %d %d",&x,&y,&h); printf("%d\n",query(x+1,y+1,h,1)); } Delete(1,n,1); } return 0; }
hdu 4417 Super Mario (线段树+动态数组)
标签:des style io color ar os java for sp
原文地址:http://blog.csdn.net/u012596172/article/details/40783145