#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;
}
void update(int ind, int val) {
seg[ind+=SZ].push_back(val);
while (ind /= 2) {
seg[ind] = merge(seg[2*ind], seg[2*ind+1]);
}
}
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;
}
bool cmpi(Query a, Query b) {
return a.ind < b.ind;
}
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);
}
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;
}
}
/*
for (int i = id-1; i >= 0; --i) {
cout << i << ":\n";
for (auto v : g2[i]) {
cout << v << ' ';
}
cout << "\n----\n";
}
*/
cnt = 0, fl = 0;
dfs(id-1);
for (int i = 0; i < id; ++i) {
g2[i].clear();
}
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) {
//cout << rngl[i].first << ' ' << rngr[i].first << "\n";
update(rngl[i].first, rngr[i].first);
}
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:187:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
187 | auto [a, b] = rep[que[i].ind];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
58204 KB |
Output is correct |
2 |
Correct |
18 ms |
58244 KB |
Output is correct |
3 |
Correct |
19 ms |
58456 KB |
Output is correct |
4 |
Correct |
16 ms |
58200 KB |
Output is correct |
5 |
Correct |
18 ms |
58204 KB |
Output is correct |
6 |
Correct |
16 ms |
58200 KB |
Output is correct |
7 |
Correct |
16 ms |
58204 KB |
Output is correct |
8 |
Correct |
17 ms |
58204 KB |
Output is correct |
9 |
Correct |
17 ms |
58252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
58204 KB |
Output is correct |
2 |
Correct |
18 ms |
58244 KB |
Output is correct |
3 |
Correct |
19 ms |
58456 KB |
Output is correct |
4 |
Correct |
16 ms |
58200 KB |
Output is correct |
5 |
Correct |
18 ms |
58204 KB |
Output is correct |
6 |
Correct |
16 ms |
58200 KB |
Output is correct |
7 |
Correct |
16 ms |
58204 KB |
Output is correct |
8 |
Correct |
17 ms |
58204 KB |
Output is correct |
9 |
Correct |
17 ms |
58252 KB |
Output is correct |
10 |
Correct |
106 ms |
59408 KB |
Output is correct |
11 |
Correct |
105 ms |
59492 KB |
Output is correct |
12 |
Correct |
95 ms |
59420 KB |
Output is correct |
13 |
Correct |
109 ms |
59600 KB |
Output is correct |
14 |
Correct |
114 ms |
59472 KB |
Output is correct |
15 |
Correct |
101 ms |
59932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4072 ms |
104568 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
58204 KB |
Output is correct |
2 |
Correct |
18 ms |
58244 KB |
Output is correct |
3 |
Correct |
19 ms |
58456 KB |
Output is correct |
4 |
Correct |
16 ms |
58200 KB |
Output is correct |
5 |
Correct |
18 ms |
58204 KB |
Output is correct |
6 |
Correct |
16 ms |
58200 KB |
Output is correct |
7 |
Correct |
16 ms |
58204 KB |
Output is correct |
8 |
Correct |
17 ms |
58204 KB |
Output is correct |
9 |
Correct |
17 ms |
58252 KB |
Output is correct |
10 |
Correct |
106 ms |
59408 KB |
Output is correct |
11 |
Correct |
105 ms |
59492 KB |
Output is correct |
12 |
Correct |
95 ms |
59420 KB |
Output is correct |
13 |
Correct |
109 ms |
59600 KB |
Output is correct |
14 |
Correct |
114 ms |
59472 KB |
Output is correct |
15 |
Correct |
101 ms |
59932 KB |
Output is correct |
16 |
Execution timed out |
4072 ms |
104568 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |