#include <bits/stdc++.h>
#define fr(i, a, b) for (int i = (a); i <= (b); i++)
#define rf(i, a, b) for (int i = (a); i >= (b); i--)
#define fe(x, y) for (auto& x : y)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
bool umn(T& a, T b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
return a < b ? a = b, 1 : 0;
}
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;
const int N = 3e5 + 5;
#define dec abcd
int n, k;
ve<pii> g[N];
ve<int> dp[N];
ve<array<int, 3>> Q;
int timer;
int tin[N], tout[N];
int up[N][20];
int par[N], val[N];
bool inc[N][20];
bool dec[N][20];
int dep[N];
void dfs(int v = 1, int p = 0) {
up[v][0] = p;
fr (i, 1, 19) up[v][i] = up[up[v][i - 1]][i - 1];
tin[v] = timer++;
fe (to, g[v]) {
if (to.fi == p) continue;
par[to.fi] = v;
val[to.fi] = to.se;
dep[to.fi] = dep[v] + 1;
dfs(to.fi, v);
}
tout[v] = timer++;
}
bool isUpper(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tin[b];
}
int getLCA(int a, int b) {
if (isUpper(a, b)) return a;
if (isUpper(b, a)) return b;
rf (i, 19, 0) {
if (up[a][i] && !isUpper(up[a][i], b)) {
a = up[a][i];
}
}
return up[a][0];
}
int getDist(int a, int b) {
return dep[a] + dep[b] - 2 * dep[getLCA(a, b)];
}
int goUp(int v, int d) {
rf (i, 19, 0) {
if (d - pw(i) >= 0) {
v = up[v][i];
d -= pw(i);
}
}
assert(v);
return v;
}
bool checkInc(int v, int d) {
if (d == 0) return 1;
int p = __lg(d);
return inc[v][p] && inc[goUp(v, d - pw(p))][p];
}
bool checkDec(int v, int d) {
if (d == 0) return 1;
int p = __lg(d);
return dec[v][p] && dec[goUp(v, d - pw(p))][p];
}
int getLastEdge(int a, int b) {
assert(a != b);
if (isUpper(a, b)) {
return val[b];
}
if (isUpper(b, a)) {
int v = goUp(a, dep[a] - dep[b] - 1);
return val[v];
}
return val[b];
}
int checkPath(int a, int b) {
if (a == b) return 1;
if (isUpper(a, b)) {
return checkDec(b, dep[b] - dep[a]);
}
if (isUpper(b, a)) {
return checkInc(a, dep[a] - dep[b]);
}
int c = getLCA(a, b);
int e1 = val[goUp(a, dep[a] - dep[c] - 1)];
int e2 = val[goUp(b, dep[b] - dep[c] - 1)];
if (e1 > e2) return 0;
return checkInc(a, dep[a] - dep[c]) && checkDec(b, dep[b] - dep[c]);
}
ve<int> G[N];
int P[N];
int sz[N];
int cmpSz;
bool block[N];
void calcSz(int v, int p) {
sz[v] = 1;
fe (to, g[v]) {
if (to.fi == p || block[to.fi]) continue;
calcSz(to.fi, v);
sz[v] += sz[to.fi];
}
}
int findCentroid(int v, int p) {
fe (to, g[v]) {
if (to.fi == p || block[to.fi]) continue;
if (sz[to.fi] > cmpSz / 2) {
return findCentroid(to.fi, v);
}
}
return v;
}
int go(int v = 1) {
calcSz(v, 0);
cmpSz = sz[v];
int c = findCentroid(v, 0);
block[c] = 1;
fe (to, g[c]) {
if (!block[to.fi]) {
P[go(to.fi)] = c;
}
}
return c;
}
int TIN[N], TOUT[N];
void dfs2(int v) {
TIN[v] = timer++;
fe (to, G[v]) {
dfs2(to);
}
TOUT[v] = timer++;
}
bool inside(int v, int c) {
return TIN[c] <= TIN[v] && TIN[v] <= TOUT[c];
}
ve<array<int, 3>> e;
int from[N];
void addEdge(int v) {
int c = v;
while (1) {
if (inside(par[v], c)) {
int a = v;
int b = par[v];
if (getDist(a, c) > getDist(b, c)) swap(a, b);
if (checkPath(c, b)) {
int e = getLastEdge(b, c);
int p = lower_bound(all(g[c]), mp(-1, e), [](pii i, pii j) {
return i.se < j.se;
}) - g[c].begin();
assert(g[c][p].se == e);
dp[c][p]++;
}
}
if (!P[c]) break;
c = P[c];
}
}
int getAns(int v, int t) {
int c = v;
int ans = 0;
while (1) {
if (checkPath(v, c)) {
int e = (v == c ? -1 : getLastEdge(v, c));
if (e <= t) {
ans++;
rf (i, sz(g[c]) - 1, 0) {
if (g[c][i].se > e) {
ans += dp[c][i];
} else {
break;
}
}
}
}
if (!P[c]) break;
c = P[c];
}
return ans;
}
int main() {
#ifndef LOCAL
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
cin >> n >> k;
fr (i, 1, n + k - 1) {
char c;
cin >> c;
if (c == 'S') {
int a, b;
cin >> a >> b;
g[a].pb({b, i});
g[b].pb({a, i});
e.pb({i, a, b});
// cout << a << " " << b << " " << i << "\n";
}
if (c == 'Q') {
int a, d;
cin >> a >> d;
Q.pb({i, a, d});
}
if (c == 'C') {
int x;
cin >> x;
Q.pb({i, x, -1});
}
}
fr (i, 1, n) {
dp[i].resize(sz(g[i]));
sort(all(g[i]), [](pii a, pii b) {
return a.se < b.se;
});
}
dfs();
fr (i, 1, n) {
if (par[i]) {
dec[i][0] = 1;
inc[i][0] = 1;
}
}
fr (p, 1, 19) {
fr (i, 1, n) {
if (up[i][p] && inc[i][p - 1] && inc[up[i][p - 1]][p - 1]) {
if (val[goUp(i, pw(p - 1) - 1)] < val[up[i][p - 1]]) {
inc[i][p] = 1;
}
}
if (up[i][p] && dec[i][p - 1] && dec[up[i][p - 1]][p - 1]) {
if (val[goUp(i, pw(p - 1) - 1)] > val[up[i][p - 1]]) {
dec[i][p] = 1;
}
}
}
}
int root = go();
fr (i, 1, n) {
if (P[i]) {
G[P[i]].pb(i);
}
}
timer = 0;
dfs2(root);
int ptr = 0;
fe (cur, Q) {
while (ptr < sz(e) && e[ptr][0] <= cur[0]) {
if (isUpper(e[ptr][1], e[ptr][2])) swap(e[ptr][1], e[ptr][2]);
addEdge(e[ptr][1]);
ptr++;
}
if (cur[2] != -1) {
if (cur[2] == cur[1]) {
cout << "yes\n";
continue;
}
if (checkPath(cur[2], cur[1]) && getLastEdge(cur[2], cur[1]) <= cur[0]) {
cout << "yes\n";
} else {
cout << "no\n";
}
} else {
cout << getAns(cur[1], cur[0]) << "\n";
}
}
return 0;
}
/*
4 4
S 1 2
S 1 3
S 3 4
Q 2 1
Q 2 2
Q 2 3
Q 2 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24320 KB |
Output is correct |
2 |
Correct |
56 ms |
25724 KB |
Output is correct |
3 |
Correct |
53 ms |
25728 KB |
Output is correct |
4 |
Correct |
75 ms |
25708 KB |
Output is correct |
5 |
Correct |
39 ms |
25848 KB |
Output is correct |
6 |
Correct |
50 ms |
25724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24320 KB |
Output is correct |
2 |
Correct |
56 ms |
25724 KB |
Output is correct |
3 |
Correct |
53 ms |
25728 KB |
Output is correct |
4 |
Correct |
75 ms |
25708 KB |
Output is correct |
5 |
Correct |
39 ms |
25848 KB |
Output is correct |
6 |
Correct |
50 ms |
25724 KB |
Output is correct |
7 |
Correct |
47 ms |
24256 KB |
Output is correct |
8 |
Correct |
73 ms |
25360 KB |
Output is correct |
9 |
Correct |
72 ms |
25456 KB |
Output is correct |
10 |
Correct |
102 ms |
25356 KB |
Output is correct |
11 |
Correct |
53 ms |
25580 KB |
Output is correct |
12 |
Correct |
135 ms |
25420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
24252 KB |
Output is correct |
2 |
Correct |
197 ms |
56000 KB |
Output is correct |
3 |
Correct |
188 ms |
55976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
24252 KB |
Output is correct |
2 |
Correct |
197 ms |
56000 KB |
Output is correct |
3 |
Correct |
188 ms |
55976 KB |
Output is correct |
4 |
Correct |
42 ms |
24256 KB |
Output is correct |
5 |
Correct |
835 ms |
56180 KB |
Output is correct |
6 |
Execution timed out |
3536 ms |
54908 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
24256 KB |
Output is correct |
2 |
Correct |
323 ms |
63628 KB |
Output is correct |
3 |
Correct |
325 ms |
63592 KB |
Output is correct |
4 |
Correct |
330 ms |
63500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
24256 KB |
Output is correct |
2 |
Correct |
323 ms |
63628 KB |
Output is correct |
3 |
Correct |
325 ms |
63592 KB |
Output is correct |
4 |
Correct |
330 ms |
63500 KB |
Output is correct |
5 |
Correct |
34 ms |
24256 KB |
Output is correct |
6 |
Correct |
378 ms |
63192 KB |
Output is correct |
7 |
Correct |
491 ms |
63252 KB |
Output is correct |
8 |
Correct |
399 ms |
62760 KB |
Output is correct |
9 |
Correct |
397 ms |
62720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
24256 KB |
Output is correct |
2 |
Correct |
272 ms |
56672 KB |
Output is correct |
3 |
Correct |
329 ms |
56980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
24256 KB |
Output is correct |
2 |
Correct |
272 ms |
56672 KB |
Output is correct |
3 |
Correct |
329 ms |
56980 KB |
Output is correct |
4 |
Correct |
41 ms |
24312 KB |
Output is correct |
5 |
Correct |
352 ms |
56464 KB |
Output is correct |
6 |
Correct |
353 ms |
56372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
24308 KB |
Output is correct |
2 |
Correct |
341 ms |
63580 KB |
Output is correct |
3 |
Correct |
328 ms |
63508 KB |
Output is correct |
4 |
Correct |
338 ms |
63512 KB |
Output is correct |
5 |
Correct |
40 ms |
24252 KB |
Output is correct |
6 |
Correct |
274 ms |
56696 KB |
Output is correct |
7 |
Correct |
310 ms |
56924 KB |
Output is correct |
8 |
Correct |
510 ms |
56964 KB |
Output is correct |
9 |
Correct |
520 ms |
56968 KB |
Output is correct |
10 |
Correct |
814 ms |
59644 KB |
Output is correct |
11 |
Correct |
910 ms |
59128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
24308 KB |
Output is correct |
2 |
Correct |
341 ms |
63580 KB |
Output is correct |
3 |
Correct |
328 ms |
63508 KB |
Output is correct |
4 |
Correct |
338 ms |
63512 KB |
Output is correct |
5 |
Correct |
40 ms |
24252 KB |
Output is correct |
6 |
Correct |
274 ms |
56696 KB |
Output is correct |
7 |
Correct |
310 ms |
56924 KB |
Output is correct |
8 |
Correct |
510 ms |
56964 KB |
Output is correct |
9 |
Correct |
520 ms |
56968 KB |
Output is correct |
10 |
Correct |
814 ms |
59644 KB |
Output is correct |
11 |
Correct |
910 ms |
59128 KB |
Output is correct |
12 |
Correct |
36 ms |
24280 KB |
Output is correct |
13 |
Correct |
364 ms |
63120 KB |
Output is correct |
14 |
Correct |
480 ms |
63292 KB |
Output is correct |
15 |
Correct |
381 ms |
62812 KB |
Output is correct |
16 |
Correct |
394 ms |
62716 KB |
Output is correct |
17 |
Correct |
40 ms |
24260 KB |
Output is correct |
18 |
Correct |
386 ms |
56516 KB |
Output is correct |
19 |
Correct |
372 ms |
56460 KB |
Output is correct |
20 |
Correct |
606 ms |
56548 KB |
Output is correct |
21 |
Correct |
613 ms |
56548 KB |
Output is correct |
22 |
Correct |
1242 ms |
58260 KB |
Output is correct |
23 |
Correct |
1176 ms |
59708 KB |
Output is correct |
24 |
Correct |
925 ms |
59916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24256 KB |
Output is correct |
2 |
Correct |
56 ms |
25580 KB |
Output is correct |
3 |
Correct |
52 ms |
25672 KB |
Output is correct |
4 |
Correct |
66 ms |
25680 KB |
Output is correct |
5 |
Correct |
39 ms |
25888 KB |
Output is correct |
6 |
Correct |
49 ms |
25652 KB |
Output is correct |
7 |
Correct |
40 ms |
24276 KB |
Output is correct |
8 |
Correct |
206 ms |
55960 KB |
Output is correct |
9 |
Correct |
186 ms |
55956 KB |
Output is correct |
10 |
Correct |
30 ms |
24316 KB |
Output is correct |
11 |
Correct |
327 ms |
63536 KB |
Output is correct |
12 |
Correct |
328 ms |
63552 KB |
Output is correct |
13 |
Correct |
325 ms |
63560 KB |
Output is correct |
14 |
Correct |
39 ms |
24252 KB |
Output is correct |
15 |
Correct |
262 ms |
56764 KB |
Output is correct |
16 |
Correct |
309 ms |
56860 KB |
Output is correct |
17 |
Correct |
507 ms |
57036 KB |
Output is correct |
18 |
Correct |
484 ms |
56880 KB |
Output is correct |
19 |
Correct |
795 ms |
59664 KB |
Output is correct |
20 |
Correct |
855 ms |
59028 KB |
Output is correct |
21 |
Correct |
199 ms |
56176 KB |
Output is correct |
22 |
Correct |
198 ms |
56344 KB |
Output is correct |
23 |
Correct |
308 ms |
56632 KB |
Output is correct |
24 |
Correct |
307 ms |
56812 KB |
Output is correct |
25 |
Correct |
408 ms |
59700 KB |
Output is correct |
26 |
Correct |
347 ms |
56576 KB |
Output is correct |
27 |
Correct |
325 ms |
56564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
24256 KB |
Output is correct |
2 |
Correct |
56 ms |
25580 KB |
Output is correct |
3 |
Correct |
52 ms |
25672 KB |
Output is correct |
4 |
Correct |
66 ms |
25680 KB |
Output is correct |
5 |
Correct |
39 ms |
25888 KB |
Output is correct |
6 |
Correct |
49 ms |
25652 KB |
Output is correct |
7 |
Correct |
40 ms |
24276 KB |
Output is correct |
8 |
Correct |
206 ms |
55960 KB |
Output is correct |
9 |
Correct |
186 ms |
55956 KB |
Output is correct |
10 |
Correct |
30 ms |
24316 KB |
Output is correct |
11 |
Correct |
327 ms |
63536 KB |
Output is correct |
12 |
Correct |
328 ms |
63552 KB |
Output is correct |
13 |
Correct |
325 ms |
63560 KB |
Output is correct |
14 |
Correct |
39 ms |
24252 KB |
Output is correct |
15 |
Correct |
262 ms |
56764 KB |
Output is correct |
16 |
Correct |
309 ms |
56860 KB |
Output is correct |
17 |
Correct |
507 ms |
57036 KB |
Output is correct |
18 |
Correct |
484 ms |
56880 KB |
Output is correct |
19 |
Correct |
795 ms |
59664 KB |
Output is correct |
20 |
Correct |
855 ms |
59028 KB |
Output is correct |
21 |
Correct |
199 ms |
56176 KB |
Output is correct |
22 |
Correct |
198 ms |
56344 KB |
Output is correct |
23 |
Correct |
308 ms |
56632 KB |
Output is correct |
24 |
Correct |
307 ms |
56812 KB |
Output is correct |
25 |
Correct |
408 ms |
59700 KB |
Output is correct |
26 |
Correct |
347 ms |
56576 KB |
Output is correct |
27 |
Correct |
325 ms |
56564 KB |
Output is correct |
28 |
Correct |
42 ms |
24320 KB |
Output is correct |
29 |
Correct |
70 ms |
25324 KB |
Output is correct |
30 |
Correct |
79 ms |
25396 KB |
Output is correct |
31 |
Correct |
95 ms |
25428 KB |
Output is correct |
32 |
Correct |
53 ms |
25520 KB |
Output is correct |
33 |
Correct |
124 ms |
25404 KB |
Output is correct |
34 |
Correct |
41 ms |
24300 KB |
Output is correct |
35 |
Correct |
826 ms |
55864 KB |
Output is correct |
36 |
Execution timed out |
3552 ms |
54776 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |