#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) 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) return false;
if (d > curmn) 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
40 ms |
8256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
40 ms |
8256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
8240 KB |
Output is correct |
2 |
Correct |
188 ms |
52396 KB |
Output is correct |
3 |
Correct |
184 ms |
52408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
8240 KB |
Output is correct |
2 |
Correct |
188 ms |
52396 KB |
Output is correct |
3 |
Correct |
184 ms |
52408 KB |
Output is correct |
4 |
Incorrect |
41 ms |
9120 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
8260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
8260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
8204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
8204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
40 ms |
8268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
40 ms |
8268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
8240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
8240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |