#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#define int ll
const int inf = int(1e9);
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> p2;
#define rep(i, high) for (int i = 0; i < high; i++)
#define repp(i, low, high) for (int i = low; i < high; i++)
#define repe(i, container) for (auto& i : container)
#define sz(container) ((int)container.size())
#define all(x) begin(x),end(x)
#define ceildiv(x,y) ((x + y - 1) / (y))
inline void fast() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
#define _LOCAL _DEBUG
#if _LOCAL
#define assert(x) if (!(x)) __debugbreak()
#endif
const int maxn = int(1.5e5);
int tin[maxn];
int tout[maxn];
int timer = 0;
int up[maxn][18];
bool upincreasing[maxn][18];
bool updecreasing[maxn][18];
int upmax[maxn][18];
int upmin[maxn][18];
int upv[maxn];
void dfs(int u, int p, int p1, int _p2, vector<vector<p2>>& edges)
{
tin[u] = timer++;
up[u][0] = p;
upmax[u][0] = upv[u];
upmin[u][0] = upv[u];
upincreasing[u][0] = upmax[u][0] < upmax[p][0] || p == 0 || u == 0;
updecreasing[u][0] = upmax[u][0] > upmax[p][0] || p == 0 || u == 0;
repp(d, 1, 18)
{
int mid = up[u][d - 1];
up[u][d] = up[mid][d - 1];
upmax[u][d] = max(upmax[u][d - 1], upmax[mid][d - 1]);
upmin[u][d] = min(upmin[u][d - 1], upmin[mid][d - 1]);
upincreasing[u][d] = upincreasing[u][d - 1] && upincreasing[mid][d - 1] && (upmax[mid][0] < upmax[up[mid][0]][0] || up[mid][0] == 0 || mid == 0);
updecreasing[u][d] = updecreasing[u][d - 1] && updecreasing[mid][d - 1] && (upmax[mid][0] > upmax[up[mid][0]][0] || up[mid][0] == 0 || mid == 0);
}
repe(e, edges[u]) if (e.first != p)
{
upv[e.first] = e.second;
dfs(e.first, u, p1, _p2, edges);
}
tout[u] = timer++;
}
bool isancestor(int a, int b)
{
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int pathmax(int a, int b)
{
if (isancestor(a, b)) return -1;
int ret = -1;
for (int d = 17; d >= 0; d--)
{
if (!isancestor(up[a][d], b))
{
ret = max(ret, upmax[a][d]);
a = up[a][d];
}
}
return max(ret, upmax[a][0]);
}
int pathmin(int a, int b)
{
if (isancestor(a, b)) return inf;
int ret = inf;
for (int d = 17; d >= 0; d--)
{
if (!isancestor(up[a][d], b))
{
ret = min(ret, upmin[a][d]);
a = up[a][d];
}
}
return min(ret, upmin[a][0]);
}
int lca(int a, int b)
{
if (isancestor(a, b)) return a;
if (isancestor(b, a)) return b;
for (int d = 17; d >= 0; d--)
{
if (!isancestor(up[a][d], b))
{
a = up[a][d];
}
}
return up[a][0];
}
struct Centroid
{
int n;
vvi edges;
vi vis;
vi par;
vi size;
Centroid() {}
Centroid(vvi& edges) : edges(edges), n(edges.size()), vis(n), par(n), size(n)
{
init_centroid(0, -1);
}
int find_centroid(int u, int p, int n)
{
repe(e, edges[u])
{
if (e == p) continue;
if (!vis[e] && size[e] > n / 2) return find_centroid(e, u, n);
}
return u;
}
int find_size(int u, int p)
{
if (vis[u]) return 0;
size[u] = 1;
repe(e, edges[u])
{
if (e == p) continue;
size[u] += find_size(e, u);
}
return size[u];
}
void init_centroid(int u, int p)
{
find_size(u, u);
int c = find_centroid(u, u, size[u]);
vis[c] = 1;
par[c] = p;
repe(e, edges[c])
{
if (!vis[e]) init_centroid(e, c);
}
}
};
struct fenwick
{
vi tree;
fenwick(int n) : tree(n+1) {}
void update(int i, int v)
{
for (i++; i < sz(tree); i += i & -i)
tree[i] += v;
}
int query(int i)
{
int ret = 0;
for (i++; i > 0; i -= i & -i)
ret += tree[i];
return ret;
}
};
signed main()
{
fast();
//ifstream in("C:\\Users\\Matis\\desktop\\comp_prog\\x64\\debug\\in.txt");
//cin.rdbuf(in.rdbuf());
int n, q;
cin >> n >> q;
vvi queries;
int t = 0;
vector<vector<p2>> edges(n);
vector<p2> edgelist;
vvi e(n);
rep(i, n + q - 1)
{
char c;
cin >> c;
if (c == 'S')
{
int a, b;
cin >> a >> b;
a--; b--;
edgelist.push_back(p2(a, b));
edges[a].push_back(p2(b, t));
edges[b].push_back(p2(a, t));
e[a].push_back(b);
e[b].push_back(a);
t++;
}
else if (c == 'Q')
{
int a, b;
cin >> a >> b;
a--; b--;
queries.push_back({ 0, b, a, t - 1 });
}
else
{
int c;
cin >> c;
c--;
queries.push_back({ 1, c, -1, t - 1 });
}
}
dfs(0, 0, -1, -1, edges);
Centroid cent(e);
auto getmax = [&](int a, int b)
{
return max(pathmax(a, b), pathmax(b, a));
};
auto getmin = [&](int a, int b)
{
return min(pathmin(a, b), pathmin(b, a));
};
auto pathgood = [&](int a, int b, int t)
{
if (getmax(a, b) > t) return false;
// all edges <= t
// from b->lca increasing
// from a->lca decreasing
bool good = 1;
int u = b;
int prev = inf;
for (int d = 17; d >= 0; d--)
{
if (!isancestor(up[u][d], a))
{
good &= updecreasing[u][d];
u = up[u][d];
}
}
if (!isancestor(u, a))
{
prev = upmax[u][0];
}
u = a;
int v = -1;
for (int d = 17; d >= 0; d--)
{
if (!isancestor(up[u][d], b))
{
good &= upincreasing[u][d];
u = up[u][d];
}
}
if (!isancestor(u, b))
{
v = upmax[u][0];
}
return good && (v < prev);
};
vector<fenwick> children(n, fenwick(0));
vi centroidpar = cent.par;
vector<vector<p2>> enableat(n + 2);
vvi values(n);
rep(i, n)
{
int u = centroidpar[i];
while (u != -1)
{
if (pathgood(u, i, inf))
{
enableat[getmax(u, i)].emplace_back(u, getmin(i, u));
values[u].push_back(getmin(i, u));
}
u = centroidpar[u];
}
}
vector<map<int, int>> fenpos(n);
rep(i, n)
{
set<int> cv(all(values[i]));
int j = 0;
repe(e, cv) fenpos[i][e] = j++;
children[i] = fenwick(cv.size());
}
vi tot(n);
int timer = 0;
repe(q, queries)
{
int type = q[0], a = q[1], b = q[2], t = q[3];
if (type == 0)
{
cout << (pathgood(a, b, t) ? "yes" : "no") << "\n";
}
else
{
while (timer <= t)
{
repe(e, enableat[timer])
{
children[e.first].update(fenpos[e.first][e.second], 1);
tot[e.first]++;
}
timer++;
}
int ans = tot[a];
int u = a;
int prev = u;
while (centroidpar[u]!=-1)
{
prev = u;
u = centroidpar[u];
if (pathgood(a, u, t))
{
ans++;
int v = getmax(a, u);
ans += tot[u];
int ind = sz(fenpos[u]);
auto it = fenpos[u].upper_bound(v);
if (it != fenpos[u].end()) ind = it->second;
if (ind) ans -= children[u].query(ind - 1);
}
}
cout << ans + 1 << "\n";
}
}
return 0;
}
Compilation message
servers.cpp: In constructor 'Centroid::Centroid(vvi&)':
servers.cpp:116:9: warning: 'Centroid::edges' will be initialized after [-Wreorder]
116 | vvi edges;
| ^~~~~
servers.cpp:115:9: warning: 'int Centroid::n' [-Wreorder]
115 | int n;
| ^
servers.cpp:122:5: warning: when initialized here [-Wreorder]
122 | Centroid(vvi& edges) : edges(edges), n(edges.size()), vis(n), par(n), size(n)
| ^~~~~~~~
servers.cpp: In function 'int main()':
servers.cpp:337:17: warning: variable 'prev' set but not used [-Wunused-but-set-variable]
337 | int prev = u;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
18420 KB |
Output is correct |
2 |
Correct |
47 ms |
20736 KB |
Output is correct |
3 |
Correct |
44 ms |
20740 KB |
Output is correct |
4 |
Correct |
66 ms |
21008 KB |
Output is correct |
5 |
Correct |
68 ms |
21244 KB |
Output is correct |
6 |
Correct |
46 ms |
20996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
18420 KB |
Output is correct |
2 |
Correct |
47 ms |
20736 KB |
Output is correct |
3 |
Correct |
44 ms |
20740 KB |
Output is correct |
4 |
Correct |
66 ms |
21008 KB |
Output is correct |
5 |
Correct |
68 ms |
21244 KB |
Output is correct |
6 |
Correct |
46 ms |
20996 KB |
Output is correct |
7 |
Correct |
36 ms |
18428 KB |
Output is correct |
8 |
Correct |
63 ms |
20344 KB |
Output is correct |
9 |
Correct |
49 ms |
20748 KB |
Output is correct |
10 |
Correct |
104 ms |
20476 KB |
Output is correct |
11 |
Correct |
150 ms |
21120 KB |
Output is correct |
12 |
Correct |
43 ms |
20740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
18408 KB |
Output is correct |
2 |
Correct |
280 ms |
106472 KB |
Output is correct |
3 |
Correct |
257 ms |
106596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
18408 KB |
Output is correct |
2 |
Correct |
280 ms |
106472 KB |
Output is correct |
3 |
Correct |
257 ms |
106596 KB |
Output is correct |
4 |
Correct |
31 ms |
18468 KB |
Output is correct |
5 |
Correct |
297 ms |
106468 KB |
Output is correct |
6 |
Correct |
236 ms |
105600 KB |
Output is correct |
7 |
Correct |
241 ms |
105704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
18432 KB |
Output is correct |
2 |
Correct |
630 ms |
120148 KB |
Output is correct |
3 |
Correct |
616 ms |
120180 KB |
Output is correct |
4 |
Correct |
733 ms |
131448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
18432 KB |
Output is correct |
2 |
Correct |
630 ms |
120148 KB |
Output is correct |
3 |
Correct |
616 ms |
120180 KB |
Output is correct |
4 |
Correct |
733 ms |
131448 KB |
Output is correct |
5 |
Correct |
30 ms |
18432 KB |
Output is correct |
6 |
Correct |
850 ms |
119728 KB |
Output is correct |
7 |
Correct |
988 ms |
130420 KB |
Output is correct |
8 |
Correct |
986 ms |
119344 KB |
Output is correct |
9 |
Correct |
1010 ms |
119152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
18444 KB |
Output is correct |
2 |
Correct |
389 ms |
110624 KB |
Output is correct |
3 |
Correct |
404 ms |
102500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
18444 KB |
Output is correct |
2 |
Correct |
389 ms |
110624 KB |
Output is correct |
3 |
Correct |
404 ms |
102500 KB |
Output is correct |
4 |
Correct |
33 ms |
18432 KB |
Output is correct |
5 |
Correct |
492 ms |
110072 KB |
Output is correct |
6 |
Correct |
527 ms |
101948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
18444 KB |
Output is correct |
2 |
Correct |
619 ms |
120032 KB |
Output is correct |
3 |
Correct |
616 ms |
120076 KB |
Output is correct |
4 |
Correct |
722 ms |
131520 KB |
Output is correct |
5 |
Correct |
32 ms |
18444 KB |
Output is correct |
6 |
Correct |
391 ms |
110652 KB |
Output is correct |
7 |
Correct |
378 ms |
102456 KB |
Output is correct |
8 |
Correct |
558 ms |
102844 KB |
Output is correct |
9 |
Correct |
554 ms |
102952 KB |
Output is correct |
10 |
Correct |
1213 ms |
113668 KB |
Output is correct |
11 |
Correct |
1226 ms |
112304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
18444 KB |
Output is correct |
2 |
Correct |
619 ms |
120032 KB |
Output is correct |
3 |
Correct |
616 ms |
120076 KB |
Output is correct |
4 |
Correct |
722 ms |
131520 KB |
Output is correct |
5 |
Correct |
32 ms |
18444 KB |
Output is correct |
6 |
Correct |
391 ms |
110652 KB |
Output is correct |
7 |
Correct |
378 ms |
102456 KB |
Output is correct |
8 |
Correct |
558 ms |
102844 KB |
Output is correct |
9 |
Correct |
554 ms |
102952 KB |
Output is correct |
10 |
Correct |
1213 ms |
113668 KB |
Output is correct |
11 |
Correct |
1226 ms |
112304 KB |
Output is correct |
12 |
Correct |
29 ms |
18428 KB |
Output is correct |
13 |
Correct |
868 ms |
119476 KB |
Output is correct |
14 |
Correct |
992 ms |
130480 KB |
Output is correct |
15 |
Correct |
1025 ms |
119400 KB |
Output is correct |
16 |
Correct |
1000 ms |
119160 KB |
Output is correct |
17 |
Correct |
33 ms |
18492 KB |
Output is correct |
18 |
Correct |
492 ms |
110100 KB |
Output is correct |
19 |
Correct |
498 ms |
102124 KB |
Output is correct |
20 |
Correct |
736 ms |
102504 KB |
Output is correct |
21 |
Correct |
693 ms |
102516 KB |
Output is correct |
22 |
Correct |
1640 ms |
111452 KB |
Output is correct |
23 |
Correct |
1854 ms |
119452 KB |
Output is correct |
24 |
Correct |
1418 ms |
118208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
18448 KB |
Output is correct |
2 |
Correct |
46 ms |
20636 KB |
Output is correct |
3 |
Correct |
44 ms |
20896 KB |
Output is correct |
4 |
Correct |
59 ms |
20988 KB |
Output is correct |
5 |
Correct |
69 ms |
21784 KB |
Output is correct |
6 |
Correct |
44 ms |
20992 KB |
Output is correct |
7 |
Correct |
31 ms |
18436 KB |
Output is correct |
8 |
Correct |
261 ms |
106476 KB |
Output is correct |
9 |
Correct |
258 ms |
106476 KB |
Output is correct |
10 |
Correct |
29 ms |
18444 KB |
Output is correct |
11 |
Correct |
597 ms |
120036 KB |
Output is correct |
12 |
Correct |
607 ms |
120184 KB |
Output is correct |
13 |
Correct |
731 ms |
131424 KB |
Output is correct |
14 |
Correct |
32 ms |
18432 KB |
Output is correct |
15 |
Correct |
383 ms |
110708 KB |
Output is correct |
16 |
Correct |
387 ms |
102456 KB |
Output is correct |
17 |
Correct |
557 ms |
102720 KB |
Output is correct |
18 |
Correct |
547 ms |
103064 KB |
Output is correct |
19 |
Correct |
1140 ms |
113676 KB |
Output is correct |
20 |
Correct |
1278 ms |
112252 KB |
Output is correct |
21 |
Correct |
274 ms |
104448 KB |
Output is correct |
22 |
Correct |
278 ms |
104196 KB |
Output is correct |
23 |
Correct |
338 ms |
103512 KB |
Output is correct |
24 |
Correct |
329 ms |
103536 KB |
Output is correct |
25 |
Correct |
765 ms |
108928 KB |
Output is correct |
26 |
Correct |
429 ms |
102084 KB |
Output is correct |
27 |
Correct |
423 ms |
101748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
18448 KB |
Output is correct |
2 |
Correct |
46 ms |
20636 KB |
Output is correct |
3 |
Correct |
44 ms |
20896 KB |
Output is correct |
4 |
Correct |
59 ms |
20988 KB |
Output is correct |
5 |
Correct |
69 ms |
21784 KB |
Output is correct |
6 |
Correct |
44 ms |
20992 KB |
Output is correct |
7 |
Correct |
31 ms |
18436 KB |
Output is correct |
8 |
Correct |
261 ms |
106476 KB |
Output is correct |
9 |
Correct |
258 ms |
106476 KB |
Output is correct |
10 |
Correct |
29 ms |
18444 KB |
Output is correct |
11 |
Correct |
597 ms |
120036 KB |
Output is correct |
12 |
Correct |
607 ms |
120184 KB |
Output is correct |
13 |
Correct |
731 ms |
131424 KB |
Output is correct |
14 |
Correct |
32 ms |
18432 KB |
Output is correct |
15 |
Correct |
383 ms |
110708 KB |
Output is correct |
16 |
Correct |
387 ms |
102456 KB |
Output is correct |
17 |
Correct |
557 ms |
102720 KB |
Output is correct |
18 |
Correct |
547 ms |
103064 KB |
Output is correct |
19 |
Correct |
1140 ms |
113676 KB |
Output is correct |
20 |
Correct |
1278 ms |
112252 KB |
Output is correct |
21 |
Correct |
274 ms |
104448 KB |
Output is correct |
22 |
Correct |
278 ms |
104196 KB |
Output is correct |
23 |
Correct |
338 ms |
103512 KB |
Output is correct |
24 |
Correct |
329 ms |
103536 KB |
Output is correct |
25 |
Correct |
765 ms |
108928 KB |
Output is correct |
26 |
Correct |
429 ms |
102084 KB |
Output is correct |
27 |
Correct |
423 ms |
101748 KB |
Output is correct |
28 |
Correct |
34 ms |
18452 KB |
Output is correct |
29 |
Correct |
62 ms |
20476 KB |
Output is correct |
30 |
Correct |
50 ms |
20732 KB |
Output is correct |
31 |
Correct |
101 ms |
20848 KB |
Output is correct |
32 |
Correct |
152 ms |
21516 KB |
Output is correct |
33 |
Correct |
44 ms |
20808 KB |
Output is correct |
34 |
Correct |
31 ms |
18444 KB |
Output is correct |
35 |
Correct |
281 ms |
106476 KB |
Output is correct |
36 |
Correct |
247 ms |
105812 KB |
Output is correct |
37 |
Correct |
253 ms |
105836 KB |
Output is correct |
38 |
Correct |
29 ms |
18432 KB |
Output is correct |
39 |
Correct |
847 ms |
119768 KB |
Output is correct |
40 |
Correct |
1010 ms |
130496 KB |
Output is correct |
41 |
Correct |
998 ms |
119596 KB |
Output is correct |
42 |
Correct |
1032 ms |
119348 KB |
Output is correct |
43 |
Correct |
32 ms |
18432 KB |
Output is correct |
44 |
Correct |
497 ms |
110716 KB |
Output is correct |
45 |
Correct |
534 ms |
102004 KB |
Output is correct |
46 |
Correct |
706 ms |
102100 KB |
Output is correct |
47 |
Correct |
687 ms |
102596 KB |
Output is correct |
48 |
Correct |
1597 ms |
111524 KB |
Output is correct |
49 |
Correct |
1815 ms |
119568 KB |
Output is correct |
50 |
Correct |
1404 ms |
118092 KB |
Output is correct |
51 |
Correct |
270 ms |
104396 KB |
Output is correct |
52 |
Correct |
260 ms |
106624 KB |
Output is correct |
53 |
Correct |
243 ms |
106104 KB |
Output is correct |
54 |
Correct |
243 ms |
106784 KB |
Output is correct |
55 |
Correct |
241 ms |
103760 KB |
Output is correct |
56 |
Correct |
383 ms |
103236 KB |
Output is correct |
57 |
Correct |
793 ms |
108844 KB |
Output is correct |
58 |
Correct |
651 ms |
101384 KB |
Output is correct |
59 |
Correct |
446 ms |
101824 KB |
Output is correct |