Submission #74682098
Source Code Expand
#include<bits/extc++.h>
#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("avx,avx2")
#include<iostream>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define coutc "\033[48;5;196m\033[38;5;15m"
#define endc "\033[0m"
#define M(_1, _2, _3, _4, NAME, ...) NAME
#define rep(...) \
M(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rep4(_, x, n, s) \
for (int _ = x; (s < 0) ? _ > n : _ < n; _ += s)
#define rep3(_, x, n) rep4(_, x, n, (x < n ? 1 : -1))
#define rep2(_, n) rep3(_, 0, n)
#define rep1(n) rep2(i, n)
#define FOR(i, a, b) for (int i=a; i<b; i++)
#define F0R(i, a) for (int i=0; i<a; i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*b/gcd(a,b)
#define mp make_pair
// #define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
// #define cin fin
// #define cout fout
// ifstream fin("word.in");
// ofstream fout("word.out");
const int inf = INT_MAX;
const int MOD = 1000000007;
double PI = 4*atan(1);
#ifdef DEBUG
string to_string(char c) { return string({c}); }
// 7
template<class... Ts>
ostream& operator<<(ostream& o, tuple<Ts...> t) {
string s = "(";
apply([&](auto&&... r) {
((s += to_string(r) + ", "), ...); }, t);
return o << s.substr(0, len(s) - 2) + ")";
}
// 3
ostream& operator<<(ostream &o, pair<auto, auto> p) {
return o << "(" << p.fi << ", " << p.se << ")";
}
// 7
template<class C, class T = typename C::value_type,
typename std::enable_if<!std::is_same<C, std::string>
::value>::type* = nullptr>
ostream& operator<<(ostream &o, C c) {
for (auto e : c) o << setw(7) << right << e;
return o << endc << endl << coutc;
}
// 7
void debug(const auto &e, const auto &... r) {
cout << coutc << e;
((cout << " " << r), ..., (cout << endc << endl));
}
#else
#define debug(...)
#endif
map<string,int> mon;
map<int,string> inp;
void _main(int tc) {
int n;
cin >> n;
vi a(n);
int i = 0;
while (i < n) cin >> a[i], i++;
vi c, l;
i = 0;
while (i < n) {
if (c.empty() || c.back() != a[i]) c.push_back(a[i]), l.push_back(1);
else l.back()++;
i++;
}
int r = (int)c.size();
vi cap(r), good(r), bad;
i = 0;
while (i < r) {
cap[i] = (l[i] >= c[i]);
good[i] = (l[i] == c[i]);
if (!good[i]) bad.push_back(i);
i++;
}
vector<pi> seg;
if (bad.empty()) seg.push_back({0, r - 1});
else {
seg.push_back({0, bad[0]});
i = 0;
while (i + 1 < (int)bad.size()) {
seg.push_back({bad[i], bad[i + 1]});
i++;
}
seg.push_back({bad.back(), r - 1});
}
vi s, w;
i = 0;
while (i < (int)seg.size()) {
int L = seg[i].f, R = seg[i].s;
int mx = cap[R] ? R : R - 1;
int p = L;
while (p <= R) {
s.push_back(c[p]);
int cur = 0;
if (cap[p] && p <= mx) cur = mx - p + 1;
w.push_back(cur);
p++;
}
s.push_back(0);
w.push_back(0);
i++;
}
reverse(s.begin(), s.end());
reverse(w.begin(), w.end());
int m = (int)s.size();
int mxs = 2 * m + 5;
vi to(mxs * 10, -1), link(mxs), len(mxs), best(mxs), cnt(m + 1), ord(mxs);
int sz = 1, last = 0;
link[0] = -1;
len[0] = 0;
auto cp = [&](int x, int y) {
int j = 0;
while (j < 10) {
to[x * 10 + j] = to[y * 10 + j];
j++;
}
};
auto nw = [&](int L) {
int id = sz++;
len[id] = L;
link[id] = -1;
best[id] = 0;
int j = 0;
while (j < 10) {
to[id * 10 + j] = -1;
j++;
}
return id;
};
auto ext = [&](int ch) {
int cur = nw(len[last] + 1);
int p = last;
while (p != -1 && to[p * 10 + ch] == -1) {
to[p * 10 + ch] = cur;
p = link[p];
}
if (p == -1) link[cur] = 0;
else {
int q = to[p * 10 + ch];
if (len[p] + 1 == len[q]) link[cur] = q;
else {
int cl = nw(len[p] + 1);
cp(cl, q);
link[cl] = link[q];
while (p != -1 && to[p * 10 + ch] == q) {
to[p * 10 + ch] = cl;
p = link[p];
}
link[q] = link[cur] = cl;
}
}
last = cur;
return cur;
};
vi pos(m);
i = 0;
while (i < m) {
pos[i] = ext(s[i]);
if (w[i] > best[pos[i]]) best[pos[i]] = w[i];
i++;
}
i = 0;
while (i < sz) cnt[len[i]]++, i++;
i = 1;
while (i <= m) cnt[i] += cnt[i - 1], i++;
i = sz - 1;
while (i >= 0) ord[--cnt[len[i]]] = i, i--;
i = sz - 1;
while (i > 0) {
int v = ord[i];
int p = link[v];
if (p >= 0 && best[v] > best[p]) best[p] = best[v];
i--;
}
ll ans = 0;
i = 1;
while (i < sz) {
int up = min(len[i], best[i]);
int lo = len[link[i]];
if (up > lo) ans += up - lo;
i++;
}
cout << ans << endl;
}
// 5
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
_main(0), exit(0);
int tc; cin >> tc; rep(i, tc) _main(i + 1);
}
Submission Info
| Submission Time |
|
| Task |
G - 221 Substring |
| User |
zaahir |
| Language |
C++23 (GCC 15.2.0) |
| Score |
600 |
| Code Size |
5919 Byte |
| Status |
AC |
| Exec Time |
221 ms |
| Memory |
171356 KiB |
Compile Error
./Main.cpp: In function 'void _main(int)':
./Main.cpp:92:16: warning: unused parameter 'tc' [-Wunused-parameter]
92 | void _main(int tc) {
| ~~~~^~
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
600 / 600 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
00-sample-01.txt, 00-sample-02.txt |
| All |
00-sample-01.txt, 00-sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt, 01-34.txt, 01-35.txt, 01-36.txt, 01-37.txt, 01-38.txt, 01-39.txt, 01-40.txt, 01-41.txt, 01-42.txt, 01-43.txt, 01-44.txt, 01-45.txt, 01-46.txt, 01-47.txt, 01-48.txt, 01-49.txt, 01-50.txt, 01-51.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 00-sample-01.txt |
AC |
1 ms |
3612 KiB |
| 00-sample-02.txt |
AC |
1 ms |
3668 KiB |
| 01-01.txt |
AC |
1 ms |
3784 KiB |
| 01-02.txt |
AC |
1 ms |
3628 KiB |
| 01-03.txt |
AC |
1 ms |
3512 KiB |
| 01-04.txt |
AC |
1 ms |
3820 KiB |
| 01-05.txt |
AC |
1 ms |
3700 KiB |
| 01-06.txt |
AC |
1 ms |
3664 KiB |
| 01-07.txt |
AC |
1 ms |
3668 KiB |
| 01-08.txt |
AC |
1 ms |
3700 KiB |
| 01-09.txt |
AC |
1 ms |
3640 KiB |
| 01-10.txt |
AC |
1 ms |
3732 KiB |
| 01-11.txt |
AC |
1 ms |
3752 KiB |
| 01-12.txt |
AC |
1 ms |
3608 KiB |
| 01-13.txt |
AC |
1 ms |
3796 KiB |
| 01-14.txt |
AC |
1 ms |
3792 KiB |
| 01-15.txt |
AC |
1 ms |
3700 KiB |
| 01-16.txt |
AC |
1 ms |
3724 KiB |
| 01-17.txt |
AC |
1 ms |
3620 KiB |
| 01-18.txt |
AC |
1 ms |
3612 KiB |
| 01-19.txt |
AC |
1 ms |
3668 KiB |
| 01-20.txt |
AC |
221 ms |
171356 KiB |
| 01-21.txt |
AC |
75 ms |
82680 KiB |
| 01-22.txt |
AC |
13 ms |
5408 KiB |
| 01-23.txt |
AC |
46 ms |
53092 KiB |
| 01-24.txt |
AC |
51 ms |
40700 KiB |
| 01-25.txt |
AC |
40 ms |
33696 KiB |
| 01-26.txt |
AC |
27 ms |
19556 KiB |
| 01-27.txt |
AC |
47 ms |
53100 KiB |
| 01-28.txt |
AC |
47 ms |
53052 KiB |
| 01-29.txt |
AC |
48 ms |
53172 KiB |
| 01-30.txt |
AC |
57 ms |
52860 KiB |
| 01-31.txt |
AC |
51 ms |
40680 KiB |
| 01-32.txt |
AC |
51 ms |
40632 KiB |
| 01-33.txt |
AC |
52 ms |
40660 KiB |
| 01-34.txt |
AC |
52 ms |
42148 KiB |
| 01-35.txt |
AC |
38 ms |
33688 KiB |
| 01-36.txt |
AC |
39 ms |
33596 KiB |
| 01-37.txt |
AC |
39 ms |
33608 KiB |
| 01-38.txt |
AC |
44 ms |
35292 KiB |
| 01-39.txt |
AC |
27 ms |
19588 KiB |
| 01-40.txt |
AC |
26 ms |
19556 KiB |
| 01-41.txt |
AC |
26 ms |
19588 KiB |
| 01-42.txt |
AC |
27 ms |
20272 KiB |
| 01-43.txt |
AC |
1 ms |
3624 KiB |
| 01-44.txt |
AC |
1 ms |
3656 KiB |
| 01-45.txt |
AC |
1 ms |
3584 KiB |
| 01-46.txt |
AC |
2 ms |
4076 KiB |
| 01-47.txt |
AC |
2 ms |
3872 KiB |
| 01-48.txt |
AC |
2 ms |
3988 KiB |
| 01-49.txt |
AC |
40 ms |
32044 KiB |
| 01-50.txt |
AC |
32 ms |
24572 KiB |
| 01-51.txt |
AC |
43 ms |
33620 KiB |