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 <iostream>
#include <string>
#include <unordered_map>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include "werewolf.h"
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define y1 abcde
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
const int mod = 1e9 + 7;
const int inf = 2e9;
const int L = 22;
const int N = 1e6 + 15;
int n, up[2][L][N], k[N], p[N], sz[N], last, root;
int tin[2][N], tout[2][N], vert[N];
vector <int> t[N << 2];
int g[2][N], curp[N], tim, wastm;
vector <pii> edge;
// tin[0] - min
// tin[1] - max
int find(int v) {
if(v == p[v])
return v;
return p[v] = find(p[v]);
}
void unio(int a, int b) {
a = find(a);
b = find(b);
if(a != b) {
if(sz[a] < sz[b])
swap(a, b);
p[b] = a;
sz[a] += sz[b];
}
}
void build(int v, int p, int f) {
if(v < n)
tin[f][v] = tim++;
up[f][0][v] = p;
for(int i = 1; i < L; ++i)
up[f][i][v] = up[f][i-1][up[f][i-1][v]];
if(v >= n) {
build(g[0][v], v, f);
tin[f][v] = tin[f][g[0][v]];
build(g[1][v], v, f);
tout[f][v] = tout[f][g[1][v]];
}
if(v < n)
tout[f][v] = tim;
if(f == 1 && v < n)
vert[tin[f][v]] = v;
}
inline bool upper(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
inline int get_parent(int v, int need_k, int f) {
for(int i = L - 1; i >= 0; --i) {
if(!f) {
if(k[up[f][i][v]] <= need_k)
v = up[f][i][v];
}
else {
if(k[up[f][i][v]] >= need_k)
v = up[f][i][v];
}
}
return v;
}
void build_tree(int v, int tl, int tr) {
if(tl == tr) {
t[v] = {tin[0][vert[tl]]};
return;
}
int mid = tl + tr >> 1;
build_tree(v << 1, tl, mid);
build_tree(v << 1 | 1, mid + 1, tr);
merge(t[v << 1].begin(), t[v << 1].end(), t[v << 1 | 1].begin(), t[v << 1 | 1].end(), back_inserter(t[v]));
}
bool get(int v, int tl, int tr, int l, int r, int low_k, int up_k) {
if(tl > r || tr < l)
return false;
if(tl >= l && tr <= r) {
int ind1 = lower_bound(t[v].begin(), t[v].end(), low_k) - t[v].begin();
int ind2 = upper_bound(t[v].begin(), t[v].end(), up_k) - t[v].begin();
ind2--;
if(ind1 != t[v].size() && ind2 >= 0) {
bool a = t[v][ind2] >= low_k && t[v][ind2] <= up_k;
bool b = t[v][ind1] >= low_k && t[v][ind2] <= up_k;
return a && b;
}
return false;
}
int mid = tl + tr >> 1;
return get(v << 1, tl, mid, l, r, low_k, up_k) | get(v << 1 | 1, mid + 1, tr, l, r, low_k, up_k);
}
inline void precalc() {
for(int i = 0; i < N; ++i)
sz[i] = 1, p[i] = curp[i] = k[i] = i;
sort(edge.begin(), edge.end(), [&](pii a, pii b) {
return max(a.f, a.se) < max(b.f, b.se);
});
last = n;
for(pii e : edge) {
int v = last;
g[0][v] = curp[find(e.f)], g[1][v] = curp[find(e.se)];
unio(e.f, e.se);
curp[find(e.f)] = v;
root = v;
k[v] = max(e.f, e.se);
last++;
}
build(root, root, 0);
wastm = tim;
sort(edge.begin(), edge.end(), [&](pii a, pii b) {
return min(a.f, a.se) > min(b.f, b.se);
});
for(int i = 0; i < N; ++i)
sz[i] = 1, p[i] = curp[i] = i;
for(pii e : edge) {
int v = last;
g[0][v] = curp[find(e.f)], g[1][v] = curp[find(e.se)];
unio(e.f, e.se);
curp[find(e.f)] = v;
root = v;
k[v] = min(e.f, e.se);
last++;
}
build(root, root, 1);
build_tree(1, wastm, tim - 1);
}
vector <int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E,
vector <int> L, vector<int> R) {
int Q = S.size();
vector <int> A(Q);
for(int i = 0; i < X.size(); ++i)
edge.pb(mp(X[i], Y[i]));
n = N;
precalc();
for(int i = 0; i < Q; ++i) {
int v = get_parent(S[i], R[i], 0);
int u = get_parent(E[i], L[i], 1);
A[i] = get(1, wastm, tim - 1, tin[1][u], tout[1][u] - 1, tin[0][v], tout[0][v] - 1);
}
return A;
}
Compilation message (stderr)
werewolf.cpp: In function 'void build_tree(int, int, int)':
werewolf.cpp:113:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
werewolf.cpp: In function 'bool get(int, int, int, int, int, int, int)':
werewolf.cpp:126:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ind1 != t[v].size() && ind2 >= 0) {
~~~~~^~~~~~~~~~~~~~
werewolf.cpp:133:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:177:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < X.size(); ++i)
~~^~~~~~~~~~
# | 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... |