HDU

来源:互联网 发布:数据库删除语句delete 编辑:程序博客网 时间:2024/05/14 08:53

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



解题思路:可持久化线段树模板题。与区间第k大一样道理!



#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<list>#include<stack>#include<iomanip>#include<cmath>#include<bitset>#define INF 0x3f3f3f3fconst int Max=30*100000;typedef long long int ll;using namespace std;int ls[Max];int rs[Max];int sum[Max];int tot=0;int root[Max];ll a[100100],b[100100];int n,m;void build(int &root,int l,int r){    root=++tot;    if(l==r) {        sum[root]=0;        ls[root]=root;        rs[root]=root;        return ;    }    int mid=(l+r)/2;    build(ls[root],l,mid);    build(rs[root],mid+1,r);    sum[root]=sum[ls[root]]+sum[rs[root]];}void update(int last,int &root,int l,int r,int x,int v){    root=++tot;    ls[root]=ls[last];    rs[root]=rs[last];    sum[root]=sum[last];    if(l==r) {        sum[root]+=v;        ls[root]=root;        rs[root]=root;        return ;    }    int mid=(l+r)/2;    if(x<=mid) update(ls[last],ls[root],l,mid,x,v);    else update(rs[last],rs[root],mid+1,r,x,v);    sum[root]=sum[ls[root]]+sum[rs[root]];}int query(int root,int l,int r,int x){    if(l==r){        return sum[root];    }    int mid=(l+r)/2;    if(x>mid) return sum[ls[root]]+query(rs[root],mid+1,r,x);    else return query(ls[root],l,mid,x);}int main(){    int T;    scanf("%d",&T);    for(int cas=1;cas<=T;cas++){        tot=0;        memset(root,0,sizeof(root));        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++) {            scanf("%I64d",&a[i]);            b[i]=a[i];        }        sort(b+1,b+n+1);        int cnt=unique(b+1,b+n+1)-(b+1);        b[++cnt]=INF;        build(root[0],1,cnt);        for(int i=1;i<=n;i++){            int x=lower_bound(b+1,b+cnt+1,a[i])-b;            update(root[i-1],root[i],1,cnt,x,1);        }        printf("Case %d:\n",cas);        for(int i=1;i<=m;i++){            int l,r;            ll v;            scanf("%d%d%I64d",&l,&r,&v);l++;r++;            int x=upper_bound(b+1,b+cnt+1,v)-b-1;            int R=0,L=0;            if(x>0){                L=query(root[l-1],1,cnt,x);                R=query(root[r],1,cnt,x);            }            printf("%d\n",R-L);        }    }    return 0;}





原创粉丝点击