1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<template<typename...> class T, typename t, typename... Args> void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int M = 4E5 + 100; int f[M]; char s[M]; int ls, T, Q, ans;
namespace sam { int t[M][26], len[M] = {-1}, fa[M], sz, last, rpos[2][M], pos[2][M]; void ins(int ch, int pp, int id) { int p = last, np = 0, nq = 0, q = -1; if (!t[p][ch]) { np = sz++; len[np] = len[p] + 1; for (; p && !t[p][ch]; p = fa[p]) t[p][ch] = np; } if (!p) fa[np] = 1; else { q = t[p][ch]; if (len[p] + 1 == len[q]) fa[np] = q; else { nq = sz++; len[nq] = len[p] + 1; memcpy(t[nq], t[q], sizeof t[0]); fa[nq] = fa[q]; fa[np] = fa[q] = nq; for (; t[p][ch] == q; p = fa[p]) t[p][ch] = nq; } } last = np ? np : nq ? nq : q; rpos[id][pp] = last; pos[id][last] = pp; } }
const int SP = 19; vector<int> G[M]; int in[M], out[M], pa[M][SP], clk, rin[M]; void dfs(int u, int fa) { in[u] = ++clk; rin[clk] = u; pa[u][0] = fa; FOR (i, 1, SP) pa[u][i] = pa[pa[u][i - 1]][i - 1]; for (int& v: G[u]) dfs(v, u); out[u] = clk; }
int go_up(int u, int l) { using namespace sam; FORD (i, SP - 1, -1) { int v = pa[u][i]; if (len[u] - len[v] <= l) { l -= len[u] - len[v]; u = v; } } return u; }
namespace tree { #define mid ((l + r) >> 1) #define lson l, mid #define rson mid + 1, r const int MAGIC = M * 30; struct P { int sum, ls, rs; } tr[MAGIC] = {{0, 0, 0}}; int sz; int N(int sum, int ls, int rs) { if (sz == MAGIC) while(1); tr[sz] = {sum, ls, rs}; return sz++; } int ins(int o, int x, int v, int l = 1, int r = ls) { if (x < l || x > r) return o; const P& t = tr[o]; if (l == r) return N(t.sum + v, 0, 0); return N(t.sum + v, ins(t.ls, x, v, lson), ins(t.rs, x, v, rson)); } int query(int o, int ql, int qr, int l = 1, int r = ls) { if (ql > r || l > qr) return 0; const P& t = tr[o]; if (ql <= l && r <= qr) return t.sum; return query(t.ls, ql, qr, lson) + query(t.rs, ql, qr, rson); } }
namespace bit { using namespace tree; int c[M]; inline int lowbit(int x) { return x & -x; } void add(int p, int x, int v) { for (int i = p; i <= sam::sz; i += lowbit(i)) c[i] = ins(c[i], x, v); } int sum(int p, int ql, int qr) { int ret = 0; for (int i = p; i > 0; i -= lowbit(i)) ret += query(c[i], ql, qr); return ret; } inline int sum(int l, int r, int ql, int qr) { return sum(r, ql, qr) - sum(l - 1, ql, qr); } }
void mod(int p, int v) { int u = in[sam::rpos[0][p]]; bit::add(u, p + 1, v - f[p]); // + 1 f[p] = v; }
int old[M]; void q_init() { int o = 0; FOR (i, 1, sam::sz) { int u = rin[i], p = sam::pos[0][u]; if (~p) o = tree::ins(o, p + 1, f[p]); // + 1 old[i] = o; } }
int query(int a, int b, int c, int d) { c += b - a; if (c > d) return 0; int u = go_up(sam::rpos[1][b], a); ++c; ++d; // + 1 return bit::sum(in[u], out[u], c, d) + tree::query(old[out[u]], c, d) - tree::query(old[in[u] - 1], c, d); }
void _init(); int main() { #ifdef zerol freopen("in", "r", stdin); #endif using namespace sam; cin >> T; while (T--) { _init(); scanf("%s", s); ls = strlen(s); FOR (i, 0, ls) ins(s[i] - 'a', i, 0);
last = 1; scanf("%s", s); FOR (i, 0, strlen(s)) ins(s[i] - 'a', i, 1);
FOR (i, 0, ls) scanf("%d", &f[i]); FOR (i, 2, sz) G[fa[i]].push_back(i); dfs(1, 0); q_init();
cin >> Q; while (Q--) { int tp, a, b, c, d; scanf("%d", &tp); if (tp == 1) { scanf("%d%d", &a, &b); #ifndef zerol a ^= ans; b ^= ans; #endif mod(a, b); } else { scanf("%d%d%d%d", &a, &b, &c, &d); #ifndef zerol a ^= ans; b ^= ans; c ^= ans; d ^= ans; #endif ans = query(a, b, c, d); printf("%d\n", ans); } } } }
void _init() { clk = 0; tree::sz = 1; memset(sam::t, 0, sizeof sam::t); memset(sam::pos, -1, sizeof sam::pos); sam::sz = 2; sam::last = 1; memset(bit::c, 0, sizeof bit::c); FOR (i, 0, M) G[i].clear(); }
/* * state [1, sz) * dfn [1, sz) * old [1, sz) * tree_range [1, len(s)] (+1 needed) * bit_range [1, sz) */
|