Caesar Cipher(线段树 + 字符串哈希)

题目链接: Caesar Cipher

大致题意

给定一个非负整数序列, 有两种操作: 操作1: 将[l, r]区间所有数字+1, 操作2: 询问两个子序列区间是否相同.

特别的: 对于每一个数字, 应该为[0, 65535]之间的整数(%65536).

解题思路

区间修改+区间查询+询问子序列是否相同 ==> 线段树 + 字符串哈希

AC代码

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
const int N = 5E5 + 10, mod = 1E9 + 7, MOD = 65536, B = 13331; //B为进制
int w[N], P[N];
struct node {
    int l, r; int base; //base表示当前区间的基数, 仅在build时固定即可
    int fmax, hash; //fmax存放当前区间数字的最大值, 根结点的fmax即为当前位的数值
    int lazy;
}t[N << 2];
void pushdown(node& op, int lazy) {
    op.lazy += lazy;
    op.fmax += lazy;
    op.hash = (op.hash + (ll)lazy * op.base) % mod;
}
void pushdown(int x) {
    if (!t[x].lazy) return;
    pushdown(t[x << 1], t[x].lazy), pushdown(t[x << 1 | 1], t[x].lazy);
    t[x].lazy = 0;
}

void pushup(int x) {
    t[x].fmax = max(t[x << 1].fmax, t[x << 1 | 1].fmax);
    t[x].hash = (t[x << 1].hash + t[x << 1 | 1].hash) % mod;
}

void build(int l, int r, int x = 1) {
    if (l == r) { 
        t[x] = { l, r, P[l], w[l], 0, 0 }; 
        t[x].hash = (ll)P[l] * w[l] % mod; 
        return;
    }
    int mid = l + r >> 1;
    build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
    t[x] = { l, r, (t[x << 1].base + t[x << 1 | 1].base) % mod, 0, 0, 0 };
    pushup(x);
}

void modify(int l, int r, int c, int x = 1) {
    if (l <= t[x].l && r >= t[x].r) { pushdown(t[x], c); return; }
    pushdown(x);
    int mid = t[x].l + t[x].r >> 1;
    if (l <= mid) modify(l, r, c, x << 1);
    if (r > mid) modify(l, r, c, x << 1 | 1);
    pushup(x);
}

int ask(int l, int r, int x = 1) {
    if (l <= t[x].l && r >= t[x].r) return t[x].hash;
    pushdown(x);
    int mid = t[x].l + t[x].r >> 1;
    if (r <= mid) return ask(l, r, x << 1);
    if (l > mid) return ask(l, r, x << 1| 1);
    auto L = ask(l, r, x << 1), R = ask(l, r, x << 1 | 1);
    return (L + R) % mod;
}

void outofmod(int x = 1) { //每次检测是否存在某些点的数值>MOD;
    if (t[x].fmax < MOD) return;
    if (t[x].l == t[x].r) {
        t[x].fmax = 0, t[x].hash = 0;
        return;
    }
    pushdown(x);
    outofmod(x << 1), outofmod(x << 1 | 1);
    pushup(x);
}

void init(int n) {
    P[0] = 1;
    rep(i, n) {
        P[i] = (ll)P[i - 1] * B % mod;
        scanf("%d", &w[i]);
    }
}
int main()
{
    int n, m; cin >> n >> m;
    init(n);
    build(1, n);
    while (m--) {
        int op; scanf("%d", &op);
        if (op == 1) {
            int l, r; scanf("%d %d", &l, &r);
            modify(l, r, 1); //由于本题的操作是给[l, r]加1, 所以传参就是1
            outofmod(); //检查溢出
        }
        else {
            int x, y, l; scanf("%d %d %d", &x, &y, &l);
            if (x > y) swap(x, y); //注意x应当小于y;
            int res1 = ask(x, x + l - 1), res2 = ask(y, y + l - 1); 
            res1 = (ll)res1 * P[y - x] % mod; //对齐区间
            printf("%s\n", res1 == res2 ? "yes" : "no");
        }
    }
    return 0;
}

END

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥Fau

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值