Super Mario
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.
InputThe first line follows an integer T, the number of test data.
For each test data: The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries. Next line contains n integers, the height of each brick, the range is [0, 1000000000]. Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)OutputFor each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query. Sample Input110 100 5 2 7 5 4 3 8 7 7 2 8 63 5 01 3 11 9 40 1 03 5 55 5 14 6 31 5 75 7 3
Sample Output
Case 1:4003120151
题意:求区间[l-r]的<=k的个数
裸的主席树,离散化一下就行了。
#includeusing namespace std;#define Debug(x) cout<<#x<<":"<<(x)< >1; build(l,m,tree[x].l); build(m+1,r,tree[x].r); pushup(x);}int query(int l,int r,int pre,int now,int x,int y){ if(x<=l&&r<=y) return tree[now].num-tree[pre].num; int m=(l+r)>>1; int ans=0; if(x<=m) ans+=query(l,m,tree[pre].l,tree[now].l,x,y); if(y>m) ans+=query(m+1,r,tree[pre].r,tree[now].r,x,y); return ans;}void updata(int l,int r,int pre,int &now,int k){ now=++cnt; tree[now]=tree[pre]; if(l==r) { tree[now].num++; return ; } int m=(l+r)>>1; if(k<=m) updata(l,m,tree[pre].l,tree[now].l,k); else updata(m+1,r,tree[pre].r,tree[now].r,k); pushup(now);}int main(){ int n,m,t,p=1; scanf("%d",&t); while(t--) { printf("Case %d:\n",p++); scanf("%d %d",&n,&m); { cnt=0; for(int i=1; i<=n; i++) scanf("%d",&a[i]),b[i]=a[i]; sort(b+1,b+1+n); int len=unique(b+1,b+1+n)-(b+1); for(int i=1; i<=n; i++) a[i]=lower_bound(b+1,b+1+len,a[i])-b; build(1,len,rt[0]); for(int i=1; i<=n; i++) updata(1,len,rt[i-1],rt[i],a[i]); while(m--) { int l,r,k1,k; scanf("%d %d %d",&l,&r,&k); l++; r++; k1=lower_bound(b+1,b+1+len,k)-b; if(b[k1]!=k) k1--; if(k1==0)//不判断会出现MLE { printf("0\n"); continue; } printf("%d\n",query(1,len,rt[l-1],rt[r],1,k1)); } } }}
PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~