#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
Tran The Bao
CTL - Da Lat
Cay ngay cay dem nhung deo duoc cong nhan
*/
const int oo = 1e9;
const int T = 4e5 + 5;
struct MinSegtree {
int tr[T];
void upd(int i, int l, int r, int q, int v) {
if (r < q || l > q) return;
if (l == r) {
tr[i] = v;
return;
}
int mid = l + (r - l) / 2;
upd(i * 2, l, mid, q, v);
upd(i * 2 + 1, mid + 1, r, q, v);
tr[i] = min(tr[i * 2], tr[i * 2 + 1]);
}
int get(int i, int l, int r, int ql, int qr) {
if (r < ql || l > qr) return oo;
if (ql <= l && r <= qr) return tr[i];
int mid = l + (r - l) / 2;
return min(get(i * 2, l, mid, ql, qr), get(i * 2 + 1, mid + 1, r, ql, qr));
}
};
struct Mergetree {
vi tr[T];
void add(int i, int l, int r, int q, int v) {
if (r < q || l > q) return;
tr[i].pb(v);
if (l == r) return;
int mid = l + (r - l) / 2;
add(i * 2, l, mid, q, v);
add(i * 2 + 1, mid + 1, r, q, v);
}
void dfs(int i, int l, int r) {
sort(all(tr[i]));
if (l == r) return;
int mid = l + (r - l) / 2;
dfs(i * 2, l, mid);
dfs(i * 2 + 1, mid + 1, r);
}
int get(int i, int l, int r, int ql, int qr, int v) {
if (r < ql || l > qr) return 0;
if (ql <= l && r <= qr) {
if (tr[i].empty() || tr[i].back() < v) return 0;
int tmp = lb(all(tr[i]), v) - tr[i].begin();
return sz(tr[i]) - tmp;
}
int mid = l + (r - l) / 2;
return get(i * 2, l, mid, ql, qr, v) + get(i * 2 + 1, mid + 1, r, ql, qr, v);
}
};
const pi M = {1e9 + 7, 1e9 + 9};
const pi base = {37, 127};
const int N = 1e5 + 5;
int n;
pi P[N];
str s;
pi q[N];
namespace subtask12 {
int p[N];
str a1[N];
set<pi> a2[N];
int getp(int i) { return i == p[i]? i : p[i] = getp(p[i]); }
int join(int x, int y) {
x = getp(x);
y = getp(y);
if (sz(a2[x]) < sz(a2[y])) swap(a2[x], a2[y]);
EACH(i, a2[y]) a2[x].insert(i);
int m1 = sz(a1[x]);
int m2 = sz(a1[y]);
vpi h(m1 + m2 + 2), h1(m1 + m2 + 2);
a1[x] += a1[y];
auto get = [&](int l, int r) -> pi {
pi rs;
rs.st = (h[r].st - 1LL * (l == 0? 0 : h[l - 1].st) * P[r - l + 1].st % M.st + M.st) % M.st;
rs.nd = (h[r].nd - 1LL * (l == 0? 0 : h[l - 1].nd) * P[r - l + 1].nd % M.nd + M.nd) % M.nd;
return rs;
};
auto get1 = [&](int l, int r) -> pi {
pi rs;
rs.st = (h1[l].st - 1LL * h1[r + 1].st * P[r - l + 1].st % M.st + M.st) % M.st;
rs.nd = (h1[l].nd - 1LL * h1[r + 1].nd * P[r - l + 1].nd % M.nd + M.nd) % M.nd;
return rs;
};
pi cur = {0, 0};
FOR(i, 0, m1 + m2 - 1) {
cur.st = (1LL * cur.st * base.st % M.st + a1[x][i] - '0' + 1) % M.st;
cur.nd = (1LL * cur.nd * base.nd % M.nd + a1[x][i] - '0' + 1) % M.nd;
h[i] = cur;
}
cur = {0, 0};
FOS(i, m1 + m2 - 1, 0) {
cur.st = (1LL * cur.st * base.st % M.st + a1[x][i] - '0' + 1) % M.st;
cur.nd = (1LL * cur.nd * base.nd % M.nd + a1[x][i] - '0' + 1) % M.nd;
h1[i] = cur;
}
FOR(i, 0, m1 - 1)
FOR(j, m1, m1 + m2 - 1) {
pi tmp = get(i, j);
if (tmp == get1(i, j)) a2[x].insert(tmp);
}
p[y] = x;
return sz(a2[x]);
}
void solve() {
FOR(i, 1, n) {
p[i] = i;
a1[i].pb(s[i]);
a2[i].insert({s[i] - '0' + 1, s[i] - '0' + 1});
}
FOR(i, 1, n - 1) {
int a = q[i].st, b = q[i].nd;
cout << join(a, b) << '\n';
}
}
}
namespace subtask3 {
pi h[N], h1[N];
int sa[N], d[N], d1[N];
pi get(int l, int r) {
pi rs;
rs.st = (h[r].st - 1LL * h[l - 1].st * P[r - l + 1].st % M.st + M.st) % M.st;
rs.nd = (h[r].nd - 1LL * h[l - 1].nd * P[r - l + 1].nd % M.nd + M.nd) % M.nd;
return rs;
}
pi get1(int l, int r) {
pi rs;
rs.st = (h1[l].st - 1LL * h1[r + 1].st * P[r - l + 1].st % M.st + M.st) % M.st;
rs.nd = (h1[l].nd - 1LL * h1[r + 1].nd * P[r - l + 1].nd % M.nd + M.nd) % M.nd;
return rs;
}
int lcp(int a, int b) {
int l = 1, r = min(n - a + 1, n - b + 1), ans = 0;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
if (get(a, a + mid - 1) == get(b, b + mid - 1)) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
return ans;
}
bool cmp(int a, int b) {
int ans = lcp(a, b);
if (ans == n - a + 1) return 1;
if (ans == n - b + 1) return 0;
return s[a + ans] < s[b + ans];
}
int LB(int a, int b) {
int l = 1, r = n, ans = -1;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
int tmp = lcp(sa[mid], l);
if (tmp < b - a + 1) {
if (tmp == n - sa[mid] + 1) l = mid + 1;
else if (s[sa[mid] + tmp] < s[a + mid]) l = mid + 1;
else r = mid - 1;
}
else {
ans = mid;
r = mid - 1;
}
}
return ans;
}
int UB(int a, int b) {
int l = 1, r = n, ans = -1;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
int tmp = lcp(sa[mid], l);
if (tmp < b - a + 1) {
if (tmp == n - sa[mid] + 1) l = mid + 1;
else if (s[sa[mid] + tmp] < s[a + mid]) l = mid + 1;
else r = mid - 1;
}
else {
ans = mid;
l = mid + 1;
}
}
return ans;
}
void solve() {
FOR(i, 1, n) {
sa[i] = i;
h[i].st = (1LL * h[i - 1].st * base.st % M.st + s[i] - '0' + 1) % M.st;
h[i].nd = (1LL * h[i - 1].nd * base.nd % M.nd + s[i] - '0' + 1) % M.nd;
}
FOS(i, n, 1) {
sa[i] = i;
h1[i].st = (1LL * h1[i + 1].st * base.st % M.st + s[i] - '0' + 1) % M.st;
h1[i].nd = (1LL * h1[i + 1].nd * base.nd % M.nd + s[i] - '0' + 1) % M.nd;
}
FOR(i, 1, n) {
int l = 1, r = min(i, n - i + 1), ans = 0;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
if (get(i - mid + 1, i + mid - 1) == get1(i - mid + 1, i + mid - 1)) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
d[i] = ans;
}
FOR(i, 1, n - 1) {
int l = 1, r = min(i, n - i), ans = 0;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
if (get(i - mid + 1, i + mid) == get1(i - mid + 1, i + mid)) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
d1[i] = ans;
}
sort(sa + 1, sa + 1 + n, cmp);
MinSegtree s;
Mergetree s1, s2;
FOR(i, 1, n) {
s1.add(1, 1, n, i, i + d[i] - 1);
if (d1[i]) s2.add(1, 1, n, i, i + d1[i]);
}
FOR(i, 1, n) s.upd(1, 1, n, i, sa[i]);
int l = 2;
ll rs = 0;
FOR(i, 2, n) {
WHILE(l <= i) {
int L = LB(l, i), R = UB(l, i);
if (L == -1 || s.get(1, 1, n, L, R) >= l) {
++l;
continue;
}
break;
}
if (l > i) continue;
int tmp = (l + i + 1) / 2 - 1;
if (tmp) {
rs += s1.get(1, 1, n, 1, tmp, i);
rs += s2.get(1, 1, n, 1, tmp, i);
}
cout << rs << '\n';
}
}
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n >> s;
P[0] = {1, 1};
FOR(i, 1, n) {
P[i].st = 1LL * P[i - 1].st * base.st % M.st;
P[i].nd = 1LL * P[i - 1].nd * base.nd % M.nd;
}
s = "#" + s + "#";
FOR(i, 1, n - 1) cin >> q[i].st >> q[i].nd;
if (n <= 1000) {
subtask12::solve();
return 0;
}
subtask3::solve();
return 0;
}
/*
3
010
1 2
2 3
*/
Compilation message
Main.cpp: In function 'int subtask12::join(int, int)':
Main.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define EACH(i, x) for (auto &(i) : (x))
| ^
Main.cpp:102:3: note: in expansion of macro 'EACH'
102 | EACH(i, a2[y]) a2[x].insert(i);
| ^~~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:120:3: note: in expansion of macro 'FOR'
120 | FOR(i, 0, m1 + m2 - 1) {
| ^~~
Main.cpp:25:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
25 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
| ^
Main.cpp:126:3: note: in expansion of macro 'FOS'
126 | FOS(i, m1 + m2 - 1, 0) {
| ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:131:3: note: in expansion of macro 'FOR'
131 | FOR(i, 0, m1 - 1)
| ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:132:3: note: in expansion of macro 'FOR'
132 | FOR(j, m1, m1 + m2 - 1) {
| ^~~
Main.cpp: In function 'void subtask12::solve()':
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:140:3: note: in expansion of macro 'FOR'
140 | FOR(i, 1, n) {
| ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:145:3: note: in expansion of macro 'FOR'
145 | FOR(i, 1, n - 1) {
| ^~~
Main.cpp: In function 'void subtask3::solve()':
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:219:3: note: in expansion of macro 'FOR'
219 | FOR(i, 1, n) {
| ^~~
Main.cpp:25:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
25 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
| ^
Main.cpp:224:3: note: in expansion of macro 'FOS'
224 | FOS(i, n, 1) {
| ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:229:3: note: in expansion of macro 'FOR'
229 | FOR(i, 1, n) {
| ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:241:3: note: in expansion of macro 'FOR'
241 | FOR(i, 1, n - 1) {
| ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:256:3: note: in expansion of macro 'FOR'
256 | FOR(i, 1, n) {
| ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:260:3: note: in expansion of macro 'FOR'
260 | FOR(i, 1, n) s.upd(1, 1, n, i, sa[i]);
| ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:263:3: note: in expansion of macro 'FOR'
263 | FOR(i, 2, n) {
| ^~~
Main.cpp: In function 'int main()':
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:288:5: note: in expansion of macro 'FOR'
288 | FOR(i, 1, n) {
| ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
Main.cpp:293:5: note: in expansion of macro 'FOR'
293 | FOR(i, 1, n - 1) cin >> q[i].st >> q[i].nd;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8104 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
5 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8104 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
5 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8124 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
44 ms |
8396 KB |
Output is correct |
16 |
Correct |
13 ms |
8404 KB |
Output is correct |
17 |
Correct |
42 ms |
8440 KB |
Output is correct |
18 |
Correct |
13 ms |
8404 KB |
Output is correct |
19 |
Correct |
43 ms |
8612 KB |
Output is correct |
20 |
Correct |
20 ms |
8660 KB |
Output is correct |
21 |
Correct |
38 ms |
8352 KB |
Output is correct |
22 |
Correct |
22 ms |
8340 KB |
Output is correct |
23 |
Correct |
37 ms |
8660 KB |
Output is correct |
24 |
Correct |
19 ms |
8688 KB |
Output is correct |
25 |
Correct |
23 ms |
8392 KB |
Output is correct |
26 |
Correct |
12 ms |
8376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1090 ms |
52820 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8104 KB |
Output is correct |
5 |
Correct |
5 ms |
8148 KB |
Output is correct |
6 |
Correct |
5 ms |
8148 KB |
Output is correct |
7 |
Correct |
5 ms |
8148 KB |
Output is correct |
8 |
Correct |
5 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
5 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8124 KB |
Output is correct |
14 |
Correct |
4 ms |
8148 KB |
Output is correct |
15 |
Correct |
44 ms |
8396 KB |
Output is correct |
16 |
Correct |
13 ms |
8404 KB |
Output is correct |
17 |
Correct |
42 ms |
8440 KB |
Output is correct |
18 |
Correct |
13 ms |
8404 KB |
Output is correct |
19 |
Correct |
43 ms |
8612 KB |
Output is correct |
20 |
Correct |
20 ms |
8660 KB |
Output is correct |
21 |
Correct |
38 ms |
8352 KB |
Output is correct |
22 |
Correct |
22 ms |
8340 KB |
Output is correct |
23 |
Correct |
37 ms |
8660 KB |
Output is correct |
24 |
Correct |
19 ms |
8688 KB |
Output is correct |
25 |
Correct |
23 ms |
8392 KB |
Output is correct |
26 |
Correct |
12 ms |
8376 KB |
Output is correct |
27 |
Execution timed out |
1090 ms |
52820 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |