题目链接:G.Caesar Cipher
题意
给一个范围为[0,65535]的数组
你可以进行以下操作:
- 操作一:给出l和r,然后对[l,r]区间里的每一个数+1,然后每个数对65536取模
- 操作二:给出x,y和l,询问[x,x+l-1]和[y,y+l-1]这两个区间里的元素是否完全相同,相同输出yes,否则no。
题解
对于询问区间相同,暴力显然不可行,所以只能哈希去判断,这种区间操作的题只能用线段树维护哈希。(大部分比赛中hash指的进制hash)
想入门hash的可以看牛客有位大佬的博客:哈希从入门到精通
进制哈希其实就是把一个数转化为一个值,这个值是base进制的。
线段树维护hash会遇到几个问题:
- 如何把左右两个区间的hash值pushup。在了解进制hash后,这个操作就不难实现。
h a s h [ p ] = h a s h [ p ∗ 2 ] ∗ b a s e l e n 右 区 间 + h a s h [ p ∗ 2 + 1 ] { hash[p]=hash[p*2]*base^{len_{右区间}}+hash[p*2+1] } hash[p]=hash[p∗2]∗baselen右区间+hash[p∗2+1] (base是进制数) - 操作一对区间+1后,如何对hash值进行修改。
其实不难发现变化的就是base的前缀和。例如某个区间的hash= ∑ i = 0 n a [ i ] ∗ b a s e i {\sum_{i=0}^{n}a[i]*base^i} ∑i=0na[i]∗basei,每个数+1后,hash= ∑ i = 0 n ( a [ i ] + 1 ) ∗ b a s e i = ∑ i = 0 n a [ i ] ∗ b a s e i + ∑ i = 0 n b a s e i {\sum_{i=0}^{n}(a[i]+1)*base^i}={\sum_{i=0}^{n}a[i]*base^i}+\sum_{i=0}^nbase^i ∑i=0n(a[i]+1)∗basei=∑i=0na[i]∗basei+∑i=0nbasei。所以我们只需维护一个base幂次的前缀和即可。 - 在加完后如何取模?
我们可以维护一个区间最大值, 在每次对区间+1后,再更新线段树。如果左子树最大值大于等于65536,则向左子树更新;右子树亦如此。直到遇到节点的最大值小于模值则停止向下递归。在最后的叶子节点减去模值来更新最大值以及hash值。 - 在查询时如何合并左右区间值
和第一个问题区间合并类似, h a s h [ r o o t ] = h a s h [ l ] ∗ b a s e l e n 查 询 右 区 间 + h a s h [ r ] {hash[root]=hash[l]*base^{len_{查询右区间}}+hash[r]} hash[root]=hash[l]∗baselen查询右区间+hash[r]
解决这些问题本题基本就迎刃而解了。
代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<unordered_set>
#include<unordered_map>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x
const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=(1<<30)+7;
const int inf=0x3f3f3f3f;
const int maxn=5e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const ll base=23333333;
const ll mod1=65536;
ll a[maxn];
ll tree[maxn<<2],lazy[maxn<<2],poww[maxn<<2],pre[maxn<<2];
ull Hash[maxn<<2];
void pushup(int p,int l,int r)
{
tree[p]=max(tree[p*2],tree[p*2+1]);
int mid=(l+r)>>1;
Hash[p]=(Hash[p*2]*poww[r-mid]%mod+Hash[p*2+1])%mod;
}
void pushdown(int p,int l,int r)
{
if(lazy[p]==0) return ;
int mid=(l+r)>>1;
Hash[p*2]=(Hash[p*2]+lazy[p]*pre[mid-l]%mod)%mod;
Hash[p*2+1]=(Hash[p*2+1]+lazy[p]*pre[r-mid-1]%mod)%mod;
lazy[p*2]+=lazy[p];
lazy[p*2+1]+=lazy[p];
tree[p*2]+=lazy[p];
tree[p*2+1]+=lazy[p];
lazy[p]=0;
}
void build(int p,int l,int r)
{
lazy[p]=0;
if(l==r) { Hash[p]=tree[p]=a[l]; return ; }
int mid=(l+r)/2;
build(p*2, l, mid);
build(p*2+1, mid+1, r);
pushup(p,l,r);
}
void add(int p,int l,int r,int addl,int addr)
{
if(addl<=l && r<=addr)
{
Hash[p]=(Hash[p]+pre[r-l])%mod;
tree[p]++;
lazy[p]++;
return ;
}
pushdown(p, l, r);
int mid=(l+r)/2;
if(addl<=mid) add(p*2,l,mid,addl,addr);
if(addr>mid) add(p*2+1,mid+1,r,addl,addr);
pushup(p, l, r);
}
void update_mod(int p,int l,int r)
{
if(tree[p]<mod1) return ;
if(l==r)
{
tree[p]-=mod1;
Hash[p]-=mod1;
return ;
}
pushdown(p, l, r);
int mid=(l+r)>>1;
if(tree[p*2]>=mod1) update_mod(p*2, l, mid);
if(tree[p*2+1]>=mod1) update_mod(p*2+1, mid+1, r);
pushup(p, l, r);
}
ll query(int p,int l,int r,int ql,int qr)
{
if(ql<=l && qr>=r) return Hash[p];
pushdown(p, l, r);
int mid=(l+r)>>1;
ll s=0;
if(ql<=mid) s=query(p*2, l, mid, ql, qr)%mod;
if(qr>mid) s=(s*poww[min(qr,r)-mid]%mod+query(p*2+1, mid+1, r, ql, qr))%mod;
return s;
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
poww[0]=1;
for(int i=1;i<=4*n;i++) poww[i]=poww[i-1]*base%mod;
pre[0]=1;
for(int i=1;i<=4*n;i++) pre[i]=(pre[i-1]+poww[i])%mod;
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
build(1, 1, n);
while (q--) {
int opt;
scanf("%d",&opt);
if(opt==1)
{
int l,r; scanf("%d%d",&l,&r);
add(1, 1, n, l, r);
update_mod(1, 1, n);
}
else
{
int x,y,l; scanf("%d%d%d",&x,&y,&l);
if(query(1,1,n,x,x+l-1)==query(1,1,n,y,y+l-1)) printf("yes\n");
else printf("no\n");
}
}
}