This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()
using namespace std;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 1e6 + 5;
//const int mod = 1e9 + 7;
//const int oo = 1e9;
int n, q, cal[N], ans[N];
vector<int> p[N];
struct tree{
int pa[N], sz[N], Min[N], Max[N], in[N], en[N], demin, be[N], f[N][22];
vector<int> g[N];
void pre(){
for(int i = 1; i <= n; i ++) pa[i] = i, sz[i] = 1, Max[i] = Min[i] = i;
}
int fin(int u){
return pa[u] == u ? u : pa[u] = fin(pa[u]);
}
bool dsu(int x,int y){
x = fin(x);
y = fin(y);
if(x == y) return 0;
if(sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y];
pa[y] = x;
Max[x] = max(Max[x], Max[y]);
Min[x] = min(Min[x], Min[y]);
return 1;
}
void dfs(int u,int v){
in[u] = ++demin;
if(v == 0) for(int i = 0; i <= 18; i ++) f[u][i] = u;
else{
f[u][0] = v;
for(int i = 1; i <= 18; i ++) f[u][i] = f[f[u][i - 1]][i - 1];
}
be[demin] = u;
for(auto j : g[u]){
if(j == v) continue;
dfs(j, u);
}
en[u] = demin;
}
bool kt(int u,int v){
return in[u] <= in[v] && in[v] <= en[u];
}
int Find(int type, int x,int lim){
if(type == 0){
int kq = x;
for(int i = 18; i >= 0; i --) if(f[x][i] <= lim){
kq = f[x][i];
x = f[x][i];
}
return kq;
}else{
int kq = x;
for(int i = 18; i >= 0; i --) if(f[x][i] >= lim){
kq = f[x][i];
x = f[x][i];
}
return kq;
}
}
} T[2];
int st[N << 1];
void update(int x){
x += n - 1;
st[x]++;
while(x > 1){
x /= 2;
st[x]++;
}
}
int get(int l,int r){
r++;
int ret = 0;
for(l += n - 1, r += n - 1; l < r; l /= 2, r /= 2){
if(l & 1) ret += st[l++];
if(r & 1) ret += st[--r];
}
return ret;
}
vector<pii> open[N], close[N];
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, q = L.size();
for(int i = 0; i < X.size(); i ++){
int x = ++X[i], y = ++Y[i];
p[x].pb(y);
p[y].pb(x);
}
T[0].pre(), T[1].pre();
for(int i = 1; i <= n; i ++) for(auto j : p[i]) if(j < i){
int tmp = T[0].Max[T[0].fin(j)];
if(T[0].dsu(i, j)){
T[0].g[i].pb(tmp);
}
}
T[0].dfs(n, 0);
for(int i = n; i >= 1; i --) for(auto j : p[i]) if(j > i){
int tmp = T[1].Min[T[1].fin(j)];
if(T[1].dsu(i, j)){
T[1].g[i].pb(tmp);
}
}
T[1].dfs(1, 0);
for(int i = 0; i < q; i ++){
int s = ++S[i], e = ++E[i], l = ++L[i], r = ++R[i];
if(e > R[i] || s < l){
ans[i] = 0;
continue;
}
int ce = T[0].Find(0, e, r), cs = T[1].Find(1, s, l);
open[T[0].in[ce] - 1].pb({i, cs});
close[T[0].en[ce]].pb({i, cs});
}
for(int i = 1; i <= n; i ++){
int u = T[0].be[i];
update(T[1].in[u]);
for(auto [id, j] : open[i]) cal[id] -= get(T[1].in[j], T[1].en[j]);
for(auto [id, j] : close[i]) cal[id] += get(T[1].in[j], T[1].en[j]);
}
vector<int> out;
for(int i = 0; i < q; i ++){
// cerr << cal[i] << " ";
if(!cal[i]) out.pb(0);
else out.pb(1);
}
// cerr << "h\n";
return out;
}
//#define lpv
#ifdef lpv
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
vector<int> x, y, st, ed, lf, rt;
int n, m, q; cin >> n >> m >> q;
for(int i = 1; i <= m; i ++){
int gg, hh; cin >> gg >> hh;
x.pb(gg),y.pb(hh);
}
for(int i = 1; i <= q; i ++){
int g, h, j, k; cin >> g >> h >> j >> k;
st.pb(g), ed.pb(h), lf.pb(j), rt.pb(k);
}
vector<int> oo = check_validity(n,x,y,st,ed,lf,rt);
for(auto j : oo) cout << j << "\n";
}
#endif // lpv
Compilation message (stderr)
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:103:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | 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... |