2020CCPC威海 G. Caesar Cipher 线段树+哈希

原题链接:https://codeforces.ml/gym/102798/problem/G

题意

有一串序列,有两个操作

  1. l r 给[l,r]区间内的数加1再对65536取模
  2. x y len 问区间[x, x+len-1]和[y, y+len-1]子串是否相同

分析

先来看第二个操作,非常典型的哈希操作,我们先设一个f[i]为base^i,然后hash值就是当前的数字a[i]乘上f[i],那么整个区间的哈希值就是
∑ l r ( f [ i ] ∗ a [ i ] ) \sum_{l}^{r}(f[i]*a[i]) lr(f[i]a[i])

然后我们再看第一个操作,如果区间内的数+1,其实就是加上

∑ l r f [ i ] \sum_{l}^{r}f[i] lrf[i]

但对于65536要特殊处理,因为65536+1取模后为0,哈希值也为0。

这样有些人可能觉得时间复杂度会炸,因为要遍历所有值为65536的点,但其实不会,因为数值的增长是每次是1,也就是说每6e4次才会特判一次,也就是说最多只会特判5e5/6e4大约是10次,也就是10*n+nlogn

Code

#include <bits/stdc++.h>
using namespace std;
//#define ACM_LOCAL
#define re register
#define fi first
#define se second
#define please_AC return 0
const int N = 5e5 + 10;
const int M = 1e6 + 10;
const int INF = 1e9;
const double eps = 1e-4;
const int MOD = 1e9 + 7;
typedef long long ll;
ll f[N], a[N];
struct node {
    int l, r;
    ll base, Hash, maxx, tag;
}t[N<<2];
void push_up(int u) {
    t[u].Hash = (t[u<<1].Hash + t[u<<1|1].Hash) % MOD;
    t[u].maxx = max(t[u<<1].maxx, t[u<<1|1].maxx);
    t[u].base = (t[u<<1].base + t[u<<1|1].base) % MOD;
}
void push_down(int u) {
    if (t[u].tag) {
        t[u<<1].tag += t[u].tag;
        t[u<<1|1].tag += t[u].tag;
        t[u<<1].maxx += t[u].tag;
        t[u<<1|1].maxx += t[u].tag;
        t[u<<1].Hash = (t[u<<1].Hash + t[u<<1].base * t[u].tag) % MOD;
        t[u<<1|1].Hash = (t[u<<1|1].Hash + t[u<<1|1].base * t[u].tag) % MOD;
        t[u].tag = 0;
    }
}
void build(int u, int l, int r) {
    t[u].l = l, t[u].r = r;
    if (l == r) {
        t[u].maxx = a[l];
        t[u].base = f[l];
        t[u].Hash = t[u].base * a[l] % MOD;
        return;
    }
    int mid = (l + r) >> 1;
    build(u<<1, l, mid);
    build(u<<1|1, mid+1, r);
    push_up(u);
}
void modify1(int u, int ql, int qr) {
    if (ql <= t[u].l && qr >= t[u].r) {
        t[u].tag++;
        t[u].maxx++;
        t[u].Hash = (t[u].Hash + t[u].base) % MOD;
        return;
    }
    push_down(u);
    int mid = (t[u].l + t[u].r) >> 1;
    if (ql <= mid) modify1(u<<1, ql, qr);
    if (qr > mid) modify1(u<<1|1, ql, qr);
    push_up(u);
}
void modify2(int u) {
    if (t[u].maxx < 65536) return;
    if (t[u].l == t[u].r) {
        t[u].maxx = 0;
        t[u].Hash = 0;
        return;
    }
    push_down(u);
    modify2(u<<1);
    modify2(u<<1|1);
    push_up(u);
}
ll query(int u, int ql, int qr) {
    if (ql <= t[u].l && qr >= t[u].r) return t[u].Hash;
    push_down(u);
    int mid = (t[u].l + t[u].r) >> 1;
    ll ans = 0;
    if (ql <= mid) ans = (ans + query(u<<1, ql, qr)) % MOD;
    if (qr > mid) ans = (ans + query(u<<1|1, ql, qr)) % MOD;
    return ans;
}
void init() {
    f[0] = 1;
    for (int i = 1; i < N; i++) f[i] = f[i-1] * 23333 % MOD;
}
void solve() {
    init();
    int n, m; cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    for (int i = 1; i <= m; i++) {
        int opt, l, r, x; cin >> opt;
        if (opt == 1) {
            cin >> l >> r;
            modify1(1, l, r);
            modify2(1);
        } else {
            cin >> l >> r >> x;
            if (l > r) swap(l, r);
            ll temp1 = query(1, l, l + x - 1);
            ll temp2 = query(1, r, r + x - 1);
            temp1 = temp1 * f[r - l] % MOD;
            printf("%s\n", temp1 == temp2 ? "yes" : "no");
        }
    }
}

signed main() {
#ifdef ACM_LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    solve();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值