TLE on #11 求条
查看原帖
TLE on #11 求条
941431
Autream楼主2024/5/7 16:19
// Problem: 最大异或和
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4735
// Memory Limit: 512 MB
// Time Limit: 1500 ms
// Author: Li_Feiy
#include<bits/stdc++.h>
#define arrout(a,n) rep(i,1,n)std::cout<<a[i]<<" "
#define arrin(a,n) rep(i,1,n)a[i]=read()
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define dep(i,x,n) for(int i=x;i>=n;i--)
#define erg(i,x) for(int i=head[x];i;i=e[i].nex)
#define dbg(x) std::cout<<#x<<":"<<x<<" "
#define mem(a,x) memset(a,x,sizeof a)
#define all(x) x.begin(),x.end()
#define arrall(a,n) a+1,a+1+n
#define PII std::pair<int,int>
#define m_p std::make_pair
#define u_b upper_bound
#define l_b lower_bound
#define p_b push_back
#define CD const double
#define CI const int
#define int long long
#define il inline
#define ss second
#define ff first
#define itn int
int read() {
    char ch=getchar();
    int r=0,w=1;
    while(ch<'0'||ch>'9') w=ch=='-'?-1:w,ch=getchar();
    while(ch>='0'&&ch<='9') r=r*10+ch-'0',ch=getchar();
    return r*w;
}
CI N=6e5+5;
int n,m,tot,a[N],s[N],tag[N*25],root[N],nex[N*25][2];
void insert(int id,int lst,int now) {
    dep(i,25,0) {
        int tmp=(s[id]>>i)&1;
        if(nex[lst][0]) {
            nex[now][0]=nex[lst][0];
        }
        if(nex[lst][1]) {
            nex[now][1]=nex[lst][1];
        }
        nex[now][tmp]=++tot;
        tag[tot]=id;
        lst=nex[lst][tmp];
        now=nex[now][tmp];
    }
}
int query(int l,int r,int val) {
    int ans=0,now=root[r];
    dep(i,25,0) {
        int tmp=(val>>i)&1;
        if(tag[nex[now][!tmp]]>=l) {
            ans+=1<<i;
            now=nex[now][!tmp];
        } else {
            now=nex[now][tmp];
        }
    }
    return ans;
}
signed main() {
    n=read(),m=read();
    root[0]=++tot;
    tag[0]=-1;
    insert(0,0,root[0]);
    arrin(a,n);
    rep(i,1,n) {
        s[i]=s[i-1]^a[i];
        root[i]=++tot;
        insert(i,root[i-1],root[i]);
    }
    while(m--) {
        char op=getchar();
        if(op=='A') {
            int x=read();
            root[++n]=++tot;
            s[n]=s[n-1]^x;
            insert(n,root[n-1],root[n]);
        } else {
            int l=read(),r=read(),x=read();
            std::cout<<query(l-1,r-1,s[n]^x)<<"\n";
        }
    }
    return 0;
}

记录

2024/5/7 16:19
加载中...