P5112 FZOUTSY 题解
由于
两个后缀的最长公共前缀
于是变成了经典的类似区间数颜色的问题,使用莫队,块长为
有点卡哈希模数,但 unsigned long long
自然溢出过了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
using ull = unsigned long long;
const int N = 3e6 + 5, M = 1e5 + 5;
int n, m, k;
char s[N];
int len, bel[N], cnt[N];
long long res, ans[M];
ull hashing[N];
ull ec[N];
int nec[N];
vector<ull> v;
struct Query
{
int l, r, id;
Query(int _l, int _r, int _i) : l(_l), r(_r), id(_i) {}
Query() {}
bool operator<(const Query& g) const
{
return (bel[l] ^ bel[g.l]) ? bel[l] < bel[g.l] : (bel[l] & 1) ? r < g.r : r > g.r;
}
}p[M];
ull qpow(ull a, ull b)
{
ull res = 1, base = a;
while (b)
{
if (b & 1)
{
res = res * base;
}
base *= base;
b >>= 1LL;
}
return res;
}
struct FastIO {
static const int S = 1e7;
int wpos;
char wbuf[S];
FastIO() : wpos(0) {}
inline int xchar() {
static char buf[S];
static int len = 0, pos = 0;
if (pos == len)
pos = 0, len = fread(buf, 1, S, stdin);
if (pos == len) exit(0);
return buf[pos++];
}
inline int xuint() {
int c = xchar(), x = 0;
while (c <= 32) c = xchar();
for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
return x;
}
inline int xint()
{
int c = xchar(), x = 0;
while (c <= 32) c = xchar();
for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
return x;
}
inline void xstring(char* s)
{
int c = xchar();
while (c <= 32) c = xchar();
for (; c > 32; c = xchar()) *s++ = c;
*s = 0;
}
inline void wchar(int x)
{
if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
wbuf[wpos++] = x;
}
inline void wint(long long x)
{
char s[24];
int n = 0;
while (x || !n) s[n++] = '0' + x % 10, x /= 10;
while (n--) wchar(s[n]);
wchar('\n');
}
inline void wstring(const char* s)
{
while (*s) wchar(*s++);
}
~FastIO()
{
if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
}
} io;
int main()
{
n = io.xint(), m = io.xint(), k = io.xint();
io.xstring(s);
len = max(1, (int)(n / sqrt(m)));
for (int i = 1; i <= n; i++)
{
hashing[i] = hashing[i - 1] * 27 + (s[i - 1] - 'a');
}
ull pp = qpow(27, k);
for (int i = 1; i + k - 1 <= n; i++)
{
ull subseq_hash = (hashing[i + k - 1] - hashing[i - 1] * pp);
ec[i] = subseq_hash;
v.emplace_back(ec[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i + k - 1 <= n; i++)
{
nec[i] = lower_bound(v.begin(), v.end(), ec[i]) - v.begin() + 1;
}
for (int i = 1; i <= m; i++)
{
int l, r;
l = io.xint(), r = io.xint();
r = min(r, n - k + 1);
p[i] = Query(l, r, i);
}
sort(p + 1, p + m + 1);
int nl(1), nr(0);
for (int i = 1; i <= m; i++)
{
if (p[i].l > p[i].r || p[i].l > n - k + 1)
{
ans[p[i].id] = 0;
continue;
}
int l = p[i].l, r = p[i].r;
while (nr < r) res += cnt[nec[++nr]]++;
while (nl > l) res += cnt[nec[--nl]]++;
while (nl < l) res -= --cnt[nec[nl++]];
while (nr > r) res -= --cnt[nec[nr--]];
ans[p[i].id] = res;
}
for (int i = 1; i <= m; i++) io.wint(ans[i]);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现