#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
const int mxN = 4e5 + 7;
const int SZ = exp2(ceil(log2(mxN)));
vector<int> seg[4*mxN];
vector<int> merge(vector<int> a, vector<int> b) {
vector<int> res;
int inda = 0, indb = 0;
while (inda < a.size() && indb < b.size()) {
if (a[inda] < b[indb]) {
res.push_back(a[inda++]);
}
else {
res.push_back(b[indb++]);
}
}
while (inda < a.size()) {
res.push_back(a[inda++]);
}
while (indb < b.size()) {
res.push_back(b[indb++]);
}
return res;
}
bool query(int lo, int hi, int a, int b, int ind = 1, int l = 0, int r = SZ - 1) {
if (lo > r || l > hi) {
return false;
}
if (lo <= l && r <= hi) {
auto it = lower_bound(seg[ind].begin(), seg[ind].end(), a);
return it != seg[ind].end() && *it <= b;
}
int mid = (l+r)/2;
return query(lo,hi,a,b,2*ind,l,mid) || query(lo,hi,a,b,2*ind+1,mid+1,r);
}
struct Query {
int s, e, l, r, ind;
};
bool cmpl(Query a, Query b) {
return a.l > b.l;
}
bool cmpr(Query a, Query b) {
return a.r < b.r;
}
int n, m, q, dsu[mxN];
Query que[mxN];
pair<int,int> rngl[mxN], rngr[mxN], rep[mxN];
vector<int> g1[mxN], g2[mxN];
int find(int x) {
return dsu[x] < 0 ? x : dsu[x] = find(dsu[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) {
return;
}
dsu[x] += dsu[y];
dsu[y] = x;
}
int cnt;
bool fl;
void dfs(int u) {
if (fl) rngr[u].first = cnt;
else rngl[u].first = cnt;
if (u < n) {
++cnt;
}
for (auto v : g2[u]) {
dfs(v);
}
g2[u].clear();
if (fl) rngr[u].second = cnt-1;
else rngl[u].second = cnt-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) {
n = N, m = X.size(), q = S.size();
for (int i = 0; i < m; ++i) {
g1[X[i]].push_back(Y[i]);
g1[Y[i]].push_back(X[i]);
}
for (int i = 0; i < q; ++i) {
que[i] = {S[i], E[i], L[i], R[i], i};
}
int ind = 0, id = n;
sort(que, que+q, cmpl);
memset(dsu, -1, sizeof(dsu));
for (int cur = n-1; cur >= 0; --cur) {
g2[id].push_back(cur);
unite(id, cur);
for (auto v : g1[cur]) {
if (v >= cur && find(v) != find(id)) {
g2[id].push_back(find(v));
unite(id, v);
}
}
++id;
while (ind < q && que[ind].l >= cur) {
rep[que[ind].ind].first = find(que[ind].s);
++ind;
}
}
cnt = 0, fl = 0;
dfs(id-1);
ind = 0, id = n;
sort(que, que+q, cmpr);
memset(dsu, -1, sizeof(dsu));
for (int cur = 0; cur <= n-1; ++cur) {
g2[id].push_back(cur);
unite(id, cur);
for (auto v : g1[cur]) {
if (v <= cur && find(v) != find(id)) {
g2[id].push_back(find(v));
unite(id, v);
}
}
++id;
while (ind < q && que[ind].r <= cur) {
rep[que[ind].ind].second = find(que[ind].e);
++ind;
}
}
cnt = 0, fl = 1;
dfs(id-1);
/*
for (int i = 0; i < n; ++i) {
update(rngl[i].first, rngr[i].first);
}
*/
for (int i = 0; i < n; ++i) {
seg[rngl[i].first+SZ].push_back(rngr[i].first);
}
for (int i = SZ-1; i >= 0; --i) {
seg[i] = merge(seg[2*i], seg[2*i+1]);
}
vector<int> ans(q);
for (int i = 0; i < q; ++i) {
auto [a, b] = rep[que[i].ind];
ans[que[i].ind] =
query(rngl[a].first, rngl[a].second, rngr[b].first, rngr[b].second);
}
return ans;
}
Compilation message
werewolf.cpp: In function 'std::vector<int> merge(std::vector<int>, std::vector<int>)':
werewolf.cpp:13:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | while (inda < a.size() && indb < b.size()) {
| ~~~~~^~~~~~~~~~
werewolf.cpp:13:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | while (inda < a.size() && indb < b.size()) {
| ~~~~~^~~~~~~~~~
werewolf.cpp:22:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | while (inda < a.size()) {
| ~~~~~^~~~~~~~~~
werewolf.cpp:26:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | while (indb < b.size()) {
| ~~~~~^~~~~~~~~~
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:173:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
173 | auto [a, b] = rep[que[i].ind];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
67396 KB |
Output is correct |
2 |
Correct |
23 ms |
67364 KB |
Output is correct |
3 |
Correct |
24 ms |
67420 KB |
Output is correct |
4 |
Correct |
25 ms |
67164 KB |
Output is correct |
5 |
Correct |
22 ms |
67164 KB |
Output is correct |
6 |
Correct |
24 ms |
67164 KB |
Output is correct |
7 |
Correct |
27 ms |
67164 KB |
Output is correct |
8 |
Correct |
22 ms |
67388 KB |
Output is correct |
9 |
Correct |
22 ms |
67164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
67396 KB |
Output is correct |
2 |
Correct |
23 ms |
67364 KB |
Output is correct |
3 |
Correct |
24 ms |
67420 KB |
Output is correct |
4 |
Correct |
25 ms |
67164 KB |
Output is correct |
5 |
Correct |
22 ms |
67164 KB |
Output is correct |
6 |
Correct |
24 ms |
67164 KB |
Output is correct |
7 |
Correct |
27 ms |
67164 KB |
Output is correct |
8 |
Correct |
22 ms |
67388 KB |
Output is correct |
9 |
Correct |
22 ms |
67164 KB |
Output is correct |
10 |
Correct |
27 ms |
68132 KB |
Output is correct |
11 |
Correct |
33 ms |
68116 KB |
Output is correct |
12 |
Correct |
27 ms |
68204 KB |
Output is correct |
13 |
Correct |
28 ms |
68428 KB |
Output is correct |
14 |
Correct |
34 ms |
68180 KB |
Output is correct |
15 |
Correct |
28 ms |
68180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
556 ms |
127628 KB |
Output is correct |
2 |
Correct |
439 ms |
135148 KB |
Output is correct |
3 |
Correct |
409 ms |
132652 KB |
Output is correct |
4 |
Correct |
463 ms |
131780 KB |
Output is correct |
5 |
Correct |
476 ms |
131788 KB |
Output is correct |
6 |
Correct |
535 ms |
131696 KB |
Output is correct |
7 |
Correct |
562 ms |
131592 KB |
Output is correct |
8 |
Correct |
419 ms |
134972 KB |
Output is correct |
9 |
Correct |
379 ms |
132668 KB |
Output is correct |
10 |
Correct |
436 ms |
131648 KB |
Output is correct |
11 |
Correct |
474 ms |
131724 KB |
Output is correct |
12 |
Correct |
454 ms |
131712 KB |
Output is correct |
13 |
Correct |
532 ms |
136264 KB |
Output is correct |
14 |
Correct |
451 ms |
136268 KB |
Output is correct |
15 |
Correct |
445 ms |
136284 KB |
Output is correct |
16 |
Correct |
465 ms |
136136 KB |
Output is correct |
17 |
Correct |
502 ms |
131644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
67396 KB |
Output is correct |
2 |
Correct |
23 ms |
67364 KB |
Output is correct |
3 |
Correct |
24 ms |
67420 KB |
Output is correct |
4 |
Correct |
25 ms |
67164 KB |
Output is correct |
5 |
Correct |
22 ms |
67164 KB |
Output is correct |
6 |
Correct |
24 ms |
67164 KB |
Output is correct |
7 |
Correct |
27 ms |
67164 KB |
Output is correct |
8 |
Correct |
22 ms |
67388 KB |
Output is correct |
9 |
Correct |
22 ms |
67164 KB |
Output is correct |
10 |
Correct |
27 ms |
68132 KB |
Output is correct |
11 |
Correct |
33 ms |
68116 KB |
Output is correct |
12 |
Correct |
27 ms |
68204 KB |
Output is correct |
13 |
Correct |
28 ms |
68428 KB |
Output is correct |
14 |
Correct |
34 ms |
68180 KB |
Output is correct |
15 |
Correct |
28 ms |
68180 KB |
Output is correct |
16 |
Correct |
556 ms |
127628 KB |
Output is correct |
17 |
Correct |
439 ms |
135148 KB |
Output is correct |
18 |
Correct |
409 ms |
132652 KB |
Output is correct |
19 |
Correct |
463 ms |
131780 KB |
Output is correct |
20 |
Correct |
476 ms |
131788 KB |
Output is correct |
21 |
Correct |
535 ms |
131696 KB |
Output is correct |
22 |
Correct |
562 ms |
131592 KB |
Output is correct |
23 |
Correct |
419 ms |
134972 KB |
Output is correct |
24 |
Correct |
379 ms |
132668 KB |
Output is correct |
25 |
Correct |
436 ms |
131648 KB |
Output is correct |
26 |
Correct |
474 ms |
131724 KB |
Output is correct |
27 |
Correct |
454 ms |
131712 KB |
Output is correct |
28 |
Correct |
532 ms |
136264 KB |
Output is correct |
29 |
Correct |
451 ms |
136268 KB |
Output is correct |
30 |
Correct |
445 ms |
136284 KB |
Output is correct |
31 |
Correct |
465 ms |
136136 KB |
Output is correct |
32 |
Correct |
502 ms |
131644 KB |
Output is correct |
33 |
Correct |
606 ms |
133012 KB |
Output is correct |
34 |
Correct |
244 ms |
102744 KB |
Output is correct |
35 |
Correct |
574 ms |
135516 KB |
Output is correct |
36 |
Correct |
568 ms |
132336 KB |
Output is correct |
37 |
Correct |
579 ms |
134932 KB |
Output is correct |
38 |
Correct |
569 ms |
132720 KB |
Output is correct |
39 |
Correct |
474 ms |
141524 KB |
Output is correct |
40 |
Correct |
556 ms |
140968 KB |
Output is correct |
41 |
Correct |
505 ms |
133976 KB |
Output is correct |
42 |
Correct |
445 ms |
132328 KB |
Output is correct |
43 |
Correct |
498 ms |
139052 KB |
Output is correct |
44 |
Correct |
514 ms |
134676 KB |
Output is correct |
45 |
Correct |
417 ms |
141656 KB |
Output is correct |
46 |
Correct |
413 ms |
141416 KB |
Output is correct |
47 |
Correct |
444 ms |
136432 KB |
Output is correct |
48 |
Correct |
435 ms |
136180 KB |
Output is correct |
49 |
Correct |
449 ms |
136484 KB |
Output is correct |
50 |
Correct |
438 ms |
136224 KB |
Output is correct |
51 |
Correct |
516 ms |
141600 KB |
Output is correct |
52 |
Correct |
515 ms |
141644 KB |
Output is correct |