#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
const int MAXN = 240013;
const int INF = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, K, T;
int parent[MAXN], subtree[MAXN], st[MAXN], ft[MAXN], dup[MAXN];
int ancestor[20][MAXN];
bool incr[20][MAXN], decr[20][MAXN];
int mx[20][MAXN], mn[20][MAXN];
vpi edge[MAXN];
pair<int, pii> quer[MAXN];
bool insubt(int u, int v) //is v in the subtree of u?
{
return (u == N || (st[u] <= st[v] && st[v] <= ft[u]));
}
void dfs(int u, int p)
{
st[u] = T;
ft[u] = T;
T++;
subtree[u] = 1;
for (auto e : edge[u])
{
int v = e.fi, d = e.se;
if (v == p) continue;
parent[v] = u;
dup[v] = d;
dfs(v, u);
subtree[u] += subtree[v];
ft[u] = ft[v];
}
}
bool check(int u, int v, int t)
{
//return if v -> u is stirctl increasing and max is <= t
// cerr << "Check " << v << " -> " << u << endl;
int curmx = -1;
int w = v;
//v going up, so it has to keep on increasing
FORD(i, 20, 0)
{
if (!insubt(ancestor[i][w], u))
{
int d = mx[i][w];
if (d < curmx || !incr[i][w] || d > t) return false;
curmx = d;
w = ancestor[i][w];
}
}
if (!insubt(w, u))
{
int d = mx[0][w];
if (d < curmx || d > t) return false;
curmx = d;
}
//u going up, so it has to keep on decreasing.
w = u;
int curmn = INF;
FORD(i, 20, 0)
{
if (!insubt(ancestor[i][w], v))
{
int d = mn[i][w];
if (d > curmn || !decr[i][w] || mx[i][w] > t) return false;
curmn = d;
w = ancestor[i][w];
}
}
if (!insubt(w, v))
{
int d = mn[0][w];
if (d > curmn || mx[0][w] > t) return false;
curmn = d;
}
if (curmn < curmx)
{
return false;
}
return true;
}
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> K;
FOR(i, 0, N + K - 1)
{
char qid; cin >> qid;
if (qid == 'S')
{
int u, v;
cin >> u >> v; u--; v--;
quer[i] = {0, {u, v}};
edge[u].PB({v, i});
edge[v].PB({u, i});
}
if (qid == 'Q')
{
int u, v;
cin >> u >> v; u--; v--;
quer[i] = {1, {u, v}};
// cout << (get(u) == get(v) ? "yes" : "no") << '\n';
}
if (qid == 'C')
{
int u;
cin >> u; u--;
quer[i] = {2, {u, -1}};
// cout << siz[get(u)] << '\n';
}
}
parent[0] = N; parent[N] = N;
dfs(0, N);
FOR(i, 0, N + 1)
{
ancestor[0][i] = parent[i];
mx[0][i] = dup[i];
mn[0][i] = dup[i];
incr[0][i] = true;
decr[0][i] = true;
}
FOR(i, 1, 20)
{
FOR(j, 0, N + 1)
{
int k = ancestor[i - 1][j];
ancestor[i][j] = ancestor[i - 1][k];
mx[i][j] = max(mx[i - 1][j], mx[i - 1][k]);
mn[i][j] = min(mn[i - 1][j], mn[i - 1][k]);
incr[i][j] = (incr[i - 1][j] && incr[i - 1][k] && mx[i - 1][j] < mn[i - 1][k]);
decr[i][j] = (decr[i - 1][j] && decr[i - 1][k] && mn[i - 1][j] > mx[i - 1][k]);
}
}
FOR(i, 0, N + K - 1)
{
if (quer[i].fi == 1)
{
int u = quer[i].se.fi, v = quer[i].se.se;
cout << ((u == v || check(u, v, i)) ? "yes" : "no") << '\n';
//check if v -> u is strictly increasing.
}
if (quer[i].fi == 2)
{
cout << "69\n";
continue;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
8316 KB |
Output is correct |
2 |
Correct |
47 ms |
9620 KB |
Output is correct |
3 |
Correct |
55 ms |
9668 KB |
Output is correct |
4 |
Correct |
54 ms |
9752 KB |
Output is correct |
5 |
Correct |
53 ms |
9796 KB |
Output is correct |
6 |
Correct |
57 ms |
9572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
8316 KB |
Output is correct |
2 |
Correct |
47 ms |
9620 KB |
Output is correct |
3 |
Correct |
55 ms |
9668 KB |
Output is correct |
4 |
Correct |
54 ms |
9752 KB |
Output is correct |
5 |
Correct |
53 ms |
9796 KB |
Output is correct |
6 |
Correct |
57 ms |
9572 KB |
Output is correct |
7 |
Incorrect |
40 ms |
8248 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
8392 KB |
Output is correct |
2 |
Correct |
193 ms |
49596 KB |
Output is correct |
3 |
Correct |
179 ms |
49592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
8392 KB |
Output is correct |
2 |
Correct |
193 ms |
49596 KB |
Output is correct |
3 |
Correct |
179 ms |
49592 KB |
Output is correct |
4 |
Incorrect |
41 ms |
8188 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
8292 KB |
Output is correct |
2 |
Incorrect |
185 ms |
57972 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
8292 KB |
Output is correct |
2 |
Incorrect |
185 ms |
57972 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
8284 KB |
Output is correct |
2 |
Correct |
149 ms |
49468 KB |
Output is correct |
3 |
Correct |
185 ms |
50120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
8284 KB |
Output is correct |
2 |
Correct |
149 ms |
49468 KB |
Output is correct |
3 |
Correct |
185 ms |
50120 KB |
Output is correct |
4 |
Incorrect |
39 ms |
8212 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
8288 KB |
Output is correct |
2 |
Incorrect |
187 ms |
58188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
8288 KB |
Output is correct |
2 |
Incorrect |
187 ms |
58188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
8196 KB |
Output is correct |
2 |
Correct |
49 ms |
9560 KB |
Output is correct |
3 |
Correct |
60 ms |
9704 KB |
Output is correct |
4 |
Correct |
51 ms |
9636 KB |
Output is correct |
5 |
Correct |
53 ms |
9912 KB |
Output is correct |
6 |
Correct |
58 ms |
9732 KB |
Output is correct |
7 |
Correct |
41 ms |
8240 KB |
Output is correct |
8 |
Correct |
175 ms |
49516 KB |
Output is correct |
9 |
Correct |
178 ms |
49624 KB |
Output is correct |
10 |
Correct |
40 ms |
8260 KB |
Output is correct |
11 |
Incorrect |
185 ms |
57960 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
8196 KB |
Output is correct |
2 |
Correct |
49 ms |
9560 KB |
Output is correct |
3 |
Correct |
60 ms |
9704 KB |
Output is correct |
4 |
Correct |
51 ms |
9636 KB |
Output is correct |
5 |
Correct |
53 ms |
9912 KB |
Output is correct |
6 |
Correct |
58 ms |
9732 KB |
Output is correct |
7 |
Correct |
41 ms |
8240 KB |
Output is correct |
8 |
Correct |
175 ms |
49516 KB |
Output is correct |
9 |
Correct |
178 ms |
49624 KB |
Output is correct |
10 |
Correct |
40 ms |
8260 KB |
Output is correct |
11 |
Incorrect |
185 ms |
57960 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |