Address
https://www.luogu.org/problemnew/show/P3823
https://www.lydsy.com/JudgeOnline/problem.php?id=4943
https://loj.ac/problem/2303
http://uoj.ac/problem/315
Solution
看到合并和分裂操作,考虑使用一个双向链表维护队列。
看到询问,考虑使用一个Hash 表,存下所有长度
的子串的 Hash 值。
合并两个队列时,只需要枚举一遍所有的长度
的、起始位置在第一个队列,结束位置在第二个队列的子串后,把这些子串插入 Hash 表。
分裂一个队列时,枚举一遍所有的长度
且跨越过分裂位置的子串,把这些子串在 Hash 表中删掉。
查询时,枚举
的所有长度为
的子串,求这些子串的出现次数并相乘。
看上去,每次加入的子串最多有
个,复杂度无法通过。
于是我们来分析下复杂度:
所有队列中的字符个数之和为
,故为
的子串个数最多有
个。分裂操作最多
次,于是我们最多删掉
个不同的子串,也最多只会加入
个而不是
个子串。
于是复杂度
。
Code
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define For(i, a, b) for (i = a; i <= b; i++)
#define Rof(i, a, b) for (i = a; i >= b; i--)
#define Edge(u) for (int e = adj[u]; e; e = nxt[e])
using namespace std;
inline int read() {
int res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
typedef unsigned long long ull;
const int N = 2e5 + 5, PYZ = 19260817, M = 1e7 + 7, E = 55,
Z = 126e5 + 5, ZZQ = 998244353;
int n, m, ecnt, a[N], pre[Z], nxt[Z], adj[PYZ + 17], cnt[Z], le[N], ri[N];
ull has[M], hasl[E], hasr[E], go[Z], pw[E];
char s[M];
int query(ull has) {
int u = (int) (has % PYZ);
Edge(u) if (go[e] == has) return e;
return -1;
}
void ins(ull has) {
int e;
if ((e = query(has)) != -1) return (void) (cnt[e]++);
int u = (int) (has % PYZ); ecnt++;
if (adj[u]) pre[adj[u]] = ecnt; nxt[ecnt] = adj[u];
adj[u] = ecnt; go[ecnt] = has; cnt[ecnt] = 1;
}
void del(ull has) {
int e = query(has);
if (cnt[e] > 1) return (void) (cnt[e]--);
if (!pre[e]) adj[(int) (has % PYZ)] = nxt[e];
else nxt[pre[e]] = nxt[e];
if (nxt[e]) pre[nxt[e]] = pre[e];
}
int queryt(ull has) {
int e = query(has);
return e == -1 ? 0 : cnt[e];
}
void mer(int x, int y) {
ri[x] = y; le[y] = x;
int i, j, tt = 1, ff = 1;
hasl[tt] = a[x]; hasr[ff] = a[y];
while (le[x] && tt < 49) x = le[x], tt++,
hasl[tt] = hasl[tt - 1] + pw[tt - 1] * a[x];
while (ri[y] && ff < 49) y = ri[y], ff++,
hasr[ff] = hasr[ff - 1] * 7 + a[y];
For (i, 1, tt) For (j, 1, min(50 - i, ff))
ins(hasl[i] * pw[j] + hasr[j]);
}
void spl(int x) {
int y = ri[x];
ri[x] = le[y] = 0;
int i, j, tt = 1, ff = 1;
hasl[tt] = a[x]; hasr[ff] = a[y];
while (le[x] && tt < 49) x = le[x], tt++,
hasl[tt] = hasl[tt - 1] + pw[tt - 1] * a[x];
while (ri[y] && ff < 49) y = ri[y], ff++,
hasr[ff] = hasr[ff - 1] * 7 + a[y];
For (i, 1, tt) For (j, 1, min(50 - i, ff))
del(hasl[i] * pw[j] + hasr[j]);
}
int query(int len, int k) {
int i, res = 1;
For (i, 1, len) has[i] = has[i - 1] * 7 + s[i] - '0';
For (i, 1, len - k + 1)
res = 1ll * res * queryt(has[i + k - 1] - has[i - 1] * pw[k])
% ZZQ;
return res;
}
int main() {
int i, op, x, y;
n = read(); m = read();
pw[0] = 1;
For (i, 1, n) a[i] = read();
For (i, 1, 50) pw[i] = pw[i - 1] * 7;
For (i, 1, n) ins(a[i]);
while (m--) {
op = read();
if (op == 1) x = read(), y = read(), mer(x, y);
else if (op == 2) x = read(), spl(x);
else scanf("%s", s + 1), x = read(),
printf("%d\n", query(strlen(s + 1), x));
}
return 0;
}