Luogu P4735(可持久化字典树)

链接:点击打开链接

题意:

给定一个非负整数序列 {a},初始长度为n。

有 m 个操作,有以下两种操作类型:

A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 n+1。
Q l r x:询问操作,你需要找到一个位置 p,满足l \le p \le r,使得: a[p] \oplus a[p+1] \oplus ... \oplus a[N] \oplus x 最大,输出最大是多少。

代码:

#include<bits/stdc++.h>
using namespace std;

const int siz=70*300005;
int num_tr,node;
int rk[siz],a[siz],tr[siz][2];
void in(int x){
    int i,op,rt;
    rt=rk[num_tr];
    rk[++num_tr]=++node;
    for(i=24;i>=0;i--){
        op=(x>>i)&1;
        a[node]=a[rt]+1;
        tr[node][op]=node+1;
        tr[node][!op]=tr[rt][!op];
        rt=tr[rt][op];
        ++node;
    }
    a[node]=a[rt]+1;
}
int cal(int l,int r,int x){
    int i,op,ans=0;
    for(i=24;i>=0;i--){
        op=(x>>i)&1;
        if(a[tr[r][!op]]-a[tr[l][!op]]>0){
            ans=(ans<<1)|1;
            l=tr[l][!op];
            r=tr[r][!op];
        }
        else{
            ans<<=1;
            l=tr[l][op];
            r=tr[r][op];
        }
    }
    return ans;
}                                               
int main(){                                     //可持久化字典树,跟主席树其实是一个意思
    char c;                                     //原问题转换一下其实就是n个数中找一个与
    int n,m,i,j,u,v,w,sum;                      //已知的数异或的最大值,直接在trie树上贪心
    scanf("%d%d",&n,&m);                        
    sum=num_tr=node=0;
    in(0);
    for(i=1;i<=n;i++){
        scanf("%d",&u);
        sum^=u;
        in(sum);
    }
    while(m--){
        getchar();
        scanf("%c",&c);
        if(c=='A'){
            scanf("%d",&u);
            sum^=u;
            in(sum);
        }
        else{
            scanf("%d%d%d",&u,&v,&w);           //最后询问要注意一下是哪两颗树
            printf("%d\n",cal(rk[u-1],rk[v],sum^w));
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值