zoukankan      html  css  js  c++  java
  • hdu5737(2016多校联赛第2场D)

    题意:给2组数据a和b数组,每次有2种操作:(+,l,r,x)把a数组第l个到第r个元素全置为x,(?,l,r)查询[l,r]之间哪些位置满足a[i]>=b[i](i>=l && i<=r)并把这些位置的数量统计

    一直想很久,没想到什么有效的方案,直到看到题解才明白过来,原来线段树套平衡树还有这种情况:里面其实不是平衡树,只是有序表。

    然后这题就转换为区间查找数对应排名

    由于此题不用对2个数组都修改,其中1个b树可作为固定的线段树套有序表以节省时间,另外1个表a树则单纯使用线段树的方法先修改,再更新对应b树结点的排名

    这里查找排名如果全部logn查找会因为常数太大直接卡,注意每个结点都含有序表并且上下有包含关系

    那咱们可以在b树自底向上更新父结点排名对应左右子树里的排名,用归并排序的方法,占用空间才o(nlogn),时间也是o(nlogn)

    顺带把会改变的a树1个个结点查询b树查出排名,修改时先查出根结点对应位置,再根据位置子树表一边向下更新一边转移到子树对应位置

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<queue>
    #include<stack>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<stdlib.h>
    #include<cmath>
    #include<string>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    typedef __int64 ll;
    int max(int a,int b){return a>b?a:b;}
    int min(int a,int b){return a<b?a:b;}
    int cnt;
    const int N=100010,M=262150,E=1768950;
    int n,m,i,a[N],b[N],x,l,r;
    int st[M],en[M],v[M],tag[M],pl[E],pr[E],pool[E],cur;
    ll ans,sum;
    
    void build(int x,int l,int r)
    {
        tag[x]=-1;
        if(l==r)
        {
            st[x]=cur+1;
            pool[++cur]=b[l];
            en[x]=cur;
            v[x]=(a[l]>=b[l]);
            return;
        }
        int mid=((l+r)>>1);
        build(x<<1,l,mid);
        build((x<<1)|1,mid+1,r);
        v[x]=v[x<<1]+v[(x<<1)|1];
        int al=st[x<<1],ar=en[x<<1],bl=st[(x<<1)|1],br=en[(x<<1)|1];
        st[x]=cur+1;
        while(al<=ar&&bl<=br)pool[++cur]=pool[al]<pool[bl]?pool[al++]:pool[bl++];
        while(al<=ar)pool[++cur]=pool[al++];
        while(bl<=br)pool[++cur]=pool[bl++];
        en[x]=cur;
        al=st[x<<1],bl=st[x<<1|1];
        for(int i=st[x];i<=cur;i++)
        {
            while(al<=ar&&pool[al]<=pool[i])al++;
            while(bl<=br&&pool[bl]<=pool[i])bl++;
            pl[i]=al-1,pr[i]=bl-1;
            if(pl[i]<st[x<<1])pl[i]=0;
            if(pr[i]<st[(x<<1)|1])pr[i]=0;
        }
    }
    
    inline void rankpt(int x,int p)
    {
        v[x]=(p?p-st[x]+1:0);
        tag[x]=p;
    }
    inline void pushdown(int x)
    {
        if(tag[x]<0)return;
        int p=tag[x];
        rankpt(x<<1,pl[p]);
        rankpt((x<<1)|1,pr[p]);
        tag[x]=-1;
    }
    
    void update(int x,int a,int b,int p)
    {
        if(l<=a && b<=r){rankpt(x,p);return;}
        pushdown(x);
        int mid=(a+b)>>1;
        if(l<=mid)update(x<<1,a,mid,pl[p]);
        if(r>mid)update((x<<1)|1,mid+1,b,pr[p]);
        v[x]=v[x<<1]+v[(x<<1)|1];
    }
    
    void query(int x,int a,int b)
    {
        if(l<=a && b<=r)
        {
            ans+=v[x];
            return;
        }
        pushdown(x);
        int mid=((a+b)>>1);
        if(l<=mid)query(x<<1,a,mid);
        if(r>mid)query((x<<1)|1,mid+1,b);
        v[x]=v[x<<1]+v[(x<<1)|1];
    }
    
    inline int lower(int x){
        //lower_bound(pool+st[1],pool+ed[1]+1,x);
        int l=st[1],r=en[1],mid,t=0;
        while(l<=r)
            if(pool[mid=(l+r)>>1]<=x)l=(t=mid)+1;
            else r=mid-1;
        return t;
    }
    
    int seeda, seedb, C = ~(1<<31), MM = (1<<16)-1;
    int rnd(int last) {
        seeda = (36969 + (last >> 3)) * (seeda & MM) + (seeda >> 16);
        seedb = (18000 + (last >> 3)) * (seedb & MM) + (seedb >> 16);
        return (C & ((seeda << 16) + seedb)) % 1000000000;
    }
    
    int main()
    {
        int t,ku;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d%d%d",&n,&m,&seeda,&seedb);
            for(i=1;i<=n;i++)scanf("%d",a+i);
            for(i=1;i<=n;i++)scanf("%d",b+i);
            ans=sum=cur=0;
            build(1,1,n);
            for(i=1;i<=m;i++)
            {
                l=rnd(ans)%n+1,r=rnd(ans)%n+1,ku=rnd(ans)+1;
                int kkk=lower(ku);
                if(l>r)l^=r^=l^=r;
                if((l+r+ku)&1)update(1,1,n,lower(ku));
                else
                {
                    ans=0;
                    query(1,1,n);
                    sum=(sum+(ll)i*ans)%1000000007;
                }
            }
            printf("%I64d
    ",sum);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    如何实现浏览器内多个标签页之间的通信?
    vue组件库的基本开发步骤(源代码)
    vue组件库的基本开发步骤
    Websocket原理
    TCP和UDP的区别
    一句话概括 tcp三次握手
    简单说一下你对http和https的理解
    .Ajax(async异步与sync同步)
    get和post请求方式的区别
    面试易忽略状态码
  • 原文地址:https://www.cnblogs.com/dgutfly/p/6014560.html
Copyright © 2011-2022 走看看