This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll INF = 100000000LL;
ll n, m, q, k, x, y, a, b, c;
vector<int> G[200000];
int to[200000];
int from[200000];
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[l];
// cout << x << ' ' << mintr[x] << ' ' << maxtr[x] << '\n';
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] = max(maxtr[x * 2 + 1], maxtr[x * 2 + 2]);
// cout << x << ' ' << mintr[x] << ' ' << maxtr[x] << '\n';
}
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) {
// cout << x << ' ' << l << ' ' << r << ' ' << L << ' ' << R << '\n';
// cout << maxtr[x] << '\n';
return maxtr[x];
}
ll mid = (r + l) / 2;
ll ans = max(getmx(x * 2 + 1, l, mid, L, R), getmx(x * 2 + 2, mid + 1, r, L, R));
// cout << x << ' ' << l << ' ' << r << ' ' << L << ' ' << R << '\n';
// cout << ans << '\n';
return ans;
}
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) {
// if (0) {
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);
// cout << sg.getmx(0, 0, n - 1, 3, 6) << '\n';
// exit(0);
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, x, 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);
// cout << x << ' ' << y << '\n';
ll l = x, r = y + 1;
while (r > l) {
// cout << l << ' ' << r << '\n';
ll mid = (r + l) / 2;
// cout << x << ' ' << mid << endl;
// cout << sg.getmx(0, 0, n - 1, x, mid) << ' ' << R[i] << '\n';
if (sg.getmx(0, 0, n - 1, x, mid) > R[i]) {
r = mid;
}
else {
l = mid + 1;
}
// cout << '\n';
}
// cout << l << '\n';
// 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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |