#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 subtere of u?
{
return (u == N || (st[u] <= st[v] && st[v] <= ft[u]));
}
int lca(int u, int v) //v keeps on climbing up until it can't.
{
if (insubt(v, u)) return -1;
FORD(i, 20, 0)
{
if (!insubt(ancestor[i][v], u))
{
v = ancestor[i][v];
}
}
return v;
}
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;
FORD(i, 20, 0)
{
if (!insubt(ancestor[i][w], u))
{
int d = mx[i][w];
if (d > t || d < curmx || !incr[i][w]) return false;
curmx = d;
w = ancestor[i][w];
}
}
if (!insubt(w, u))
{
int d = mx[0][w];
if (d > t || d < curmx) return false;
curmx = d;
}
w = u;
int curmn = INF;
FORD(i, 20, 0)
{
if (!insubt(ancestor[i][w], v))
{
int d = mn[i][w];
if (mx[i][w] > t || d > curmn || !decr[i][w]) return false;
curmn = d;
w = ancestor[i][w];
}
}
if (!insubt(w, v))
{
int d = mn[0][w];
if (mx[0][w] > t) return false;
if (d > curmn) 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 << (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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
8260 KB |
Output is correct |
2 |
Correct |
48 ms |
11060 KB |
Output is correct |
3 |
Correct |
55 ms |
11064 KB |
Output is correct |
4 |
Correct |
51 ms |
11036 KB |
Output is correct |
5 |
Correct |
53 ms |
11204 KB |
Output is correct |
6 |
Correct |
57 ms |
11028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
8260 KB |
Output is correct |
2 |
Correct |
48 ms |
11060 KB |
Output is correct |
3 |
Correct |
55 ms |
11064 KB |
Output is correct |
4 |
Correct |
51 ms |
11036 KB |
Output is correct |
5 |
Correct |
53 ms |
11204 KB |
Output is correct |
6 |
Correct |
57 ms |
11028 KB |
Output is correct |
7 |
Incorrect |
42 ms |
9140 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
8316 KB |
Output is correct |
2 |
Correct |
192 ms |
49588 KB |
Output is correct |
3 |
Correct |
178 ms |
49572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
8316 KB |
Output is correct |
2 |
Correct |
192 ms |
49588 KB |
Output is correct |
3 |
Correct |
178 ms |
49572 KB |
Output is correct |
4 |
Incorrect |
42 ms |
8208 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
8184 KB |
Output is correct |
2 |
Incorrect |
209 ms |
61328 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
8184 KB |
Output is correct |
2 |
Incorrect |
209 ms |
61328 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
8260 KB |
Output is correct |
2 |
Correct |
155 ms |
52760 KB |
Output is correct |
3 |
Correct |
196 ms |
53220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
8260 KB |
Output is correct |
2 |
Correct |
155 ms |
52760 KB |
Output is correct |
3 |
Correct |
196 ms |
53220 KB |
Output is correct |
4 |
Incorrect |
39 ms |
9088 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
8240 KB |
Output is correct |
2 |
Incorrect |
227 ms |
61272 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
8240 KB |
Output is correct |
2 |
Incorrect |
227 ms |
61272 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
8276 KB |
Output is correct |
2 |
Correct |
50 ms |
10960 KB |
Output is correct |
3 |
Correct |
61 ms |
10972 KB |
Output is correct |
4 |
Correct |
52 ms |
11092 KB |
Output is correct |
5 |
Correct |
53 ms |
11312 KB |
Output is correct |
6 |
Correct |
56 ms |
10988 KB |
Output is correct |
7 |
Correct |
43 ms |
9120 KB |
Output is correct |
8 |
Correct |
175 ms |
52436 KB |
Output is correct |
9 |
Correct |
182 ms |
52440 KB |
Output is correct |
10 |
Correct |
41 ms |
9108 KB |
Output is correct |
11 |
Incorrect |
207 ms |
61380 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
8276 KB |
Output is correct |
2 |
Correct |
50 ms |
10960 KB |
Output is correct |
3 |
Correct |
61 ms |
10972 KB |
Output is correct |
4 |
Correct |
52 ms |
11092 KB |
Output is correct |
5 |
Correct |
53 ms |
11312 KB |
Output is correct |
6 |
Correct |
56 ms |
10988 KB |
Output is correct |
7 |
Correct |
43 ms |
9120 KB |
Output is correct |
8 |
Correct |
175 ms |
52436 KB |
Output is correct |
9 |
Correct |
182 ms |
52440 KB |
Output is correct |
10 |
Correct |
41 ms |
9108 KB |
Output is correct |
11 |
Incorrect |
207 ms |
61380 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |