#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, q, num[200200];
vector<int> ans, adj[200200];
struct Query{
int s, e, l, r, i;
Query(){}
Query(int _s, int _e, int _l, int _r, int _i): s(_s), e(_e), l(_l), r(_r), i(_i) {}
bool operator<(const Query &Q) const{
return l > Q.l;
}
};
vector<Query> Q;
struct Range{
int l, r, t;
Range(){}
Range(int _l, int _r, int _t): l(_l), r(_r), t(_t) {}
bool operator<(const Range &R) const{
return t < R.t;
}
};
vector<Range> ran[200200], root[200200];
struct DSU1{
int path[200200];
vector<int> S[200200];
void init(int n){for (int i=0;i<n;i++){path[i] = i; S[i] = {i};}}
int find(int s){
if (path[s]==s) return s;
return find(path[s]);
}
void merge(int s, int v, int t){
s = find(s), v = find(v);
if (s==v) return;
if (S[s].size() > S[v].size()) swap(s, v);
int sz = S[v].size();
for (auto &x:S[s]){
num[x] += sz;
if (!root[x].empty() && root[x].back().t==t) root[x].back().l = v;
else root[x].emplace_back(v, -1, t);
S[v].push_back(x);
}
path[s] = v;
}
void update(int s, int t){
s = find(s);
ran[s].emplace_back(S[s][0], S[s].back(), t);
}
}dsu1;
struct DSU2{
int path[200200];
set<int> S[200200];
void init(int n){for (int i=0;i<n;i++){path[i] = i; S[i].insert(i);}}
int find(int s){
if (path[s]==s) return s;
return find(path[s]);
}
void merge(int s, int v){
s = find(s), v = find(v);
if (s==v) return;
if (S[s].size() > S[v].size()) swap(s, v);
for (auto &x:S[s]) S[v].insert(x);
path[s] = v;
}
bool valid(int s, int l, int r){
s = find(s);
auto iter = S[s].lower_bound(l);
return (iter!=S[s].end() && *iter<=r);
}
}dsu2;
void init(){ ///renumbering
dsu1.init(n);
for (int i=0;i<n;i++){
root[i].emplace_back(i, -1, -1);
ran[i].emplace_back(i, i, -1);
}
for (int i=0;i<n;i++){
for (auto &v:adj[i]) if (v<i) dsu1.merge(v, i, i);
dsu1.update(i, i);
}
for (int i=0;i<n;i++){
for (auto &q:ran[i]) q.l = num[q.l], q.r = num[q.r];
}
}
pair<int, int> get_range(int s, int t){
auto iter = upper_bound(root[s].begin(), root[s].end(), Range(-1, -1, t));
s = prev(iter)->l;
iter = --upper_bound(ran[s].begin(), ran[s].end(), Range(-1, -1, t));
return {iter->l, iter->r};
}
std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y,
std::vector<int> S, std::vector<int> E,
std::vector<int> L, std::vector<int> R) {
n = N, m = X.size(), q = S.size();
ans.resize(q);
for (int i=0;i<m;i++){
adj[X[i]].push_back(Y[i]);
adj[Y[i]].push_back(X[i]);
}
for (int i=0;i<q;i++) Q.emplace_back(S[i], E[i], L[i], R[i], i);
sort(Q.begin(), Q.end());
//printf("YES\n");
init();
dsu2.init(n);
//printf("YES\n");
//printf("num: ");
//for (int i=0;i<n;i++) printf("%d ", num[i]);
//printf("\n");
int pt = 0;
for (int i=n-1;i>=0;i--){
for (auto &v:adj[i]) if (v>i) dsu2.merge(num[v], num[i]);
for (;pt<q && Q[pt].l==i;pt++){
auto p = get_range(Q[pt].e, Q[pt].r);
//if (Q[pt].s==8 && Q[pt].e==3) printf(" %d %d\n", p.first, p.second);
if (dsu2.valid(num[Q[pt].s], p.first, p.second)) ans[Q[pt].i] = 1;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
28492 KB |
Output is correct |
2 |
Correct |
16 ms |
28496 KB |
Output is correct |
3 |
Correct |
14 ms |
28428 KB |
Output is correct |
4 |
Correct |
14 ms |
28520 KB |
Output is correct |
5 |
Correct |
14 ms |
28548 KB |
Output is correct |
6 |
Correct |
16 ms |
28484 KB |
Output is correct |
7 |
Correct |
14 ms |
28496 KB |
Output is correct |
8 |
Correct |
16 ms |
28448 KB |
Output is correct |
9 |
Correct |
16 ms |
28620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
28492 KB |
Output is correct |
2 |
Correct |
16 ms |
28496 KB |
Output is correct |
3 |
Correct |
14 ms |
28428 KB |
Output is correct |
4 |
Correct |
14 ms |
28520 KB |
Output is correct |
5 |
Correct |
14 ms |
28548 KB |
Output is correct |
6 |
Correct |
16 ms |
28484 KB |
Output is correct |
7 |
Correct |
14 ms |
28496 KB |
Output is correct |
8 |
Correct |
16 ms |
28448 KB |
Output is correct |
9 |
Correct |
16 ms |
28620 KB |
Output is correct |
10 |
Correct |
25 ms |
29676 KB |
Output is correct |
11 |
Correct |
21 ms |
29776 KB |
Output is correct |
12 |
Correct |
21 ms |
30200 KB |
Output is correct |
13 |
Correct |
20 ms |
29776 KB |
Output is correct |
14 |
Correct |
23 ms |
29944 KB |
Output is correct |
15 |
Correct |
20 ms |
29948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1039 ms |
168036 KB |
Output is correct |
2 |
Correct |
553 ms |
116624 KB |
Output is correct |
3 |
Correct |
608 ms |
132056 KB |
Output is correct |
4 |
Correct |
714 ms |
151844 KB |
Output is correct |
5 |
Correct |
697 ms |
155680 KB |
Output is correct |
6 |
Correct |
874 ms |
163860 KB |
Output is correct |
7 |
Correct |
1033 ms |
177352 KB |
Output is correct |
8 |
Correct |
525 ms |
116384 KB |
Output is correct |
9 |
Correct |
518 ms |
132000 KB |
Output is correct |
10 |
Correct |
623 ms |
151836 KB |
Output is correct |
11 |
Correct |
656 ms |
154140 KB |
Output is correct |
12 |
Correct |
804 ms |
164296 KB |
Output is correct |
13 |
Correct |
498 ms |
112560 KB |
Output is correct |
14 |
Correct |
545 ms |
112564 KB |
Output is correct |
15 |
Correct |
486 ms |
112588 KB |
Output is correct |
16 |
Correct |
477 ms |
112540 KB |
Output is correct |
17 |
Correct |
1024 ms |
174868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
28492 KB |
Output is correct |
2 |
Correct |
16 ms |
28496 KB |
Output is correct |
3 |
Correct |
14 ms |
28428 KB |
Output is correct |
4 |
Correct |
14 ms |
28520 KB |
Output is correct |
5 |
Correct |
14 ms |
28548 KB |
Output is correct |
6 |
Correct |
16 ms |
28484 KB |
Output is correct |
7 |
Correct |
14 ms |
28496 KB |
Output is correct |
8 |
Correct |
16 ms |
28448 KB |
Output is correct |
9 |
Correct |
16 ms |
28620 KB |
Output is correct |
10 |
Correct |
25 ms |
29676 KB |
Output is correct |
11 |
Correct |
21 ms |
29776 KB |
Output is correct |
12 |
Correct |
21 ms |
30200 KB |
Output is correct |
13 |
Correct |
20 ms |
29776 KB |
Output is correct |
14 |
Correct |
23 ms |
29944 KB |
Output is correct |
15 |
Correct |
20 ms |
29948 KB |
Output is correct |
16 |
Correct |
1039 ms |
168036 KB |
Output is correct |
17 |
Correct |
553 ms |
116624 KB |
Output is correct |
18 |
Correct |
608 ms |
132056 KB |
Output is correct |
19 |
Correct |
714 ms |
151844 KB |
Output is correct |
20 |
Correct |
697 ms |
155680 KB |
Output is correct |
21 |
Correct |
874 ms |
163860 KB |
Output is correct |
22 |
Correct |
1033 ms |
177352 KB |
Output is correct |
23 |
Correct |
525 ms |
116384 KB |
Output is correct |
24 |
Correct |
518 ms |
132000 KB |
Output is correct |
25 |
Correct |
623 ms |
151836 KB |
Output is correct |
26 |
Correct |
656 ms |
154140 KB |
Output is correct |
27 |
Correct |
804 ms |
164296 KB |
Output is correct |
28 |
Correct |
498 ms |
112560 KB |
Output is correct |
29 |
Correct |
545 ms |
112564 KB |
Output is correct |
30 |
Correct |
486 ms |
112588 KB |
Output is correct |
31 |
Correct |
477 ms |
112540 KB |
Output is correct |
32 |
Correct |
1024 ms |
174868 KB |
Output is correct |
33 |
Correct |
854 ms |
129108 KB |
Output is correct |
34 |
Correct |
267 ms |
64988 KB |
Output is correct |
35 |
Correct |
792 ms |
115016 KB |
Output is correct |
36 |
Correct |
858 ms |
133960 KB |
Output is correct |
37 |
Correct |
798 ms |
116524 KB |
Output is correct |
38 |
Correct |
915 ms |
129456 KB |
Output is correct |
39 |
Correct |
668 ms |
125612 KB |
Output is correct |
40 |
Correct |
825 ms |
127580 KB |
Output is correct |
41 |
Correct |
718 ms |
118212 KB |
Output is correct |
42 |
Correct |
836 ms |
133932 KB |
Output is correct |
43 |
Correct |
722 ms |
115116 KB |
Output is correct |
44 |
Correct |
739 ms |
116636 KB |
Output is correct |
45 |
Correct |
567 ms |
117744 KB |
Output is correct |
46 |
Correct |
621 ms |
127012 KB |
Output is correct |
47 |
Correct |
547 ms |
112688 KB |
Output is correct |
48 |
Correct |
508 ms |
112552 KB |
Output is correct |
49 |
Correct |
546 ms |
112588 KB |
Output is correct |
50 |
Correct |
567 ms |
112540 KB |
Output is correct |
51 |
Correct |
815 ms |
126296 KB |
Output is correct |
52 |
Correct |
766 ms |
126268 KB |
Output is correct |