#ifndef LOCAL
#include "werewolf.h"
#endif
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;
typedef long long ll;
struct UnionFind{
int par[202020];
UnionFind(){ iota(par, par+202020, 0); }
int find(int v){ return v == par[v] ? v : par[v] = find(par[v]); }
bool merge(int P, int C){
P = find(P); C = find(C);
if(P == C) return false;
par[C] = P; return true;
}
} uf;
struct MergeSortTree{
static const int sz = 1 << 18;
vector<int> tree[1 << 19];
void set(int x, int v){
tree[x|sz].push_back(v);
}
void build(){
for(int i=sz-1; i; i--) merge(all(tree[i << 1]), all(tree[i << 1 | 1]), back_inserter(tree[i]));
}
int query(int l, int r, int mn, int mx){
l |= sz; r |= sz;
while(l <= r){
if(l & 1){
auto it = lower_bound(all(tree[l]), mn);
if(it != tree[l].end() && *it <= mx) return 1;
l++;
}
if(~r & 1){
auto it = lower_bound(all(tree[r]), mn);
if(it != tree[r].end() && *it <= mx) return 1;
r--;
}
l >>= 1; r >>= 1;
}
return 0;
}
} seg;
struct Tree{
int par[22][202020], in[202020], out[202020], pv;
vector<int> g[202020];
UnionFind uf;
void addEdge(int P, int C){
if(uf.find(P) == uf.find(C)) return;
C = uf.find(C);
par[0][C] = P; g[P].push_back(C);
uf.merge(P, C);
}
void dfs(int v){
in[v] = ++pv;
for(auto i : g[v]) dfs(i);
out[v] = pv;
}
void build(int n, int rt){
dfs(rt); par[0][rt] = rt;
for(int i=1; i<22; i++) for(int j=1; j<=n; j++) par[i][j] = par[i-1][par[i-1][j]];
}
} tree1, tree2;
int n, m, q;
vector<int> g[202020];
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 = L.size();
for(auto &i : X) i++; for(auto &i : Y) i++; // I love 1-based!
for(auto &i : S) i++; for(auto &i : E) i++; // I love 1-based!
for(auto &i : L) i++; for(auto &i : R) i++; // I love 1-based!
vector<int> ret;
for(int i=0; i<m; i++){
int s = X[i], e = Y[i];
g[s].push_back(e); g[e].push_back(s);
}
for(int i=1; i<=n; i++){
for(auto j : g[i]) if(j < i) tree1.addEdge(i, j);
}
for(int i=n; i>=1; i--){
for(auto j : g[i]) if(i < j) tree2.addEdge(i, j);
}
tree1.build(n, n); tree2.build(n, 1);
for(int i=1; i<=n; i++) seg.set(tree1.in[i], tree2.in[i]);
seg.build();
for(int i=0; i<q; i++){
int t1 = E[i], t2 = S[i];
for(int j=21; ~j; j--) if(tree1.par[j][t1] <= R[i]) t1 = tree1.par[j][t1];
for(int j=21; ~j; j--) if(tree2.par[j][t2] >= L[i]) t2 = tree2.par[j][t2];
ret.push_back(seg.query(tree1.in[t1], tree1.out[t1], tree2.in[t2], tree2.out[t2]));
}
return ret;
}
#ifdef LOCAL
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int N, M, Q; cin >> N >> M >> Q;
vector<int> X, Y, S, E, L, R;
for(int i=0; i<M; i++){
int a, b; cin >> a >> b;
X.push_back(a); Y.push_back(b);
}
for(int i=0; i<Q; i++){
int a, b, c, d; cin >> a >> b >> c >> d;
S.push_back(a); E.push_back(b);
L.push_back(c); R.push_back(d);
}
auto res = check_validity(N, X, Y, S, E, L, R);
for(auto i : res) cout << i << "\n";
}
#endif
Compilation message
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:79:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
79 | for(auto &i : X) i++; for(auto &i : Y) i++; // I love 1-based!
| ^~~
werewolf.cpp:79:27: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
79 | for(auto &i : X) i++; for(auto &i : Y) i++; // I love 1-based!
| ^~~
werewolf.cpp:80:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
80 | for(auto &i : S) i++; for(auto &i : E) i++; // I love 1-based!
| ^~~
werewolf.cpp:80:27: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
80 | for(auto &i : S) i++; for(auto &i : E) i++; // I love 1-based!
| ^~~
werewolf.cpp:81:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
81 | for(auto &i : L) i++; for(auto &i : R) i++; // I love 1-based!
| ^~~
werewolf.cpp:81:27: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
81 | for(auto &i : L) i++; for(auto &i : R) i++; // I love 1-based!
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
29568 KB |
Output is correct |
2 |
Correct |
21 ms |
29568 KB |
Output is correct |
3 |
Correct |
21 ms |
29568 KB |
Output is correct |
4 |
Correct |
21 ms |
29440 KB |
Output is correct |
5 |
Correct |
21 ms |
29568 KB |
Output is correct |
6 |
Correct |
21 ms |
29568 KB |
Output is correct |
7 |
Correct |
21 ms |
29568 KB |
Output is correct |
8 |
Correct |
21 ms |
29564 KB |
Output is correct |
9 |
Correct |
21 ms |
29568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
29568 KB |
Output is correct |
2 |
Correct |
21 ms |
29568 KB |
Output is correct |
3 |
Correct |
21 ms |
29568 KB |
Output is correct |
4 |
Correct |
21 ms |
29440 KB |
Output is correct |
5 |
Correct |
21 ms |
29568 KB |
Output is correct |
6 |
Correct |
21 ms |
29568 KB |
Output is correct |
7 |
Correct |
21 ms |
29568 KB |
Output is correct |
8 |
Correct |
21 ms |
29564 KB |
Output is correct |
9 |
Correct |
21 ms |
29568 KB |
Output is correct |
10 |
Correct |
30 ms |
31032 KB |
Output is correct |
11 |
Correct |
30 ms |
30976 KB |
Output is correct |
12 |
Correct |
29 ms |
30976 KB |
Output is correct |
13 |
Correct |
30 ms |
31104 KB |
Output is correct |
14 |
Correct |
30 ms |
31096 KB |
Output is correct |
15 |
Correct |
31 ms |
31096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1501 ms |
125320 KB |
Output is correct |
2 |
Correct |
1207 ms |
128936 KB |
Output is correct |
3 |
Correct |
1238 ms |
126636 KB |
Output is correct |
4 |
Correct |
1416 ms |
125500 KB |
Output is correct |
5 |
Correct |
1503 ms |
125628 KB |
Output is correct |
6 |
Correct |
1593 ms |
125628 KB |
Output is correct |
7 |
Correct |
1451 ms |
125372 KB |
Output is correct |
8 |
Correct |
996 ms |
128940 KB |
Output is correct |
9 |
Correct |
840 ms |
126592 KB |
Output is correct |
10 |
Correct |
611 ms |
125608 KB |
Output is correct |
11 |
Correct |
660 ms |
125608 KB |
Output is correct |
12 |
Correct |
835 ms |
125372 KB |
Output is correct |
13 |
Correct |
1359 ms |
134196 KB |
Output is correct |
14 |
Correct |
1364 ms |
134324 KB |
Output is correct |
15 |
Correct |
1361 ms |
134332 KB |
Output is correct |
16 |
Correct |
1371 ms |
134204 KB |
Output is correct |
17 |
Correct |
1440 ms |
125372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
29568 KB |
Output is correct |
2 |
Correct |
21 ms |
29568 KB |
Output is correct |
3 |
Correct |
21 ms |
29568 KB |
Output is correct |
4 |
Correct |
21 ms |
29440 KB |
Output is correct |
5 |
Correct |
21 ms |
29568 KB |
Output is correct |
6 |
Correct |
21 ms |
29568 KB |
Output is correct |
7 |
Correct |
21 ms |
29568 KB |
Output is correct |
8 |
Correct |
21 ms |
29564 KB |
Output is correct |
9 |
Correct |
21 ms |
29568 KB |
Output is correct |
10 |
Correct |
30 ms |
31032 KB |
Output is correct |
11 |
Correct |
30 ms |
30976 KB |
Output is correct |
12 |
Correct |
29 ms |
30976 KB |
Output is correct |
13 |
Correct |
30 ms |
31104 KB |
Output is correct |
14 |
Correct |
30 ms |
31096 KB |
Output is correct |
15 |
Correct |
31 ms |
31096 KB |
Output is correct |
16 |
Correct |
1501 ms |
125320 KB |
Output is correct |
17 |
Correct |
1207 ms |
128936 KB |
Output is correct |
18 |
Correct |
1238 ms |
126636 KB |
Output is correct |
19 |
Correct |
1416 ms |
125500 KB |
Output is correct |
20 |
Correct |
1503 ms |
125628 KB |
Output is correct |
21 |
Correct |
1593 ms |
125628 KB |
Output is correct |
22 |
Correct |
1451 ms |
125372 KB |
Output is correct |
23 |
Correct |
996 ms |
128940 KB |
Output is correct |
24 |
Correct |
840 ms |
126592 KB |
Output is correct |
25 |
Correct |
611 ms |
125608 KB |
Output is correct |
26 |
Correct |
660 ms |
125608 KB |
Output is correct |
27 |
Correct |
835 ms |
125372 KB |
Output is correct |
28 |
Correct |
1359 ms |
134196 KB |
Output is correct |
29 |
Correct |
1364 ms |
134324 KB |
Output is correct |
30 |
Correct |
1361 ms |
134332 KB |
Output is correct |
31 |
Correct |
1371 ms |
134204 KB |
Output is correct |
32 |
Correct |
1440 ms |
125372 KB |
Output is correct |
33 |
Correct |
1604 ms |
125868 KB |
Output is correct |
34 |
Correct |
414 ms |
61436 KB |
Output is correct |
35 |
Correct |
1516 ms |
128572 KB |
Output is correct |
36 |
Correct |
1560 ms |
126380 KB |
Output is correct |
37 |
Correct |
1527 ms |
128044 KB |
Output is correct |
38 |
Correct |
1560 ms |
126760 KB |
Output is correct |
39 |
Correct |
1703 ms |
136328 KB |
Output is correct |
40 |
Correct |
1412 ms |
136276 KB |
Output is correct |
41 |
Correct |
974 ms |
127344 KB |
Output is correct |
42 |
Correct |
664 ms |
126252 KB |
Output is correct |
43 |
Correct |
1186 ms |
134024 KB |
Output is correct |
44 |
Correct |
1303 ms |
127792 KB |
Output is correct |
45 |
Correct |
918 ms |
136580 KB |
Output is correct |
46 |
Correct |
891 ms |
136236 KB |
Output is correct |
47 |
Correct |
1377 ms |
134440 KB |
Output is correct |
48 |
Correct |
1361 ms |
134268 KB |
Output is correct |
49 |
Correct |
1401 ms |
134464 KB |
Output is correct |
50 |
Correct |
1360 ms |
134588 KB |
Output is correct |
51 |
Correct |
1225 ms |
136124 KB |
Output is correct |
52 |
Correct |
1198 ms |
136384 KB |
Output is correct |