zoukankan      html  css  js  c++  java
  • 【BZOJ3261】最大异或和(可持久化Trie)

    题意:

     

     思路:可持久化Trie板子题,支持序列插入和询问

      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 typedef long long ll;
      4 typedef unsigned int uint;
      5 typedef unsigned long long ull;
      6 typedef long double ld;
      7 typedef pair<int,int> PII;
      8 typedef pair<ll,ll> Pll;
      9 typedef vector<int> VI;
     10 typedef vector<PII> VII;
     11 typedef pair<ll,ll>P;
     12 #define N  12000010
     13 #define M  6000010
     14 #define INF 1e9
     15 #define fi first
     16 #define se second
     17 #define MP make_pair
     18 #define pb push_back
     19 #define pi acos(-1)
     20 #define mem(a,b) memset(a,b,sizeof(a))
     21 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
     22 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
     23 #define lowbit(x) x&(-x)
     24 #define Rand (rand()*(1<<16)+rand())
     25 #define id(x) ((x)<=B?(x):m-n/(x)+1)
     26 #define ls p<<1
     27 #define rs p<<1|1
     28 #define fors(i) for(auto i:e[x]) if(i!=p)
     29 
     30 const int MOD=1e8+7,inv2=(MOD+1)/2;
     31       int p=1e4+7;
     32       double eps=1e-8;
     33       int dx[4]={-1,1,0,0};
     34       int dy[4]={0,0,-1,1};
     35 
     36 char ch[10];
     37 int t[N][2],s[N],root[N],b[N],cnt;
     38 
     39 int read()
     40 {
     41    int v=0,f=1;
     42    char c=getchar();
     43    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
     44    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
     45    return v*f;
     46 }
     47 
     48 ll readll()
     49 {
     50    ll v=0,f=1;
     51    char c=getchar();
     52    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
     53    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
     54    return v*f;
     55 }
     56 
     57 void ins(int p1,int &p2,int z,int d)
     58 {
     59     int p=(z>>d)&1;
     60     p2=++cnt;
     61     s[p2]=s[p1]+1;
     62     if(d<0) return;
     63     t[p2][p^1]=t[p1][p^1];
     64     ins(t[p1][p],t[p2][p],z,d-1);
     65 }
     66 
     67 int query(int p1,int p2,int z,int d)
     68 {
     69     if(d<0) return 0;
     70     int p=(z>>d)&1;
     71     if(s[t[p2][p^1]]-s[t[p1][p^1]]) return (1<<d)+query(t[p1][p^1],t[p2][p^1],z,d-1);
     72      else return query(t[p1][p],t[p2][p],z,d-1);
     73 }
     74 
     75 int main()
     76 {
     77     //freopen("1.in","r",stdin);
     78     //freopen("1.out","w",stdout);
     79     cnt=0;
     80     ins(0,root[1],0,24);
     81     int n=read(),m=read();
     82     n++;
     83     rep(i,2,n)
     84     {
     85         int x=read();
     86         b[i]=b[i-1]^x;
     87         ins(root[i-1],root[i],b[i],24);
     88     }
     89     while(m--)
     90     {
     91         scanf("%s",ch);
     92         if(ch[0]=='A')
     93         {
     94             int x=read();
     95             n++;
     96             b[n]=b[n-1]^x;
     97             ins(root[n-1],root[n],b[n],24);
     98         }
     99          else
    100          {
    101              int l=read(),r=read(),x=read();
    102              int ans=query(root[l-1],root[r],b[n]^x,24);
    103              printf("%d
    ",ans);
    104          }
    105     }
    106 
    107     return 0;
    108 }
  • 相关阅读:
    English,The Da Vinci Code, Chapter 23
    python,meatobject
    English,The Da Vinci Code, Chapter 22
    English,The Da Vinci Code, Chapter 21
    English,The Da Vinci Code, Chapter 20
    English,The Da Vinci Code, Chapter 19
    python,xml,ELement Tree
    English,The Da Vinci Code, Chapter 18
    English,The Da Vinci Code, Chapter 17
    English,The Da Vinci Code, Chapter 16
  • 原文地址:https://www.cnblogs.com/myx12345/p/11883403.html
Copyright © 2011-2022 走看看