[2020 CCPC 威海] G. Caesar Cipher 線段樹+hash
阿新 • • 發佈:2020-12-10
技術標籤:線段樹/數狀陣列
題目連結: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} - 在加完後如何取模?
我們可以維護一個區間最大值, 在每次對區間+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");
}
}
}