HDU 4417 Super Mario(主席树)

来源:互联网 发布:牧原股份 知乎 编辑:程序博客网 时间:2024/05/14 17:02

Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6955    Accepted Submission(s): 2997


Problem Description
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.
 

Input
The 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.)
 

Output
For 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 Input
110 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
 

Source
2012 ACM/ICPC Asia Regional Hangzhou Online
 

Recommend
liuyiding
 

题意: 给出n个数m个问题让求出区间[l,r]内比 k小的有多少个。


     分析:直接上主席树,查询的时候类似于线段树区间和。


AC代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAX 100010int a[MAX],sorta[MAX];int root[MAX<<5];int cnt;struct Tree{int sum,lson,rson;}tree[MAX<<5];void bulid(int &v,int l,int r){v=++cnt;tree[v].sum=0;if(l==r)return;int mid=(l+r)>>1;bulid(tree[v].lson,l,mid);bulid(tree[v].rson,mid+1,r);}int Binary_search(int *p,int l,int r,int aim){while(l<=r){int mid=(l+r)>>1;if(p[mid]<=aim)l=mid+1;elser=mid-1;}return r;}void update(int p,int &v,int l,int r,int s){v=++cnt;tree[v]=tree[p];tree[v].sum++;if(l==r)return;int mid=(l+r)>>1;if(mid<s)update(tree[p].rson,tree[v].rson,mid+1,r,s);elseupdate(tree[p].lson,tree[v].lson,l,mid,s);}int query(int p,int v,int l,int r,int k){if(k<0)return 0;if(k>=r)return tree[v].sum-tree[p].sum;int mid=(l+r)>>1,ret=0;if(k>=l)ret+=query(tree[p].lson,tree[v].lson,l,mid,k);if(k>mid)ret+=query(tree[p].rson,tree[v].rson,mid+1,r,k);return ret;}int main(){int T,cas=0;scanf("%d",&T);while(T--){printf("Case %d:\n",++cas);cnt=0;int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d",&a[i]);sorta[i]=a[i];}sort(sorta+1,sorta+n+1);int t=1;for(int i=2;i<=n;i++)if(sorta[i]!=sorta[i-1])sorta[++t]=sorta[i];bulid(root[0],1,t);for(int i=1;i<=n;i++){int s=Binary_search(sorta,1,t,a[i]);update(root[i-1],root[i],1,t,s);}while(m--){int l,r,k;scanf("%d%d%d",&l,&r,&k);l++,r++;int y=Binary_search(sorta,1,t,k);printf("%d\n",query(root[l-1],root[r],1,t,y));}}}



原创粉丝点击