• HDU4417 Super Merio(可持久化线段树)


    马里奥是一个举世闻名的管道工,他的跳跃能力让我们钦佩。在一条长度为n的道路上,在每个整数点i的位置都有一个高度为hi的障碍物。现在的问题是:假设马里奥可以跳跃的最高高度为H,在道路的[L,R] 区间内他可以跳跃过的障碍物有多少个(不要考虑他被挡住)?

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+100;
    const int M=maxn*40;
    int _;
    int n,m,q;
    int a[maxn];
    int t[maxn];
    int T[maxn];
    int lson[M];
    int rson[M];
    int c[M];
    int tot;
    
    int build (int l,int r) {
        int root=tot++;
        c[root]=0;
        if (l!=r) {
            int mid=(l+r)>>1;
            lson[root]=build(l,mid);
            rson[root]=build(mid+1,r);
        }
        return root;
    }
    int up (int root,int p,int v) {
        int newRoot=tot++;
        int tmp=newRoot;
        int l=1,r=m;
        c[newRoot]=c[root]+v;
        while (l<r) {
            int mid=(l+r)>>1;
            if (p<=mid) {
                lson[newRoot]=tot++;
                rson[newRoot]=rson[root];
                newRoot=lson[newRoot];
                root=lson[root];
                r=mid;
            }
            else {
                rson[newRoot]=tot++;
                lson[newRoot]=lson[root];
                newRoot=rson[newRoot];
                root=rson[root];
                l=mid+1;
            }
            c[newRoot]=c[root]+v;
        }
        return tmp;
    }
    
    int query (int left_root,int right_root,int l,int r,int L,int R) {
        if (l>=L&&r<=R) return c[left_root]-c[right_root];
        int mid=(l+r)>>1;
        int ans=0;
        if (L<=mid) ans+=query(lson[left_root],lson[right_root],l,mid,L,R);
        if (R>mid) ans+=query(rson[left_root],rson[right_root],mid+1,r,L,R);
        return ans;
    }
    
    int main () {
        scanf("%d",&_);
        for (int k=1;k<=_;k++) {
            scanf("%d%d",&n,&q);
            for (int i=1;i<=n;i++) scanf("%d",a+i),t[i]=a[i];
            sort(t+1,t+n+1);
            m=unique(t+1,t+n+1)-t-1;
            for (int i=1;i<=n;i++) {
                a[i]=upper_bound(t+1,t+m+1,a[i])-t-1;
            }
            tot=0;
            T[n+1]=build(1,m);
            for (int i=n;i;i--) T[i]=up(T[i+1],a[i],1);
            printf("Case %d:
    ",k);
            for (int i=1;i<=q;i++) {
                int l,r,x;
                scanf("%d%d%d",&l,&r,&x);
                l++,r++;
                int y=upper_bound(t+1,t+m+1,x)-t-1;
                //printf("%d
    ",y);
                if (!y)
                    printf("0
    ");
                else
                    printf("%d
    ",query(T[l],T[r+1],1,m,1,y)); 
            }
        }
    }
  • 相关阅读:
    寒假学习第六天
    寒假学习第五天
    寒假学习第四天
    spark生态体系了解学习(六)
    spark生态体系了解学习(五)
    spark生态体系了解学习(四)
    spark生态体系了解学习(三)
    spark生态体系了解学习(二)
    spark生态体系了解学习(一)
    共享
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/14010276.html
Copyright © 2020-2023  润新知