zoukankan      html  css  js  c++  java
  • codechef XRQRS


    给定一个初始时为空的整数序列(元素由1开始标号)以及一些询问:
    类型1:在数组后面就加入数字x。
    类型2:在区间L…R中找到y,最大化(x xor y)。
    类型3:删除数组最后K个元素。
    类型4:在区间L…R中,统计小于等于x的元素个数。
    类型5:在区间L…R中,找到第k小的数。
    Input
    输入数据第一行为一个整数q,表示询问个数,接下来q行,每行一条询问 对应题目描述。
    类型1的询问格式为“1 x”。
    类型2的询问格式为“2 L R x”。
    类型3的询问格式为“3 k”。
    类型4的询问格式为“4 L R x”。
    类型5的询问格式为“5 L R k”。
    Output
    对于每个2、4、5询问输出一行对应答案
    Sample Input
    10
    1 8
    5 1 1 1
    1 2
    2 2 2 7
    2 2 2 7
    1 1
    4 2 2 2
    2 1 2 3
    4 1 3 5
    1 6
    Sample Output
    8
    2
    2
    1
    8
    2
    HINT
    令N表示每次询问前数组中元素的个数
    1<=L<=R<=N
    1<=x<=500,000
    对于第三类询问 1<=k<=N
    对于第五类询问 k<=R-L+1
    1<=N<=500,000

    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
     
    inline char nc()
    {
        static char buf[100000],*p1=buf,*p2=buf;
        if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
        return *p1++;
    }
     
    inline void read(int &x)
    {
        char c=nc(),b=1;
        for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
        for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
    }
     
    const int N=500005;
    const int K=20;
    const int M=10921000;
     
    int ncnt;
    int root[N];
    int ch[M][2],sum[M];
     
    inline void insert(int &rt,int x,int t)
    {
    	
        rt=++ncnt; 
        int y; sum[y=rt]=sum[x]+1;
        for (int i=K;~i;i--)
        {
            if ((t>>i)&1)
                ch[y][0]=ch[x][0],y=(ch[y][1]=++ncnt),x=ch[x][1];
            else
                ch[y][1]=ch[x][1],y=(ch[y][0]=++ncnt),x=ch[x][0];
            sum[y]=sum[x]+1;
        }
    }
     
    inline int Max(int l,int r,int t)
    {
        int ret=0,tmp;
        for (int i=K;~i;i--)
        {
            tmp=(t>>i)&1;
            if (sum[ch[r][tmp^1]]-sum[ch[l][tmp^1]])
                ret+=((tmp^1)<<i),r=ch[r][tmp^1],l=ch[l][tmp^1];
            else
                ret+=(tmp<<i),r=ch[r][tmp],l=ch[l][tmp];
        }
        return ret;
    }
     
    inline int Count(int l,int r,int t)
    {
        int ret=0,tmp;
        for (int i=K;~i;i--)
        {
            tmp=(t>>i)&1;
            if (tmp)
                ret+=sum[ch[r][0]]-sum[ch[l][0]],l=ch[l][1],r=ch[r][1];
            else
                l=ch[l][0],r=ch[r][0];
        }
        ret+=sum[r]-sum[l];
        return ret;
    }
     
    inline int Kth(int l,int r,int k)
    {
        int ret=0;
        for (int i=K;~i;i--)
            if (k>sum[ch[r][0]]-sum[ch[l][0]])
                k-=sum[ch[r][0]]-sum[ch[l][0]],ret+=(1<<i),l=ch[l][1],r=ch[r][1];
            else
                l=ch[l][0],r=ch[r][0];
        return ret;
    }
     
    int pnt;
     
    int main()
    {
        int Q,order,l,r,x;
        read(Q);
        while (Q--)
        {
            read(order); 
            if (order==1)
            //在数组后面就加入数字x
            {
                read(x); 
    			pnt++;
                insert(root[pnt],root[pnt-1],x);
            }
            else if (order==2)
            //在区间L…R中找到y,最大化(x xor y)
            {
                read(l); read(r); read(x);
                printf("%d
    ",Max(root[l-1],root[r],x));
            }
            else if (order==3)
            //删除数组最后K个元素
            {
                read(x);
                pnt-=x;
            }
            else if (order==4)
            //在区间L…R中,统计小于等于x的元素个数
            {
                read(l); read(r); read(x);
                printf("%d
    ",Count(root[l-1],root[r],x));
            }
            else if (order==5)
            //在区间L…R中,找到第k小的数
            {
                read(l); read(r); read(x);
                printf("%d
    ",Kth(root[l-1],root[r],x));
            }
        }
        return 0;
    }
    ?
    

      

  • 相关阅读:
    BNU 33693——Problemsetting——————【枚举+最大流】
    HDU 5656 ——CA Loves GCD——————【dp】
    HZAU 21——Arithmetic Sequence——————【暴力 or dp】
    HZAU 18——Array C——————【贪心】
    BNU 20950 ——沉重的货物 —————— · 最短路、最短边最大化」
    vim创建新的命令
    vim 配置文件——部分配置
    nyoj 1239——引水工程——————【最小生成树 prim】
    内部排序 ——第3波——————【快速排序】
    PostgreSQL 安装配置 (亲测可用)
  • 原文地址:https://www.cnblogs.com/cutemush/p/12728801.html
Copyright © 2011-2022 走看看