QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#291127 | #2994. 制胡窜 | MoRanSky | 100 ✓ | 406ms | 144880kb | C++23 | 4.4kb | 2023-12-26 05:00:28 | 2023-12-26 05:00:29 |
Judging History
answer
// Skyqwq
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
const int N = 2e5 + 5, L = 18;
int n, q, loc[N];
char s[N];
struct SAM{
int ch[10], link, len;
} t[N];
int last = 1, idx = 1;
void inline extend(int c) {
int x = ++idx, p = last; t[x].len = t[p].len + 1;
while (p && !t[p].ch[c]) t[p].ch[c] = x, p = t[p].link;
if (!p) t[x].link = 1;
else {
int q = t[p].ch[c];
if (t[p].len + 1 == t[q].len) t[x].link = q;
else {
int y = ++idx; t[y] = t[q];
t[y].len = t[p].len + 1;
while (p && t[p].ch[c] == q) t[p].ch[c] = y, p = t[p].link;
t[q].link = t[x].link = y;
}
}
last = x;
}
vector<int> g[N];
int fa[N][L], d[N], pos[N];
LL inline get(LL l, LL r) {
return (l + r) * (r - l + 1) / 2;
}
struct Node{
int pr, sf, l, r;
LL v;
};
Node operator + (const Node &a, const Node &b) {
Node c;
c.l = a.l, c.r = b.r;
c.pr = a.pr;
if (!c.pr) c.pr = b.pr;
c.sf = b.sf;
if (!c.sf) c.sf = a.sf;
c.v = a.v + b.v;
if (a.sf) {
if (b.pr) c.v += 1ll * (b.pr - b.l) * a.sf;
else c.v += 1ll * (b.r - b.l + 1) * a.sf;
}
return c;
}
int rt[N];
namespace Seg{
#define ls t[p].l
#define rs t[p].r
struct T{
int l, r;
Node v;
} t[N * 19];
int idx = 0;
void inline pushup(int p, int l, int mid, int r) {
if (!ls) t[p].v = (Node) { 0, 0, l, mid, 0 } + t[rs].v;
else if (!rs) t[p].v = t[ls].v + (Node) { 0, 0, mid + 1, r, 0 };
else t[p].v = t[ls].v + t[rs].v;
}
void ins(int &p, int l, int r, int x) {
if (!p) p = ++idx;
if (l == r) {
t[p].v = (Node) { r, r, r, r, r };
return;
}
int mid = (l + r) >> 1;
if (x <= mid) ins(ls, l, mid, x);
else ins(rs, mid + 1, r, x);
pushup(p, l, mid, r);
}
Node query(int p, int l, int r, int x, int y) {
if (!p && x <= l && r <= y) return (Node) { 0, 0, l, r, 0 };
if (x <= l && r <= y) return t[p].v;
int mid = (l + r) >> 1;
if (y <= mid) return query(ls, l, mid, x, y);
if (x > mid) return query(rs, mid + 1, r, x, y);
return query(ls, l, mid, x, y) + query(rs, mid + 1, r, x, y);
}
void merge(int &p, int q, int l, int r) {
if (!q) return;
if (!p) {
p = q;
return;
}
int la = p;
t[p = ++idx] = t[la];
int mid = (l + r) >> 1;
merge(ls, t[q].l, l, mid);
merge(rs, t[q].r, mid + 1, r);
pushup(p, l, mid, r);
}
}
void dfs(int u) {
if (pos[u]) {
Seg::ins(rt[u], 1, n, pos[u]);
}
for (int i = 1; i < L; i++)
fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int v: g[u]) {
fa[v][0] = u;
d[v] = d[u] + 1;
dfs(v);
Seg::merge(rt[u], rt[v], 1, n);
}
}
int inline fixPos(int p, int l) {
for (int i = L - 1; ~i; i--) {
if (fa[p][i] && t[fa[p][i]].len >= l)
p = fa[p][i];
}
if (l <= t[fa[p][0]].len) p = fa[p][0];
return p;
}
int now;
Node ask(int l, int r) {
if (l > r) return Seg::t[0].v;
return Seg::query(rt[now], 1, n, l, r);
}
LL G(int l, int r) {
if (l > r) return 0;
Node u = ask(l, r);
Node a = ask(1, l - 1);
if (u.pr) u.v += (u.pr - u.l) * a.sf;
else u.v += (u.r - u.l + 1) * a.sf;
return u.v;
}
LL sm;
LL inline Ask(int x, int y, int p) {
now = p;
int l = y - x + 1;
Node u = ask(1, n);
int A = u.pr, B = u.sf;
LL ret = 0;
if (A > B - l + 1) {
ret += get(B - l + 1 - 1, A - 2);
}
int L = max(B - l + 1, A);
int R = n - 1;
Node v = ask(A + l - 1, n);
if (v.pr) R = v.pr - 1;
if (L <= R) {
ret += (A + l - 1ll) * (R - L + 1);
ret -= G(L, R);
}
return ret;
}
int main() {
read(n), read(q);
scanf("%s", s + 1);
for (int i = 1; i <= n; i++) extend(s[i] - '0'), loc[i] = last, pos[last] = i;
for (int i = 2; i <= idx; i++) g[t[i].link].pb(i);
sm = 1ll * (n - 1) * (n - 2) / 2;
dfs(1);
while (q--) {
int x, y; read(x), read(y);
int l = y - x + 1;
int p = fixPos(loc[y], l);
printf("%lld\n", sm - Ask(x, y, p));
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 2ms
memory: 12144kb
input:
50 100 00000000001701785388333333333333333333330000000000 3 6 2 7 30 39 26 31 26 34 3 10 5 9 28 39 16 31 2 4 25 30 5 10 38 40 30 35 24 30 4 7 2 9 2 6 37 40 9 22 16 16 32 40 6 11 21 26 24 39 47 50 43 46 22 28 4 9 7 10 7 50 35 35 8 10 41 48 29 48 21 39 3 5 7 9 30 31 7 9 34 39 15 29 3 5 32 33 24 41 23 ...
output:
1176 1175 1140 1176 1161 1151 1176 999 561 1176 1176 1175 1176 1176 1176 1176 1151 1176 1176 630 1176 1161 946 1176 693 1176 1176 1176 1175 1176 15 1176 1176 1151 435 495 1176 1176 1176 1176 1176 595 1176 1176 496 624 1175 1176 741 495 741 1176 1176 1176 1176 630 1176 1176 300 1176 1176 1173 1176 19...
result:
ok 100 lines
Test #2:
score: 5
Accepted
time: 0ms
memory: 12312kb
input:
300 300 0111000111000111000111000111000111000111000111000111000111003356818960290708802430213991150010990635595501751240659489792352352352352352352352352352352352352352352352352352352352352352352352352352352352352352352352352352352352352352352352350111000111000111000111000111000111000111000111000111...
output:
11628 44497 12246 44551 43710 44551 43647 43710 44110 44551 28908 44551 44551 19900 23871 37002 44551 44551 43119 44487 42702 44551 44551 28203 29161 2211 44362 9591 36048 44551 44551 22366 44551 44551 27966 44551 25173 18145 44535 44551 44551 44551 11325 31683 44551 44551 1431 44551 37002 42787 445...
result:
ok 300 lines
Test #3:
score: 5
Accepted
time: 2ms
memory: 12248kb
input:
300 300 1111111111111111111111111111111111111111111111111111111111116019044685804177317473193411274635553548254305456830610715972532354253235425323542532354253235425323542532354253235425323542532354253235425323542532354253235425323542532354253235421111111111111111111111111111111111111111111111111111...
output:
44551 39060 8385 44190 9730 44551 44551 44550 44382 44551 44551 40470 44404 14878 44551 44551 13041 44551 3403 44551 12561 19701 44542 44551 28920 44262 41896 44551 44550 34453 44551 34716 42870 44550 44551 28203 27951 44526 28680 44551 42489 24906 44551 44551 37131 44551 7750 44551 44551 33687 4455...
result:
ok 300 lines
Test #4:
score: 5
Accepted
time: 0ms
memory: 15792kb
input:
2000 3000 10111011001011101100101110110010111011001011101100101110110010111011001011101100101110110010111011001011101100101110110010111011001011101100101110110010111011001011101100101110110010111011001011101100101110110010111011001011101100101110110010111011001011101100101110110010111011001011101100...
output:
1766838 1997001 1997001 1997001 1991223 1272810 1704873 1997001 1997001 1923465 1997001 1997001 1997001 1997001 1647243 1996140 122265 1997001 1997001 380628 1997001 1590480 1997001 1718559 1997001 1504245 55611 1997001 1997001 1815705 764466 1997001 106030 1996880 631126 1997001 1368990 1964616 199...
result:
ok 3000 lines
Test #5:
score: 5
Accepted
time: 0ms
memory: 13764kb
input:
2000 3000 00110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100...
output:
1997001 786885 91378 1997001 1997001 1997001 646953 1997001 850860 419070 1983077 1997001 1100386 1997001 1985120 1993752 1997001 1997001 742371 325221 1997001 1997001 1985120 1430586 1997001 1997001 4560 25200 1997001 1984752 1997001 1699246 1296925 1115271 997578 1964360 1952901 1997001 432915 199...
result:
ok 3000 lines
Test #6:
score: 5
Accepted
time: 118ms
memory: 140844kb
input:
100000 100000 0100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100...
output:
4896156512 2092981278 4999850001 4617175426 4999850001 2925559841 4999850001 4938583085 4928869376 4999850001 4999850001 4999850001 4999850001 4919553876 4986160001 4961169590 4999850001 4829768652 4999850001 4999850001 4999150036 4999850001 4999150036 4999250028 4999850001 4999850001 4999850001 499...
result:
ok 100000 lines
Test #7:
score: 5
Accepted
time: 111ms
memory: 141112kb
input:
100000 100000 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
4999850001 4999724741 4928261480 3939457674 4999850001 4999845992 4999850001 4999850001 4935449376 4999850001 4999850001 4999850001 4999850001 2951865060 4789860920 4999850001 4999850001 3297383641 4999850001 4999850001 4999850001 4999850001 4999850001 4999850001 4999850001 4999850001 4999850001 499...
result:
ok 100000 lines
Test #8:
score: 5
Accepted
time: 119ms
memory: 138024kb
input:
100000 100000 0110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110...
output:
4991076557 2762772308 4999850001 4999850001 4999850001 3078960340 4999850001 4826721168 4915265192 4999850001 4999850001 4999850001 4999850001 4999850001 4999610880 4873826312 4981812992 4498539903 4999850001 4931123912 4999850001 4999849992 4999850001 4999850001 4999550010 4999850001 4999850001 499...
result:
ok 100000 lines
Test #9:
score: 5
Accepted
time: 117ms
memory: 141828kb
input:
100000 100000 1000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100101000100...
output:
4746721901 3562111476 4999850001 4584838905 4999850001 4999850001 4999850001 4481202810 4999850001 4999850001 4998956976 3995674456 4999850001 4996175232 4999850001 3006685885 4999850001 4999844892 4908341645 4999850001 4999850001 4999250028 4999850001 4999850001 4999850001 4999850001 4999850001 499...
result:
ok 100000 lines
Test #10:
score: 5
Accepted
time: 57ms
memory: 47104kb
input:
30000 50000 101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101...
output:
349417830 449955001 449955001 449955001 449955001 449955001 449955001 449955001 449955001 448283152 449955001 424322146 449955001 168407128 344864067 93824451 54554235 20272528 449955001 434042880 208947903 386600721 449955001 429884601 449955001 232414686 449632596 38592505 24791361 271293571 38199...
result:
ok 50000 lines
Test #11:
score: 5
Accepted
time: 54ms
memory: 49192kb
input:
30000 50000 101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100101110100...
output:
449955001 449955001 449864400 338819496 449911320 449955001 448797514 449955001 449954677 297423855 449955001 449955001 449955001 449955001 377339256 320994453 52700511 449468406 394201081 166266730 449955001 449955001 449955001 449955001 449955001 449955001 449204072 179864061 443724985 449955001 4...
result:
ok 50000 lines
Test #12:
score: 5
Accepted
time: 52ms
memory: 51020kb
input:
30000 50000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
449955001 283351518 266239350 449955001 153554050 449955001 448596225 449950815 449955001 433547145 449955001 37849350 449955001 449340615 448324272 449955001 449955001 449955001 449674875 449955001 441241776 100529110 449955001 449181855 131714565 449955001 449955001 449955001 449955001 22147840 44...
result:
ok 50000 lines
Test #13:
score: 5
Accepted
time: 90ms
memory: 144700kb
input:
100000 100000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
0 0 3 9 18 30 45 63 84 108 135 165 198 234 273 315 360 408 459 513 570 630 693 759 828 900 975 1053 1134 1218 1305 1395 1488 1584 1683 1785 1890 1998 2109 2223 2340 2460 2583 2709 2838 2970 3105 3243 3384 3528 3675 3825 3978 4134 4293 4455 4620 4788 4959 5133 5310 5490 5673 5859 6048 6240 6435 6633 ...
result:
ok 100000 lines
Test #14:
score: 5
Accepted
time: 391ms
memory: 144880kb
input:
100000 300000 0100010101000101010001010100010101000101010001010100010101000101010001010100010101000101010001010100010101000101010001010100010101000101010001010100010101000101010001010100010101000101010001010100010101000101010001010100010101000101010001010100010101000101010001010100010101000101010001...
output:
4999850001 2702411403 90001236 3464848788 1580147436 4999850001 4993949960 1176391765 775569420 4961107998 4999850001 4999850001 4999850001 4994501916 4320582525 4999850001 4244475180 4999850001 1668310966 4703713359 359696431 4951903473 1685859211 4999850001 1318539628 4999850001 4927122171 3533696...
result:
ok 300000 lines
Test #15:
score: 5
Accepted
time: 388ms
memory: 142976kb
input:
100000 300000 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
67750620 1286639628 2670087426 4999850001 4999850001 4999850001 4938848465 4386723936 4999850001 4999850001 4999850001 4051979235 4999850001 4988050776 1820367291 168627430 1796192016 2264409456 4999850001 4999850001 4999850001 4999850001 4999563776 4920526690 558398071 4999850001 4999850001 4999850...
result:
ok 300000 lines
Test #16:
score: 5
Accepted
time: 402ms
memory: 141664kb
input:
100000 300000 1001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110001001110...
output:
4999850001 4999850001 3310335028 4999850001 1170917028 4999850001 4999850001 4999850001 4999850001 3979702720 4999850001 1320080653 4989533057 4999850001 4966012512 4999850001 578187015 3148075260 3900255360 4999293485 2944140801 4906958957 631279278 4999850001 4999850001 4999850001 3224888205 49998...
result:
ok 300000 lines
Test #17:
score: 5
Accepted
time: 358ms
memory: 142644kb
input:
100000 300000 1100110111001101110011011100110111001101110011011100110111001101110011011100110111001101110011011100110111001101110011011100110111001101110011011100110111001101110011011100110111001101110011011100110111001101110011011100110111001101110011011100110111001101110011011100110111001101110011...
output:
4999850001 4016634006 4519295056 3183022578 3384382128 4999850001 3440807490 4999850001 4999850001 1792418001 3117177270 4999850001 4999850001 4893918711 1549713628 4999850001 4999850001 2537222334 2633529025 4999850001 1805374005 723501780 4956943096 4999850001 4999850001 4999850001 4999850001 4996...
result:
ok 300000 lines
Test #18:
score: 5
Accepted
time: 387ms
memory: 137616kb
input:
100000 300000 1110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011111110011...
output:
4999850001 2283224100 4999850001 4999850001 4537590085 4639012003 4999850001 4999850001 705132681 4999850001 4999850001 4999850001 3624196953 2343899278 4999850001 4999850001 4999850001 4999850001 1624129521 4999850001 4999850001 4999850001 4999850001 4999850001 818606953 4999850001 642808440 488925...
result:
ok 300000 lines
Test #19:
score: 5
Accepted
time: 406ms
memory: 143100kb
input:
100000 300000 1100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001...
output:
4828188080 4999850001 4997010656 4999850001 4895953888 1104006555 4981256657 4441159176 4984144632 334382730 1854435450 4999850001 4999643276 2131816456 4986359072 4999850001 1578910915 4992641397 4536213384 4999850001 4999850001 4999850001 4999850001 4999850001 764972055 4999850001 3663837152 24874...
result:
ok 300000 lines
Test #20:
score: 5
Accepted
time: 387ms
memory: 141060kb
input:
100000 300000 0001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101010001101...
output:
4999850001 4761703377 4999850001 4450555018 2839557726 4999850001 4965579077 4782169485 4999850001 4999850001 4999850001 4999850001 4999850001 4999850001 4999850001 3592079475 3747139165 1242137403 1935384220 4999850001 4999850001 4966741485 4999850001 3191245995 4999850001 4988045057 200730666 4234...
result:
ok 300000 lines