QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#92478 | #21648. Runs | zghtyarecrenj# | 100 ✓ | 657ms | 217656kb | C++14 | 12.0kb | 2023-03-30 17:09:40 | 2023-03-30 17:09:43 |
Judging History
answer
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
namespace standard_utilities {
template <typename Ty>
inline Ty max(const Ty &a, const Ty &b) {
return a > b ? a : b;
}
template <typename Ty>
inline Ty min(const Ty &a, const Ty &b) {
return a < b ? a : b;
}
template <typename Ty>
inline void check_max(Ty &a, const Ty &b) {
if (b > a)
a = b;
}
template <typename Ty>
inline void check_min(Ty &a, const Ty &b) {
if (b < a)
a = b;
}
template <typename Ty>
inline void swap(Ty &a, Ty &b) {
Ty c = a;
a = b, b = c;
}
} // namespace standard_utilities
using namespace standard_utilities;
namespace Fast_IO {
const int MaxBuff = 1 << 15;
const int MaxOut = 1 << 24;
char b[MaxBuff], *S = b, *T = b;
char buff[MaxOut], *iter = buff;
inline void flush() {
fwrite(buff, 1, iter - buff, stdout), iter = buff;
}
inline char get_char() {
return (S == T && (T = (S = b) + fread(b, 1, MaxBuff, stdin), S == T) ? 0 : *S++);
}
inline void put_char(char c) {
if (iter - buff >= 1 << 24 - 1)
flush();
*iter++ = c;
}
struct Fast_IO_template {
~Fast_IO_template() {
flush();
}
inline Fast_IO_template &operator>>(char &c) {
c = get_char();
return *this;
}
inline Fast_IO_template &operator<<(char c) {
put_char(c);
return *this;
}
inline Fast_IO_template &operator>>(char *s) {
char *iter = s;
while (*iter = get_char(), *iter == ' ' || *iter == '\n' || *iter == '\r');
while (*++iter = get_char(), *iter && *iter != ' ' && *iter != '\n' && *iter != '\r');
*iter = 0;
return *this;
}
inline Fast_IO_template &operator<<(int x) {
static int stack[110];
int O = 0;
if (!x)
put_char('0');
else {
if (x < 0)
x = -x, put_char('-');
for (; x; x /= 10)
stack[++O] = x % 10;
for (; O; put_char('0' + stack[O--]));
}
return *this;
}
};
} // namespace Fast_IO
Fast_IO::Fast_IO_template io;
const int N = 1e6 + 10;
struct my_tuple {
int x, y, z;
inline bool operator < (const my_tuple &b) const {
if (x != b.x)
return x < b.x;
if (y != b.y)
return y < b.y;
return z < b.z;
}
inline int operator[](const int b) {
if (b == 0)
return x;
if (b == 1)
return y;
if (b == 2)
return z;
}
inline bool operator == (const my_tuple &b) {
return x == b.x && y == b.y && z == b.z;
}
inline bool operator != (const my_tuple &b) {
return x != b.x || y != b.y || z != b.z;
}
};
struct my_pair {
int x, y;
inline int &operator[](const int b) {
return b == 0 ? x : y;
}
};
namespace SAIS {
#define alloc(x, type, len) type *x = new type[len];
void induced_sort(int n, int m, int *s, bool *T, int *sa, int *cnt) {
static int lptr[N], rptr[N];
int tmp;
lptr[0] = rptr[0] = 0;
for (int i = 1; i <= m; ++i)
lptr[i] = cnt[i - 1], rptr[i] = cnt[i] - 1;
for (int i = 0; i <= n; ++i)
sa[i] > 0 && !T[tmp = sa[i] - 1] ? sa[lptr[s[tmp]]++] = tmp : 0;
for (int i = n; i >= 0; --i)
sa[i] > 0 && T[tmp = sa[i] - 1] ? sa[rptr[s[tmp]]--] = tmp : 0;
}
bool compare(int *x, int *y, int n) {
while (n--)
if (*x++ != *y++)
return 1;
return 0;
}
void sais(int n, int m, int *s, int *sa) {
--n;
alloc(T, bool, n + 10);
alloc(cnt, int, m + 10);
alloc(lms, int, n + 10);
alloc(tmp, int, n + 10);
for (int i = n; i >= 0; --i) {
T[i] = (i == n || s[i] < s[i + 1] || s[i] == s[i + 1] && T[i + 1]);
++cnt[s[i]];
}
tmp[0] = 0;
for (int i = 1; i <= m; ++i)
cnt[i] += cnt[i - 1], tmp[i] = cnt[i] - 1;
int LMS = 0;
for (int i = 1; i <= n; ++i)
T[i] && !T[i - 1] ? lms[LMS++] = i : 0;
memset(sa, -1, (n + 1) << 2);
for (int i = 0; i < LMS; ++i)
sa[tmp[s[lms[i]]]--] = lms[i];
induced_sort(n, m, s, T, sa, cnt);
alloc(len, int, n + 10);
lms[LMS] = n + 1;
for (int i = 0; i < LMS; ++i)
len[lms[i]] = lms[i + 1] - lms[i] + 1;
alloc(lab, int, n + 10);
int m0 = 0;
memset(lab, -1, (n + 1) << 2);
for (int i = 1, p = -1, q = -1; i <= n; ++i)
if ((q = sa[i]) > 0 && T[q] && !T[q - 1]) {
if (p == -1 || len[p] != len[q] || compare(s + p, s + q, len[p]))
++m0;
lab[q] = m0;
p = q;
}
lab[n] = 0;
alloc(s0, int, LMS + 10);
alloc(sa0, int, LMS + 10);
for (int i = 0, p = 0; i <= n; ++i)
lab[i] >= 0 ? s0[p++] = lab[i] : 0;
if (m0 + 1 == LMS)
for (int i = 0; i < LMS; ++i)
sa0[s0[i]] = i;
else
sais(LMS, m0, s0, sa0);
tmp[0] = 0;
for (int i = 1; i <= m; ++i)
tmp[i] = cnt[i] - 1;
memset(sa, -1, (n + 1) << 2);
for (int i = LMS - 1; i >= 0; --i)
sa[tmp[s[lms[sa0[i]]]]--] = lms[sa0[i]];
induced_sort(n, m, s, T, sa, cnt);
}
} // namespace SAIS
inline int log2n(int n) {
return 32 - __builtin_clz(n - 1);
}
namespace RMQ_table {
const int N = 2e6 + 20;
const int D_max = 21;
const int L = 20;
const int inf = 1 << 30;
int stack[L + 1];
template <typename T, int MAXN>
class ST {
private:
int f[N / L][D_max], M[N / L], a[N + L], n, q, D;
struct node {
int state[L + 1], *a;
int Qmin(int x, int y) {
return a[x + __builtin_ctz(state[y] >> x)];
}
void init(int *_a) {
int top = 0;
a = _a;
for (int i = 1; i <= L; ++i) {
state[i] = state[i - 1];
while (top && a[i] <= a[stack[top]])
state[i] -= 1 << stack[top], --top;
stack[++top] = i;
state[i] += 1 << i;
}
}
} c[N / L];
public:
void build(int _n, int *_a) {
n = _n;
for (int i = 1; i <= _n; ++i)
a[i] = _a[i];
D = int(log2(n)) + 1;
int nn = n / L;
M[0] = -1;
for (int i = 1; i <= nn; ++i) {
f[i][0] = inf;
for (int j = 1; j <= L; ++j)
f[i][0] = min(f[i][0], a[(i - 1) * L + j]);
}
for (int i = 1; i <= nn; ++i)
M[i] = !(i & (i - 1)) ? M[i - 1] + 1 : M[i - 1];
for (int j = 1; j <= D; ++j)
for (int i = 1; i <= nn - (1 << j) + 1; ++i)
f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
for (int i = 1; i <= nn + 1; ++i)
c[i].init(a + (i - 1) * L);
}
inline int Qmin_ST(int x, int y) {
int z = M[y - x + 1];
return min(f[x][z], f[y - (1 << z) + 1][z]);
}
inline int rmq(int x, int y) {
++x, ++y;
int xx = (x - 1) / L + 1, yy = (y - 1) / L + 1, res;
if (xx + 1 <= yy - 1)
res = Qmin_ST(xx + 1, yy - 1);
else
res = inf;
if (xx == yy)
res = min(res, c[xx].Qmin(x - (xx - 1) * L, y - (yy - 1) * L));
else
res = min(res, c[xx].Qmin(x - (xx - 1) * L, L)), res = min(res, c[yy].Qmin(1, y - (yy - 1) * L));
return res;
}
};
} // namespace RMQ_table
using RMQ_table::ST;
template <int N, template <typename, int> class RMQ_T>
class SA {
public:
RMQ_T < int, N - 1 > rmq;
int n, sa[N], rk[N], height[N];
void build(int _n, char *str) {
n = _n;
alloc(s, int, n + 10);
for (int i = 0; i < n; ++i)
s[i] = str[i];
SAIS::sais(n + 1, 26, s, sa);
for (int i = 1; i <= n; ++i)
rk[sa[i]] = i;
for (int k = 0, i = 0, j; i < n; height[rk[i++]] = k)
for (k ? k-- : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; ++k);
rmq.build(n, height);
}
int lcp(int i, int j) {
//printf("lcp(%d,%d)\n", i, j);
if (i == j)
return n - i;
if (j == n)
return 0;
i = rk[i], j = rk[j];
if (i > j)
std::swap(i, j);
return rmq.rmq(i, j - 1);
}
};
template <int N, template <typename, int> class RMQ_T>
class ISA {
public:
RMQ_T < int, N - 1 > rmq;
int n, sa[N], rk[N], height[N];
void build(int _n, char *str) {
n = _n;
alloc(s, int, n + 10);
for (int i = 0; i < n; ++i)
s[i] = str[n - i - 1];
SAIS::sais(n + 1, 26, s, sa);
for (int i = 1; i <= n; ++i)
rk[sa[i]] = i;
for (int k = 0, i = 0, j; i < n; height[rk[i++]] = k)
for (k ? k-- : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; ++k);
rmq.build(n, height);
//for (int i = 2; i <= n + 1; ++i) printf("%d ", height[i]); printf("\n");
//for (int i = 0; i < n; ++i) printf("%d ", rk[i]); printf("\n");
}
int lcs(int i, int j) {
//++i, ++j;
if (i <= 0 || j <= 0)
return 0;
if (i == j)
return i;
i = rk[n - i], j = rk[n - j];
if (i > j)
std::swap(i, j);
//printf("rmq(%d,%d)", i, j);
return rmq.rmq(i, j - 1);
}
};
int n, nsv[N], nbv[N];
char s[N];
SA<N, ST> sa;
ISA<N, ST> isa;
std::vector<my_tuple> runs;
int get_lcs(int i, int j) {
int low = 0, high = min(i, j);
while (low < high) {
int middle = low + high + 1 >> 1;
if (sa.lcp(i - middle, j - middle) >= middle)
low = middle;
else
high = middle - 1;
}
//printf("lcs(%d,%d) : %d %d\n", i, j, isa.lcs(i, j), low);
return low;
}
void add_run(int i, int j) {
int length = j - i;
int lcs = isa.lcs(i, j);
if (lcs < length) {
int lcp = sa.lcp(i, j);
if (lcs + lcp >= length)
runs.push_back((my_tuple) {
i - lcs + 1, j + lcp, length
});
}
}
std::vector<int> a[N], a2[N], a3[N];
int b[N], c[N];
void my_sort_and_unique() {
int m = runs.size();
for (int i = 0; i < m; ++i)
a[runs[i].z].push_back(i);
for (int i = 1, tmp = 0; i <= n; ++i)
for (int j = 0; j < a[i].size(); ++j)
b[tmp++] = a[i][j];
for (int i = 0; i < m; ++i)
a2[runs[b[i]].y].push_back(b[i]);
for (int i = 1, tmp = 0; i <= n; ++i)
for (int j = 0; j < a2[i].size(); ++j)
b[tmp++] = a2[i][j];
for (int i = 0; i < m; ++i)
a3[runs[b[i]].x].push_back(b[i]);
for (int i = 1, tmp = 0; i <= n; ++i)
for (int j = 0; j < a3[i].size(); ++j)
b[tmp++] = a3[i][j];
int tmp = 0;
c[0] = b[0];
for (int i = 1; i < m; ++i)
if (runs[b[i - 1]] != runs[b[i]])
c[++tmp] = b[i];
io << tmp + 1 << '\n';
for (int i = 0; i <= tmp; ++i)
io << runs[c[i]].x << ' ' << runs[c[i]].y << ' ' << runs[c[i]].z << '\n';
}
int main() {
io >> s;
n = strlen(s);
for (int i = 0; i < n; ++i)
s[i] = s[i] - 'a' + 1;
sa.build(n, s);
isa.build(n, s);
for (int i = n; i--;) {
for (int &j = nsv[i] = i + 1; j < n && sa.rk[i] < sa.rk[j];)
j = nsv[j];
for (int &j = nbv[i] = i + 1; j < n && sa.rk[i] > sa.rk[j];)
j = nbv[j];
add_run(i, nsv[i]);
if (nsv[i] != nbv[i])
add_run(i, nbv[i]);
}
std::sort(runs.begin(), runs.end());
runs.erase(std::unique(runs.begin(), runs.end()), runs.end());
io << (int)runs.size() << '\n';
for (auto a : runs)
io << a.x << ' ' << a.y << ' ' << a.z << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 91ms
memory: 129304kb
input:
luynenolgrtkiqkdbpcoqsjyisilepmrkleqytdfmxrupcartchjwqhekspohcftpmkpfnwtmoqesqxluevqwewgylwgpdbgplwwbsqpigtayqmswjksngymtwulavrpjpomiedsmxtmpheoqogfwhtpdnaflsxwhlkrrnkfmfrcmvqelyjhfdylszqftndcynurbgwnnnrbkjfxioeptfaoervxgzzhovypdvfsiytviysqpzhkeiyibwhjviqjgpblmieugxroykgnloxpyyzzugirqbcwqdicloyulqij...
output:
7394 99 100 1 164 165 1 200 202 1 222 223 1 277 278 1 279 280 1 334 335 1 406 407 1 410 411 1 412 413 1 421 422 1 434 435 1 458 459 1 494 495 1 559 560 1 564 565 1 566 567 1 577 578 1 624 625 1 633 634 1 704 705 1 706 707 1 710 711 1 749 750 1 769 770 1 797 798 1 800 801 1 858 859 1 866 867 1 868 86...
result:
ok 7395 lines
Test #2:
score: 10
Accepted
time: 17ms
memory: 125772kb
input:
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...
output:
1 1 200000 1
result:
ok 2 lines
Test #3:
score: 10
Accepted
time: 76ms
memory: 131296kb
input:
bbccaaababbbbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccaaaba...
output:
102706 1 2 1 1 27 11 1 70 27 1 183 70 1 479 183 1 1254 479 1 3283 1254 1 8595 3283 1 22502 8595 1 58911 22502 1 154231 58911 3 4 1 5 7 1 7 10 2 10 13 1 12 43 16 12 113 43 12 296 113 12 775 296 12 2029 775 12 5312 2029 12 13907 5312 12 36409 13907 12 95320 36409 12 200000 95320 14 15 1 16 18 1 18 21 ...
result:
ok 102707 lines
Test #4:
score: 10
Accepted
time: 59ms
memory: 128424kb
input:
dcbaabcdabcddcbaabcdabcddcbadcbaabcdabcddcbaabcdabcddcbadcbaabcdabcddcbadcbaabcdabcddcbaabcdabcddcbadcbaabcdabcddcbaabcdabcddcbadcbaabcdabcddcbadcbaabcdabcddcbaabcdabcddcbadcbaabcdabcddcbadcbaabcdabcddcbaabcdabcddcbadcbaabcdabcddcbaabcdabcddcbadcbaabcdabcddcbadcbaabcdabcddcbaabcdabcddcbadcbaabcdabcd...
output:
69092 1 28 12 1 72 28 1 188 72 1 492 188 1 1288 492 1 3372 1288 1 8828 3372 1 23112 8828 1 60508 23112 1 158412 60508 4 5 1 5 12 4 12 13 1 13 44 16 13 116 44 13 304 116 13 796 304 13 2084 796 13 5456 2084 13 14284 5456 13 37396 14284 13 97904 37396 13 200000 97904 16 17 1 17 24 4 24 25 1 25 32 4 29 ...
result:
ok 69093 lines
Test #5:
score: 10
Accepted
time: 4ms
memory: 112076kb
input:
dcbaabcdabcddcbaabcdabcddcbadcbaabcdabcddcbaabcdabcddcbadcbaabcdabcddcbadcbaabcdabcddcbaabcdabcddcba
output:
31 1 28 12 1 72 28 4 5 1 5 12 4 12 13 1 13 44 16 13 100 44 16 17 1 17 24 4 24 25 1 25 32 4 29 56 12 32 33 1 33 40 4 40 41 1 41 88 16 44 45 1 45 52 4 52 53 1 53 60 4 60 61 1 61 68 4 68 69 1 69 76 4 73 100 12 76 77 1 77 84 4 84 85 1 88 89 1 89 96 4 96 97 1
result:
ok 32 lines
Test #6:
score: 10
Accepted
time: 59ms
memory: 133176kb
input:
bbabaababaababbbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbaba...
output:
140058 1 2 1 1 34 15 1 87 34 1 227 87 1 594 227 1 1555 594 1 4071 1555 1 10658 4071 1 27903 10658 1 73051 27903 1 191250 73051 2 5 2 2 14 5 3 8 3 5 6 1 6 10 2 8 13 3 10 11 1 11 14 2 14 17 1 15 53 19 15 140 53 15 367 140 15 961 367 15 2516 961 15 6587 2516 15 17245 6587 15 45148 17245 15 118199 45148...
result:
ok 140059 lines
Test #7:
score: 10
Accepted
time: 657ms
memory: 196920kb
input:
ghbaahxvveloseywqeuokpraqgyscdcimfioohjjnwyhdyftczjorapjhncjqetfjetxnfidbgkeesajrjxkkmutayescxxndtmryuubdgijyisstqefcayeycxccwpfpdypautdbbmblfvexakzakgzpfdrdtytyzkytfdwgqarxvyuvivysbzhgcakxbfwarwvxaufsuzprxjnhenbimlqoncmojkqbgoaiifaegpvdakmhxoplzfamkodtatwghwprerxkhtqhfcofqfqrnsgxgjsgcppmlgfrxdceyuo...
output:
38316 4 5 1 8 9 1 36 37 1 39 40 1 76 77 1 84 85 1 94 95 1 102 103 1 111 112 1 124 125 1 137 138 1 158 161 2 229 230 1 273 276 2 287 288 1 342 344 1 352 353 1 412 413 1 449 450 1 458 459 1 463 464 1 514 516 1 564 565 1 588 589 1 621 622 1 630 631 1 653 654 1 678 679 1 682 683 1 689 690 1 776 779 1 78...
result:
ok 38317 lines
Test #8:
score: 10
Accepted
time: 95ms
memory: 166572kb
input:
pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp...
output:
1 1 999998 1
result:
ok 2 lines
Test #9:
score: 10
Accepted
time: 619ms
memory: 202788kb
input:
aababbbbccabbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccabbccaaababbbbccaaababbbbccabbccaaababbbbccaaaba...
output:
513547 1 2 1 1 43 16 1 113 43 1 296 113 1 775 296 1 2029 775 1 5312 2029 1 13907 5312 1 36409 13907 1 95320 36409 1 249551 95320 1 653333 249551 2 5 2 5 8 1 7 16 5 9 10 1 12 13 1 12 70 27 12 183 70 12 479 183 12 1254 479 12 3283 1254 12 8595 3283 12 22502 8595 12 58911 22502 12 154231 58911 12 40378...
result:
ok 513548 lines
Test #10:
score: 10
Accepted
time: 592ms
memory: 217656kb
input:
aababaababbbbabbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbabaababaababbbbabaababaababbbbabbbaba...
output:
700312 1 2 1 1 10 5 1 53 19 1 140 53 1 367 140 1 961 367 1 2516 961 1 6587 2516 1 17245 6587 1 45148 17245 1 118199 45148 1 309449 118199 1 810148 309449 2 6 2 4 9 3 6 7 1 7 10 2 8 17 5 10 13 1 11 19 4 15 17 1 15 87 34 15 227 87 15 594 227 15 1555 594 15 4071 1555 15 10658 4071 15 27903 10658 15 730...
result:
ok 700313 lines