zoukankan      html  css  js  c++  java
  • 【BZOJ】【3261】最大异或和

    可持久化Trie


      嗯……同样搞个前缀异或和,然后将x与sum异或一下,就是在[l-1,r-1]中找x^sum的最大异或值了。同样可持久化Trie搞搞即可(模板还是没背全啊……sad

     1 /**************************************************************
     2     Problem: 3261
     3     User: Tunix
     4     Language: C++
     5     Result: Accepted
     6     Time:3888 ms
     7     Memory:239552 kb
     8 ****************************************************************/
     9  
    10 //BZOJ 3261
    11 #include<cstdio>
    12 #include<cstring>
    13 #include<cstdlib>
    14 #include<iostream>
    15 #include<algorithm>
    16 #define rep(i,n) for(int i=0;i<n;++i)
    17 #define F(i,j,n) for(int i=j;i<=n;++i)
    18 #define D(i,j,n) for(int i=j;i>=n;--i)
    19 #define pb push_back
    20 using namespace std;
    21 typedef long long LL;
    22 inline int getint(){
    23     int r=1,v=0; char ch=getchar();
    24     for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-1;
    25     for(; isdigit(ch);ch=getchar()) v=v*10-'0'+ch;
    26     return r*v;
    27 }
    28 const int N=20000010;
    29 /*******************template********************/
    30  
    31 int n,m,t[N][2],tot,rt[1000010],id[N];
    32 inline void Ins(int pre,int x,int k){
    33     int now=rt[k]=++tot; id[tot]=k;
    34     D(i,30,0){
    35         int j=(x>>i)&1;
    36         t[now][j^1]=t[pre][j^1];
    37         t[now][j]=++tot; id[tot]=k; now=tot;
    38         pre=t[pre][j];
    39     }
    40 }
    41 inline int ask(int l,int r,int x){
    42     int ans=0,now=rt[r];
    43     D(i,30,0){
    44         int j=((x>>i)&1)^1;
    45         if (id[t[now][j]]>=l) ans|=1<<i; else j^=1;
    46         now=t[now][j];
    47     }
    48     return ans;
    49 }
    50  
    51 int main(){
    52 #ifndef ONLINE_JUDGE
    53     freopen("3261.in","r",stdin);
    54     freopen("3261.out","w",stdout);
    55 #endif
    56     n=getint(); m=getint();
    57     id[0]=-1;
    58     Ins(rt[0],0,0);
    59     int sum=0;
    60     F(i,1,n){
    61         sum^=getint();
    62         Ins(rt[i-1],sum,i);
    63     }
    64     char cmd[5]; int x,l,r;
    65     F(i,1,m){
    66         scanf("%s",cmd);
    67         if (cmd[0]=='A'){
    68             sum^=getint();
    69             Ins(rt[n],sum,n+1); n++;
    70         }else{
    71             l=getint(); r=getint(); x=getint();
    72             printf("%d
    ",ask(l-1,r-1,sum^x));
    73         }
    74     }
    75     return 0;
    76 }
    View Code

    3261: 最大异或和

    Time Limit: 10 Sec  Memory Limit: 512 MB
    Submit: 743  Solved: 323
    [Submit][Status][Discuss]

    Description

         

    给定一个非负整数序列 {a},初始长度为 N。      
    有   M个操作,有以下两种操作类型:
     
    1 、A x:添加操作,表示在序列末尾添加一个数 x,序列的长度 N+1。
    2 、Q l r x:询问操作,你需要找到一个位置 p,满足 l<=p<=r,使得:
     
    a[p] xor a[p+1] xor ... xor a[N] xor x 最大,输出最大是多少。  

    Input

    第一行包含两个整数 N  ,M,含义如问题描述所示。  
    第二行包含 N个非负整数,表示初始的序列 A 。
     
    接下来 M行,每行描述一个操作,格式如题面所述。   

    Output

    假设询问操作有 T个,则输出应该有 T行,每行一个整数表示询问的答案。

    Sample Input

    5 5
    2 6 4 3 6
    A 1
    Q 3 5 4
    A 4
    Q 5 7 0
    Q 3 6 6
    对于测试点 1-2,N,M<=5 。

    对于测试点 3-7,N,M<=80000 。
    对于测试点 8-10,N,M<=300000 。

    其中测试点 1, 3, 5, 7, 9保证没有修改操作。
    对于 100% 的数据, 0<=a[i]<=10^7。

    Sample Output

    4
    5
    6

    HINT

    对于      100%  的数据,     0<=a[i]<=10^7  。

    Source

    [Submit][Status][Discuss]
  • 相关阅读:
    JSTL学习总结
    Spring 3 MVC: Create Hello World Application In Spring 3.0 MVC(reprint)
    如何查询端口号被哪个程序占用?
    php 共享内存
    php 消息队列
    php 快速fork出指定个子进程
    批量 kill mysql 中运行时间长的sql
    socket发送http请求
    TCP/IP、Http、Socket的区别
    文本协议和二进制协议
  • 原文地址:https://www.cnblogs.com/Tunix/p/4553261.html
Copyright © 2011-2022 走看看