SPOJ MAXOR (分块 || 可持久化字典树 || 异或)(好题)
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]区间的最大的连续异或值,强制在线。
解法: 分块+可持久化字典树
- 首先,分块,把每个点分属于一个块,belong[i]=(i-1)/sqrt(n) +1;
- 预处理得到每个块的左边界到右边的点的区间的最大异或,即如果(i-1)%sqrt(n)=0 ,则计算Maxxor[belong[i],j],(i<=j<=n)(这里很关键,不过先别管这里这么实现的,看完)
- 对于每个询问[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 (分块 || 可持久化字典树 || 异或)(好题)的更多相关文章
- 【BZOJ 3261】最大异或和【可持久化字典树】
题意 给出一个长度为n的整数序列,给出m个操作.操作有两种.1,Ax表示在序列结尾增加x.2,Qlrx表示找到一个位置p满足 l<=p<=r,使得a[p] xor a[p+1]xor... ...
- bzoj 3261 最大异或和 可持久化字典树(01树)
题目传送门 思路: 由异或的性质可得,题目要求的式子可以转化成求$max(pre[n]^x^pre[i])$,$pre[i]$表示前缀异或和,那么我们现在就要求出这个东西,所以用可持久化字典树来求,每 ...
- HDU 4757 Tree 可持久化字典树
Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...
- HDU - 6191 Query on A Tree (可持久化字典树/字典树合并)
题目链接 题意:有一棵树,树根为1,树上的每个结点都有一个数字x.给出Q组询问,每组询问有两个值u,x,代表询问以结点u为根的子树中的某一个数与x的最大异或值. 解法一:dfs序+可持久化字典树.看到 ...
- 【HDU 6191】Query on A Tree 【可持久化字典树】
题目 给出一棵有n个结点的树,树根是1,每个结点给出一个value.然后给出q个询问,每个询问给出两个整数u和x,你要在以u结点为根的子树中找出一个结点v,使得val[v] xor x最大, 并输出这 ...
- Hdu-4757 Tree(可持久化字典树+lca)
题目链接:点这 我的github地址:点这 Problem Description Zero and One are good friends who always have fun wi ...
- 51Nod XOR key —— 区间最大异或值 可持久化字典树
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1295 1295 XOR key 题目来源: HackerRa ...
- Codeforces 706D Vasiliy's Multiset(可持久化字典树)
[题目链接] http://codeforces.com/problemset/problem/706/D [题目大意] 要求实现一个集合中的三个操作,1:在集合中加入一个元素x,2:从集合中删除一个 ...
- 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 ...
随机推荐
- 控制CUP占用率曲线
在<编程之美>上看过一道面试题就是要求:输出cup占用率的曲线图 今天看到了一篇文章就试试看看: #include <iostream> #include <cmath& ...
- Java代理学习笔记
代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类之间通常会存在关联关 ...
- CMake - boost - 可运行程序 - 静态库
CMake - boost 最后更新日期:2014-04-25by kagula 阅读前提:<CMake入门(二)>.Linux的基本操作 环境: Windows 8.1 64bit英文版 ...
- JavaScript技巧手冊
js小技巧 每一项都是js中的小技巧,但十分的有用! 1.document.write(""); 输出语句 2.JS中的凝视为// 3.传统的HTML文档顺序是:documen ...
- discuz X3.1+Apache2.2+php-5.2.17+mysql5.6.14+Discuz_X3.1
discuz X3.1+Apache2.2.25+php-5.2.17+mysql5.6.14+Discuz_X3.1 一.准备 1.httpd-2.2.25-win32-x86-no_ssl.msi ...
- react webapp 开发小结
1.监听props的方法 componentWillReceiveProps(nextProps) { // } 2.监听state的方法 3.props 传递的方法 <AlarmList {. ...
- 构造方法后面带:this()
可以这么理解,有参数的构造函数需要执行无参构造函数中的代码,为了省去重复代码的编写,所以就继承了,先执行没参数的那个构造函数. 在this上“转到定义”(F12)就到第一个构造函数上去了.
- MySQL 下优化SQL语句的一些经验
http://java-guru.iteye.com/blog/143377
- idea常用的快捷命令
main方法: psvm System.out.println(): sout
- paxos算法之粗浅理解
paxos出身 paxos出身名门,它爹是没多久前获得图灵奖的在分布式领域大名鼎鼎的LeslieLamport. paxos为何而生 那么Lamport他老人家为什么要搞这个东东呢,不是吃饱了撑的,而 ...