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 ...
随机推荐
- python 工具 二进制文件处理之——去掉指定长度数据包头
包头48bit 数据98464 ...如此循环: piece_size = 48 piece_size1 = 98464 with open("C:\\Users\\Administrato ...
- hdu5379||2015多校联合第7场1011 树形统计
pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is ...
- react 自定义 百度地图(BMap)组件
1.html 页面引入 相关js public/index.html <!DOCTYPE html> <html lang="en"> <head&g ...
- java中使用opencv
Java + opencv学习:在Eclipse下配置基于Java的OpenCV开发环境 2016-04-08 17:43 6491人阅读 评论(0) 收藏 举报 分类: OpenCV学习(10) ...
- Linux中的du和df命令
现在也将前阵子学习到du/df两个命令总结一下吧.前阵子测试工作中有遇到过由于磁盘空间满导致程序无法执行到情况,所以使用了df和du两个命令. du查看目录大小,df查看磁盘使用情况.我常使用的命令( ...
- mysql could not be resolved: Name or service not known
问题: mysql DNS反解:skip-name-resolve 错误日志有类似警告: 1.120119 16:26:04 [Warning] IP address '192.168.1.10' c ...
- FFmpeg Basics阅读笔记1:介绍
Multimedia handling with a fast audio and video encoder 作者:Frantisek Korbel 网址:http://ffmpeg.tv/ FFm ...
- 九度OJ 1135:字符串排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1559 解决:807 题目描述: 先输入你要输入的字符串的个数.然后换行输入该组字符串.每个字符串以回车结束,每个字符串少于一百个字符. 如 ...
- 九度OJ 1099:后缀子串排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3439 解决:1491 题目描述: 对于一个字符串,将其后缀子串进行排序,例如grain 其子串有: grain rain ain i ...
- if UDP is permitted
Networking Basics (The Java™ Tutorials > Custom Networking > Overview of Networking) https://d ...