[BZOJ2741][FOTILE模拟]L(分块+可持久化Trie)
2017-06-15 07:28
387 查看
=== ===
这里放传送门=== ===
题解
这道题的答案显然没有办法合并,而平常写分块的大部分题目都是一块一块查出来答案再合并,所以显然需要一些另外的思路。。这道题的做法就是非常常见的一种做法,因为数据范围比较小,所以可以开一个大小为O(nn√)的数组记录对于每一块的端点,它到后面每一个点所组成的区间的答案,这个可以直接递推出来。然后当查询一段区间[l,r]的时候,先找到l位置右边离它最近的端点pos,然后[pos..r]这一段区间的答案可以直接查出来,这样就得到了所有左右端点都[pos..r]这一段范围内的答案,然后要处理的就是左端点在[l..pos-1]内的这些区间。而[pos..l]这段范围内的数字个数不会超过O(n√)个,所以可以枚举左端点,然后就是找一段范围内能和它构成最大连续异或和的右端点在什么位置了,这个问题就比较经典了,弄一个前缀和就变成了找和它异或和最大的数字,然后用可持久化Trie就可以了。时间复杂度O(nn√logn)。代码
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define Pow 64 using namespace std; int n,m,pos[12010],root[12010],tot,len[410],Block,v[70],cnt; long long a[12010],Ans[410][12010],lastans; struct Trie{ int ch[2],val; }t[1000010]; void Insert(int now,int last,int x){ int c=0; memset(v,0,sizeof(v)); while (x!=0){v[++c]=(x&1);x>>=1;} for (int i=Pow;i>=1;i--){ t[now].ch[v[i]]=++tot; now=t[now].ch[v[i]]; last=t[last].ch[v[i]]; t[now]=t[last];t[now].val++; } } long long Query(int now,int last,int x){ int c=0; long long ans=0; memset(v,0,sizeof(v)); while (x!=0){v[++c]=x&1;x>>=1;} for (int i=Pow;i>=1;i--){ int u=v[i],l1,l2; l1=t[now].ch[u^1];l2=t[last].ch[u^1]; if (t[l1].val-t[l2].val!=0){ now=l1;last=l2;ans=(ans|(1<<(i-1))); }else{now=t[now].ch[u];last=t[last].ch[u];} } return ans; } void BuildBlocks(){ int tail; for (int i=1;i<=n;i=tail+1){ tail=min(i+Block-1,n); ++cnt;len[cnt]=tail; for (int j=i;j<=tail;j++) pos[j]=cnt; Ans[cnt][i]=a[i]^a[i-1]; for (int j=i+1;j<=n;j++) Ans[cnt][j]=max(Ans[cnt][j-1],Query(root[j+1],root[i-1],a[j])); } } long long Count(int x,int y,int R){ long long ans=0; for (int i=x;i<=y;i++) ans=max(ans,Query(root[R+1],root[i-1],a[i-1])); return ans; } long long Ask(int x,int y){ int px,py,H; long long ans=0; px=pos[x];py=pos[y]; if (px==py) return Count(x,y,y); H=px+1; if (x==len[px-1]+1) --H; ans=Ans[H][y]; if (H==px) return ans; ans=max(ans,Count(x,len[px],y)); return ans; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%I64d",&a[i]); for (int i=1;i<=n;i++) a[i]^=a[i-1]; for (int i=0;i<=n;i++){ root[i+1]=++tot;t[tot]=t[root[i]]; t[tot].val++; Insert(root[i+1],root[i],a[i]); } Block=floor(sqrt(n)); BuildBlocks(); for (int i=1;i<=m;i++){ long long l,r; scanf("%I64d%I64d",&l,&r); l=(l+lastans)%n+1; r=(r+lastans)%n+1; if (l>r) swap(l,r); lastans=Ask(l,r); printf("%I64d\n",lastans); } return 0; }
相关文章推荐
- BZOJ 2741: 【FOTILE模拟赛】L [分块 可持久化Trie]
- 【分块】【可持久化Trie】bzoj2741 【FOTILE模拟赛】L
- BZOJ 2741 【FOTILE模拟赛】L 可持久化Trie 分块
- BZOJ2741 【FOTILE模拟赛】L 可持久化Trie+分块
- [BZOJ]2741: 【FOTILE模拟赛】L 可持久化Trie+分块
- [BZOJ2741][[FOTILE模拟赛]][可持久化Trie+分块]
- [BZOJ2741][FOTILE模拟赛]L(trie+分块)
- bzoj2741: 【FOTILE模拟赛】L 可持久化trie
- bzoj 2741 【FOTILE模拟赛】L 可持久化字典树+分块
- bzoj 2741: 【FOTILE模拟赛】L 分块+可持久化trie
- [可持久化字典树 分块] BZOJ 2741 【FOTILE模拟赛】L
- BZOJ 2741 【FOTILE模拟赛】L(可持久化trie)
- 【BZOJ2741】【FOTILE模拟赛】L 可持久化字典树+分块
- bzoj 2741 【FOTILE模拟赛】L 分块 可持久化trie树
- BZOJ 2741 【FOTILE模拟赛】L 分块+可持久化Trie树
- [BZOJ2741]【FOTILE模拟赛】L(分块+可持久化trie树)
- [BZOJ2741][FOTILE模拟赛]L-可持久化字典树-分块
- bzoj 2741: 【FOTILE模拟赛】L (分块+可持久化trie树)
- BZOJ 2741【FOTILE模拟赛】L 分块+可持久化trie
- BZOJ 2741【FOTILE模拟赛】L 分块+可持久化Trie树