#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
using ll = int;
constexpr ll INF = 1000000000LL;
ll n, m, q, k, x, y, a, b, c;
vector<int> G[200000];
map<int, int> to;
map<int, int> from;
int bfs(ll s, ll e, ll l, ll r) {
queue< pair<ll, bool> > q;
q.push({s, 0});
vector< vector<ll> > vis(n, vector<ll> (2, 0));
vis[s][0] = 1;
while (!q.empty()) {
auto [x, w] = q.front();
q.pop();
// cout << x << ' ' << w << '\n';
if (!vis[x][1] && x <= r) {
q.push({x, 1});
vis[x][1] = 1;
}
for (int i : G[x]) {
if (!vis[i][w] && (w == 1 || i >= l) && (w == 0 || i <= r)) {
q.push({i, w});
vis[i][w] = 1;
}
}
}
// cout << '\n';
return vis[e][1];
}
void dfs(ll x, ll p, ll num) {
to[x] = num;
from[num] = x;
for (auto i : G[x]) {
if (i != p) {
dfs(i, x, num + 1);
}
}
}
struct segtree {
ll n;
vector<ll> mintr;
vector<ll> maxtr;
void build(ll x, ll l, ll r) {
if (l == r) {
mintr[x] = from[l];
maxtr[x] = from[r];
return;
}
ll mid = (r + l) / 2;
build(x * 2 + 1, l, mid);
build(x * 2 + 2, mid + 1, r);
mintr[x] = min(mintr[x * 2 + 1], mintr[x * 2 + 2]);
maxtr[x] = min(maxtr[x * 2 + 1], maxtr[x * 2 + 2]);
}
segtree(ll N) : n(N) {
mintr.resize(4 * n);
maxtr.resize(4 * n);
build(0, 0, n - 1);
}
ll getmx(ll x, ll l, ll r, ll L, ll R) {
if (r < l) {
return -INF;
}
if (r < L) {
return -INF;
}
if (R < l) {
return -INF;
}
if (L <= l && r <= R) {
return maxtr[x];
}
ll mid = (r + l) / 2;
return max(getmx(x * 2 + 1, l, mid, L, R), getmx(x * 2 + 2, mid + 1, r, L, R));
}
ll getmn(ll x, ll l, ll r, ll L, ll R) {
if (r < l) {
return INF;
}
if (r < L) {
return INF;
}
if (R < l) {
return INF;
}
if (L <= l && r <= R) {
return mintr[x];
}
ll mid = (r + l) / 2;
return min(getmn(x * 2 + 1, l, mid, L, R), getmn(x * 2 + 2, mid + 1, r, L, R));
}
};
vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
n = N;
m = X.size();
q = S.size();
for (int i = 0; i < m; i++) {
G[X[i]].push_back(Y[i]);
G[Y[i]].push_back(X[i]);
}
vector<int> ans;
if (n <= 3000 && m <= 6000 && q <= 3000) {
for (int i = 0; i < q; i++) {
ans.push_back(bfs(S[i], E[i], L[i], R[i]));
}
}
else {
ans.resize(q);
ll x = 0;
for (int i = 0; i < n; i++) {
if (G[i].size() == 1) {
x = i;
break;
}
}
dfs(x, -1, 0);
// cout << (*from.rbegin()).first << endl;
segtree sg(n);
for (int i = 0; i < q; i++) {
ll x = to[S[i]];
ll y = to[E[i]];
// cout << x << ' ' << y << '\n';
if (x < y) {
ll l = x, r = y + 1;
while (r > l) {
ll mid = (r + l) / 2;
if (sg.getmn(0, 0, n - 1, l, mid) < L[i]) {
r = mid;
}
else {
l = mid + 1;
}
}
ans[i] = 1;
if (l == x) {
ans[i] = 0;
}
if (from[l - 1] > R[i]) {
ans[i] = 0;
}
if (from[y] > R[i]) {
ans[i] = 0;
}
if (sg.getmx(0, 0, n - 1, l, y) > R[i]) {
ans[i] = 0;
}
}
else {
swap(x, y);
ll l = x, r = y + 1;
while (r > l) {
ll mid = (r + l) / 2;
// cout << mid << endl;
if (sg.getmx(0, 0, n - 1, l, mid) > R[i]) {
r = mid;
}
else {
l = mid + 1;
}
}
// cout << "DEBUG" << endl;
ans[i] = 1;
if (l == x) {
ans[i] = 0;
}
if (from[l - 1] < L[i]) {
ans[i] = 0;
}
if (from[y] < L[i]) {
ans[i] = 0;
}
if (sg.getmn(0, 0, n - 1, l, y) < L[i]) {
ans[i] = 0;
}
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
5136 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
5136 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
632 ms |
5468 KB |
Output is correct |
11 |
Correct |
529 ms |
5640 KB |
Output is correct |
12 |
Correct |
297 ms |
5468 KB |
Output is correct |
13 |
Correct |
575 ms |
5644 KB |
Output is correct |
14 |
Correct |
498 ms |
5468 KB |
Output is correct |
15 |
Correct |
532 ms |
5720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1238 ms |
67568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
5136 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
632 ms |
5468 KB |
Output is correct |
11 |
Correct |
529 ms |
5640 KB |
Output is correct |
12 |
Correct |
297 ms |
5468 KB |
Output is correct |
13 |
Correct |
575 ms |
5644 KB |
Output is correct |
14 |
Correct |
498 ms |
5468 KB |
Output is correct |
15 |
Correct |
532 ms |
5720 KB |
Output is correct |
16 |
Incorrect |
1238 ms |
67568 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |