Description

多个询问l,r,求所有子区间异或和中最大是多少

强制在线

Solution

分块+可持久化trie

1.对于每块的左端点L,预处理出L到任意一个i,[L,j] 间所有子区间异或和中最大为多少,\(\Theta(n\sqrt n)\)

2.对于询问x,y:

①x,y属于同一块,O(\(\sqrt n log n\))直接扫

②x,y不属于同一块, 找到x右边第一块的左端点,用预处理求出左端点到y,剩下的直接扫,O(\(\sqrt n log n\))

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long LL;
const int M=12930;
const int B=30;
const int L=111;
inline int rd(){
int x=0;bool f=1;char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')f=0;
for(;isdigit(c);c=getchar())x=x*10+c-48;
return x;
} int n,m,sn;
int a[M];
int s[L][M];
int tot;
int ch[M*32][2];
int sz[M*32];
int root[M]; int F(int x){//写法取决于存法
return x/sn;
} int cpynode(int rt){
tot++;
ch[tot][0]=ch[rt][0];
ch[tot][1]=ch[rt][1];
sz[tot]=sz[rt]+1;
return tot;
} int ins(int rt,int d){
int tmp,x,y,i,k;
tmp=x=cpynode(rt);
for(i=B;i>=0;i--){
k=(d>>i)&1;
y=cpynode(ch[x][k]);
ch[x][k]=y;
x=y;
}
return tmp;
} int get(int lt,int rt,int d){
int i,k,res=0;
for(i=B;i>=0;i--){
k=((d>>i)&1)^1;
if(sz[ch[rt][k]]-sz[ch[lt][k]]>0)
lt=ch[lt][k],rt=ch[rt][k],res+=(1<<i);
else lt=ch[lt][k^1],rt=ch[rt][k^1];
}
return res;
} int main(){
int i,j;
int x,y,z,st,ll,rr;
n=rd(),m=rd();
sn=(int)sqrt(n);
for(i=1;i<=n;i++) a[i]=a[i-1]^rd();
root[0]=ins(root[0],0);
for(i=1;i<=n;i++) root[i]=ins(root[i-1],a[i]);
for(i=0;i<=n;i++) if(i%sn==0){//
x=F(i);
s[x][i]=0;
for(j=i+1;j<=n;j++){
s[x][j]=max(s[x][j-1],get(root[(i-1)>=0?(i-1):(n+1)],root[j-1],a[j]));
}
}
int ans=0;
for(i=1;i<=m;i++){
ans%=n;
ll=rd()%n,rr=rd()%n;
x=min((ll+ans)%n+1,(rr+ans)%n+1);
y=max((ll+ans)%n+1,(rr+ans)%n+1);
x--;
ans=0;
if(F(x)==F(y)){
//ans=a[y];这样写是错的
for(j=x;j<y;j++) ans=max(ans,get(root[j],root[y],a[j]));
}
else{
if(x%sn==0) z=F(x);//
else z=F(x)+1;
st=z*sn;//取决于存法
ans=s[z][y];
for(j=x;j<st;j++) ans=max(ans,get(root[j],root[y],a[j]));
}
printf("%d\n",ans);
}
return 0;
}

bzoj 2741 分块+可持久化trie的更多相关文章

  1. bzoj2741(分块+可持久化Trie)

    题意中文我就不说了 解析: 分块+可持久化Trie,先得到前缀异或值,插入到Trie中,然后分块,对每一块,处理出dp[i][j](i代表第几块,j代表第几个位置),dp[i][j]代表以第i块开始的 ...

  2. BZOJ 2741: 【FOTILE模拟赛】L [分块 可持久化Trie]

    题意: 区间内最大连续异或和 5点调试到现在....人生无望 但总算A掉了 一开始想错可持久化trie的作用了...可持久化trie可以求一个数与一个数集(区间中的一个数)的最大异或和 做法比较明显, ...

  3. BZOJ.2741.[FOTILE模拟赛]L(分块 可持久化Trie)

    题目链接 首先记\(sum\)为前缀异或和,那么区间\(s[l,r]=sum[l-1]^{\wedge}sum[r]\).即一个区间异或和可以转为求两个数的异或和. 那么对\([l,r]\)的询问即求 ...

  4. BZOJ2741 FOTILE模拟赛L(分块+可持久化trie)

    显然做个前缀和之后变成询问区间内两个数异或最大值. 一种暴力做法是建好可持久化trie后直接枚举其中一个数查询,复杂度O(nmlogv). 观察到数据范围很微妙.考虑瞎分块. 设f[i][j]为第i个 ...

  5. 【BZOJ2741】【FOTILE模拟赛】L 分块+可持久化Trie树

    [BZOJ2741][FOTILE模拟赛]L Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max( ...

  6. BZOJ 3166 set+可持久化trie树(OR 莫队)

    思路: 1.找次大值 我们不妨设当前点是次大的 那这段区间为 左边第二个比它大的点的坐标+1 和右边第二个比它大的点的坐标-1 2.用可持久化trie树找异或最大值 也可以用莫队 //By Siriu ...

  7. BZOJ 2741 L (可持久化01Trie+分块)

    题目大意:给你一个序列,共有$q$个询问,每次询问区间$[L,R]$内最大连续字段异或和,强制在线,$n<=12000,m<=5000$ 有个细节没处理好$WA$了好久..还有一次$ans ...

  8. BZOJ - 2741 分块维护最大连续异或和

    题意:给定\(a[l...r]\),多次询问区间\([l,r]\)中的最大连续异或和\(a_i⊕a_{i+1}⊕...⊕a_{j},l≤i≤j≤r\) 一眼过去认为是不可做的,但题目给出\(n=1.2 ...

  9. bzoj 2741 [FOTILE模拟赛] L

    Description 多个询问l,r,求所有子区间异或和中最大是多少 强制在线 Solution 分块+可持久化trie 1.对于每块的左端点L,预处理出L到任意一个i,[L,j] 间所有子区间异或 ...

随机推荐

  1. 利用maven的resources、filter和profile实现不同环境使用不同配置文件

    基本概念说明(resources.filter和profile): 1.profiles定义了各个环境的变量id 2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profil ...

  2. 流形(Manifold)初步【转】

    转载自:http://blog.csdn.net/wangxiaojun911/article/details/17076465 欧几里得几何学(Euclidean Geometry) 两千三百年前, ...

  3. 在调用Qt库来实现功能过程中的一些总结

    1.对于QTabWidget中tab名字的变化.当其中只有一个&时,Qt Assistant中给出的解释是:If the tab's label contains an ampersand, ...

  4. 【BZOJ3527】【FFT】力

    [问题描述]给出n个数qi,给出Fj的定义如下:令Ei=Fi/qi.试求Ei.[输入格式]输入文件force.in包含一个整数n,接下来n行每行输入一个数,第i行表示qi.[输出格式]输出文件forc ...

  5. jQuery慢慢啃之回调(十三)

    1.callbacks.add(callbacks)//回调列表中添加一个回调或回调的集合 // a sample logging function to be added to a callback ...

  6. POJ 1014 Dividing 多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63980   Accepted: 16591 Descri ...

  7. 复选框字段数组拆分后循环选项值,if判断根据选项值,前端输出html

    {php $specials = explode(',',$r[special]);} <div class="special"> {loop $specials $s ...

  8. mysql 中执行的 sql 注意字段之间的反向引号和单引号

    如下的数据表 create table `test`( `id` int(11) not null auto_increment primary key, `user` varchar(100) no ...

  9. css3 iphone开关 移动端开关、按钮、input

    css3  iphone开关  移动端开关.按钮.input <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  10. Php会员权限

    <?phpecho $uu=@array_sum(@$_POST['gr']);?><form action="" method="POST" ...