#include <bits/stdc++.h> using namespace std; #define endl "\n" #define int long long typedef long long ll; const int base = 131; const int p1 = 1222827239; const int N = 1e6 + 100; int n, q, pn[N]; string s; struct node { int lsum, rsum, len; node() {lsum = rsum = 0;} } Seg[N * 4]; node operator + (const node &l, const node &r) { node ans; ans.lsum = (l.lsum + pn[l.len] * r.lsum % p1) % p1; ans.rsum = (r.rsum + pn[r.len] * l.rsum % p1) % p1; ans.len = l.len + r.len; return ans; } void pushup(int id) { Seg[id] = Seg[id * 2] + Seg[id * 2 + 1]; } void build(int id, int l, int r) { Seg[id].len = r - l + 1; if (l == r) { Seg[id].lsum = Seg[id].rsum = s[l]; return; } int mid = l + r >> 1; build(id * 2, l, mid); build(id * 2 + 1, mid + 1, r); pushup(id); } void modify(int id, int l, int r, int pos, char c) { if (l == r) { Seg[id].lsum = Seg[id].rsum = c; return; } int mid = l + r >> 1; if (pos <= mid) modify(id * 2, l, mid, pos, c); else modify(id * 2 + 1, mid + 1, r, pos, c); pushup(id); } node query(int id, int l, int r, int x, int y) { if (x <= l && y >= r) return Seg[id]; int mid = l + r >> 1; if (y <= mid) return query(id * 2, l, mid, x, y); if (x > mid) return query(id * 2 + 1, mid + 1, r, x, y); return query(id * 2, l, mid, x, y) + query(id * 2 + 1, mid + 1, r, x, y); } void solve() { cin >> n >> q >> s; s = " " + s; pn[0] = 1; for (int i = 1; i <= n; i++) pn[i] = pn[i - 1] * 1ll * base % p1; build(1, 1, n); while (q--) { int op; cin >> op; if (op == 1) { int pos; cin >> pos; char c; cin >> c; modify(1, 1, n, pos, c); } else { int l, r; cin >> l >> r; node ans = query(1, 1, n, l, r); if (ans.lsum == ans.rsum) cout << "Yes" << endl; else cout << "No" << endl; } } } signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); // int T = 1; cin >> T; // while(T--) solve(); solve(); return 0; }View Code
标签:int,线段,mid,Seg,哈希,ans,字符串,lsum,id From: https://www.cnblogs.com/zhujio/p/18012053