You are given a sequence A[1], A[2], ..., A[N]. (0 ≤ A[i] < 231, 1 ≤ N ≤ 12000).

A query is defined as follows:

  • Query(x,y) = Max { a[i] xor a[i+1] xor ... xor a[j] ; l ≤ i ≤ j ≤ r }.
  • l = min ( ((x+lastans) mod N)+1 , ((y+lastans) mod N)+1 ).
  • r = max ( ((x+lastans) mod N)+1 , ((y+lastans) mod N)+1 ).
  • lastans[1] = 0 , lastans[i] = Query[i-1].

Given M queries, your program must output the results of these queries. (0 ≤ M ≤ 6000).

IMPORTANT : PROBLEM ENHANCED. ( I'M SO SORRY.. )

Input

  • The first line of the input file contains 2 numbers : N M.
  • In the second line, N numbers follow.
  • M lines follow, where line i contains 2 numbers xi and yi.

Output

Your program should output the results of the M queries, one query per line.

Example

Input:
3 3
1 4 3
0 1
0 1
4 3 Output:
5
7
7

题目:

对于每个询问,输出[L,R]区间的最大的连续异或值,强制在线。

解法: 分块+可持久化字典树

  1. 首先,分块,把每个点分属于一个块,belong[i]=(i-1)/sqrt(n) +1;
  2. 预处理得到每个块的左边界到右边的点的区间的最大异或,即如果(i-1)%sqrt(n)=0 ,则计算Maxxor[belong[i],j],(i<=j<=n)(这里很关键,不过先别管这里这么实现的,看完)
  3. 对于每个询问[L,R],由于我们预处理得到了[belong[L]+1,R]的最大异或和,现在只要计算[t,R]的最大异或和(L<=t<=belong[L]的右边界)。

-------------------------------------------嗯,看似行得通,怎么实现呢?----------------------------------------

首先,此题的分块没有暴力删除,没有添加等等操作,分块只是为了预处理,的得到多个区间的最大异或和。

对于第二步, 我们怎么实现呢,看似复杂度很大。但是我们对于MAXOR[i,j],充分利用到前面计算的结果,MAXOR[i,j]=max(MAXOR[i,j-1],num[j]^字典树)。

然后问题来了,字典树怎么限定边界呢? ------可持久化实现。

那么对于每个询问,只有[L,相应块的右边界]需要在持久化Trie里找最大,然后结合预先处理的[L的右边界+1,R]得到结果。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
struct nmphy
{
int n,m,a[maxn],G[][maxn];
int rt[maxn],sum[maxn*],ch[maxn*][],cnt;
int B,group[maxn],L; int swap(int &u,int &v) { u^=v; v^=u; u^=v; }
int Max (int &x,int y) { if(y>x) x=y;} void init()
{
scanf("%d%d",&n,&m);
n++; int tmp=;
for(int i=;i<=n;i++){ //这里i==1是为了把a[1]==0加进去。
scanf("%d",&a[i]);
a[i]^=a[i-];
tmp|=a[i]; //得到最长位。
}
L=; while(tmp) L++,tmp>>=; if(!L) L=;
for(int i=;i<=n;i++) insert(rt[i-],rt[i],a[i],L-);
} void insert(int x,int &y,int val,int pos)
{
sum[y=++cnt]=sum[x]+;
if(pos==) return ;
int d=(val>>pos)&;
ch[y][d^]=ch[x][d^];
insert(ch[x][d],ch[y][d],val,pos-);
} int query(int x,int y,int val)
{
int res=;
for(int i=L-;i>=;i--){
int d=(val>>i)&;
if(sum[ch[y][d^]]-sum[ch[x][d^]]>) {
res+=(<<i);
x=ch[x][d^];y=ch[y][d^];
}
else x=ch[x][d],y=ch[y][d];
} return res;
}
int cal(int x, int y, int v, int d) {
if (d < ) return ;
int p = v >> d & ; int tmp = sum[ch[y][p ^ ]] - sum[ch[x][p ^ ]];
if (tmp > ) return ( << d) + cal(ch[x][p ^ ], ch[y][p ^ ], v, d - );
else return cal(ch[x][p], ch[y][p], v, d - );
} void divide()
{
while(B*B<n) B++;
for(int i=;i<=n;i++) {
group[i]=(i-)/B+;
if((i-)%B==){
for(int j=i;j<=n;j++){
G[group[i]][j]=cal(rt[i],rt[j],a[j],L-);
Max(G[group[i]][j],G[group[i]][j-]);
}
}
}
} void solve()
{
int ans=,tmp,l,r;
for(int i=;i<=m;i++)
{
scanf("%d%d",&l,&r);
l=(l+(long long)ans)%(n-)+;
r=(r+(long long)ans)%(n-)+;
if(l>r) swap(l,r); r++;
ans=G[group[l]+][r];
for(int j=l;j<=r&&group[j]==group[l];j++){
tmp=cal(rt[l],rt[r],a[j],L-);
Max(ans,tmp);
}
printf("%d\n",ans);
}
}
}Tree;
int main()
{
Tree.init(); //输入,持久化Trie树
Tree.divide(); //分块,预处理块尾到后面的信息。
Tree.solve(); //快头之后的信息+块头前的暴力。
return ;
}

SPOJ MAXOR (分块 || 可持久化字典树 || 异或)(好题)的更多相关文章

  1. 【BZOJ 3261】最大异或和【可持久化字典树】

    题意 给出一个长度为n的整数序列,给出m个操作.操作有两种.1,Ax表示在序列结尾增加x.2,Qlrx表示找到一个位置p满足 l<=p<=r,使得a[p] xor a[p+1]xor... ...

  2. bzoj 3261 最大异或和 可持久化字典树(01树)

    题目传送门 思路: 由异或的性质可得,题目要求的式子可以转化成求$max(pre[n]^x^pre[i])$,$pre[i]$表示前缀异或和,那么我们现在就要求出这个东西,所以用可持久化字典树来求,每 ...

  3. HDU 4757 Tree 可持久化字典树

    Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...

  4. HDU - 6191 Query on A Tree (可持久化字典树/字典树合并)

    题目链接 题意:有一棵树,树根为1,树上的每个结点都有一个数字x.给出Q组询问,每组询问有两个值u,x,代表询问以结点u为根的子树中的某一个数与x的最大异或值. 解法一:dfs序+可持久化字典树.看到 ...

  5. 【HDU 6191】Query on A Tree 【可持久化字典树】

    题目 给出一棵有n个结点的树,树根是1,每个结点给出一个value.然后给出q个询问,每个询问给出两个整数u和x,你要在以u结点为根的子树中找出一个结点v,使得val[v] xor x最大, 并输出这 ...

  6. Hdu-4757 Tree(可持久化字典树+lca)

    题目链接:点这 我的github地址:点这     Problem Description   Zero and One are good friends who always have fun wi ...

  7. 51Nod XOR key —— 区间最大异或值 可持久化字典树

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1295 1295 XOR key  题目来源: HackerRa ...

  8. Codeforces 706D Vasiliy's Multiset(可持久化字典树)

    [题目链接] http://codeforces.com/problemset/problem/706/D [题目大意] 要求实现一个集合中的三个操作,1:在集合中加入一个元素x,2:从集合中删除一个 ...

  9. hdu 6191--Query on A Tree(持久化字典树)

    题目链接 Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey A l ...

随机推荐

  1. 【Lintcode】二叉树的最大深度 - 比较简单,用递归比较好,不递归也能做,比较麻烦

    给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的 ...

  2. Cucumber+Rest Assured快速搭建api自动化测试平台

    转载:http://www.jianshu.com/p/6249f9a9e9c4 什么是Cucumber?什么是BDD?这里不细讲,不懂的直接查看官方:https://cucumber.io/ 什么是 ...

  3. 使用match、test控制输入字符格式后键盘向左向右键光标自动定位解决

    直接举例说明(正则表达式替换红色区域即可) /** * 判断是否位数字 * @param obj 数字 */ function numOnly(obj) { if(!(/^\d*$/.test(obj ...

  4. ZipOutputStream 用法 小计

    ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile)); 构造函数之后 文件就已经创建出来了 只是 0kb s.Write(bu ...

  5. web前端面试系列 一 js闭包

    一.什么是闭包? JavaScript高级程序设计第三版: 闭包是指有权访问另一个函数作用域中的变量的函数. 在js中定义在函数内部的子函数能够访问外部函数定义的变量,因此js内部函数就是一个闭包. ...

  6. ubuntu将快捷方式复制到桌面

    /usr/share/applications目录下,如果我们要创建桌面快捷方式,只需要右键-复制-桌面 就Ok

  7. C#获取Access数据库中的所有表名和列名

    //C#获取Access数据库中的所有表名和列名    string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" ...

  8. ecshop忘记管理员密码

    直接修改数据表 ecs_admin_user, 找到对应的管理员, 同时修改 password 为 2fc3ec4c91d51bee94f4a8ccbdbe5383 和 ec_salt 为1819, ...

  9. iOS 图像处理-剪裁图像

    解决这个问题:依照某一长宽比例,剪裁图片的上部和下部.保留中间的内容.当然也能够自己定义须要剪裁留下的区域 前提:须要加入Framework:CoreGraphics.framework 代码: - ...

  10. ffmpeg下载rtmp flv

    ffmpeg -i rtmp://shanghai.chinatax.gov.cn:1935/fmsApp/16a0148f117.flv -c copy dump.flv