#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
struct segtree {
vector<int> T;
int get(int idx, int s, int e, int l, int r) {
if(r < s || e < l) return 0;
if(l <= s && e <= r) return T[idx];
int m = s+e >> 1;
return get(idx*2, s, m, l, r) + get(idx*2+1, m+1, e, l, r);
}
void upd(int idx, int s, int e, int p, int v) {
if(p < s || e < p) return;
if(s == e) {
T[idx] = v;
return;
}
int m = s+e >> 1;
upd(idx*2, s, m, p, v);
upd(idx*2+1, m+1, e, p, v);
T[idx] = T[idx*2] + T[idx*2+1];
}
segtree(int sz) { T.resize(4*sz); }
};
struct UF {
int P[400009], X[400009];
int root(int x) {
if(P[x] == x) return x;
return P[x] = root(P[x]);
}
pii merg(int u, int v, int Xi) {
u = root(u); v = root(v);
int a = X[u], b = X[v];
X[v] = X[u] = Xi;
P[u] = v;
return (pii){a, b};
}
UF(int sz) {
for(int i=0; i<sz; i++) P[i] = i, X[i] = i;
}
};
const int FF = 400009;
bool HVS[FF], WVS[FF];
int ans[FF], x[FF], ys[FF], ye[FF], C[FF], lineId[FF];
int Hw[FF], Ww[FF], HP[22][FF], WP[22][FF], l, Hi[FF], Ho[FF], Wi[FF], Wo[FF], Ht = 1, Wt = 1;
vector<int> adj[FF], HT[FF], WT[FF];
void WA() {
exit(0);
}
void Hdfs(int x, int p) {
Hi[x] = Ht;
if((int)HT[x].size() == 0) ++Ht;
HP[0][x] = p;
for(auto& it: HT[x]) Hdfs(it, x);
Ho[x] = Ht-1 ;
}
void Wdfs(int x, int p) {
Wi[x] = Wt;
if((int)WT[x].size() == 0) ++Wt;
WP[0][x] = p;
for(auto& it: WT[x]) Wdfs(it, x);
Wo[x] = Wt-1 ;
}
UF HUF(400001), WUF(400001);
vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
int M = X.size(), Q = S.size();
for(int i=0; i<M; i++) {
int u = X[i], v = Y[i];
adj[u].push_back(v);
adj[v].push_back(u);
}
int I = N - 1;
for(int i=N-1; i>=0; i--) {
for(auto& it: adj[i]) {
if(it < i) continue;
int u = i, v = it;
if(HUF.root(u) != HUF.root(v)) {
int a, b; tie(a, b) = HUF.merg(u, v, ++I);
HT[I].push_back(a);
HT[I].push_back(b);
Hw[I] = i;
}
}
}
Hdfs(I, I);
I = N - 1;
for(int i=0; i<N; i++) {
for(auto& it: adj[i]) {
if(it > i) continue;
int u = i, v = it;
if(WUF.root(u) != WUF.root(v)) {
int a, b; tie(a, b) = WUF.merg(u, v, ++I);
WT[I].push_back(a);
WT[I].push_back(b);
Ww[I] = i;
}
}
}
Wdfs(I, I);
/*
puts("DFS Ordering");
for(int i=0; i<N; i++) printf("%d ", Hi[i]);
puts("");
for(int i=0; i<N; i++) printf("%d ", Wi[i]);
puts("\n");
*/
for(int i=1; (1<<i)<=I; i++) {
for(int j=0; j<=I; j++) {
HP[i][j] = HP[i-1][HP[i-1][j]];
WP[i][j] = WP[i-1][WP[i-1][j]];
}
l = i;
}
vector<pii> evt;
for(int i=0; i<N; i++) evt.push_back({1, i});
int lineIdx = 0;
for(int i=0; i<Q; i++) {
int Hnow = S[i], Wnow = E[i];
for(int j=l; j>=0; j--) if(Hw[HP[j][Hnow]] >= L[i]) Hnow = HP[j][Hnow];
for(int j=l; j>=0; j--) if(Ww[WP[j][Wnow]] <= R[i]) Wnow = WP[j][Wnow];
x[++lineIdx] = Hi[Hnow] - 1; C[lineIdx] = -1;
ys[lineIdx] = Wi[Wnow]; ye[lineIdx] = Wo[Wnow];
lineId[lineIdx] = i;
evt.push_back({2, lineIdx});
x[++lineIdx] = Ho[Hnow]; C[lineIdx] = +1;
ys[lineIdx] = Wi[Wnow]; ye[lineIdx] = Wo[Wnow];
lineId[lineIdx] = i;
evt.push_back({2, lineIdx});
//printf("rectangle : (%d, %d, %d, %d)\n", Hi[Hnow], Wi[Wnow], Ho[Hnow], Wo[Wnow]);
}
sort(evt.begin(), evt.end(), [&](pii A, pii B) {
int Aty, Aid, Bty, Bid;
tie(Aty, Aid) = A; tie(Bty, Bid) = B;
int ax = (Aty == 1 ? Hi[Aid] : x[Aid]), bx = (Bty == 1 ? Hi[Bid] : x[Bid]);
if(ax == bx) return Aty < Bty;
return ax < bx;
});
segtree seg(N);
for(auto& it: evt) {
int ty, id; tie(ty, id) = it;
if(ty == 1) {
seg.upd(1, 1, N, Wi[id], 1);
//printf("dot update: vertex: %d, (%d, %d)\n", id, Hi[id], Wi[id]);
}
if(ty == 2) ans[lineId[id]] += C[id] * seg.get(1, 1, N, ys[id], ye[id]);
}
vector<int> A(Q, 0);
for(int i=0; i<Q; i++) if(ans[i]) A[i] = 1;
return A;
}
Compilation message
werewolf.cpp: In member function 'int segtree::get(int, int, int, int, int)':
werewolf.cpp:12:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
12 | int m = s+e >> 1;
| ~^~
werewolf.cpp: In member function 'void segtree::upd(int, int, int, int, int)':
werewolf.cpp:21:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
21 | int m = s+e >> 1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
34892 KB |
Output is correct |
2 |
Correct |
18 ms |
34936 KB |
Output is correct |
3 |
Correct |
17 ms |
34840 KB |
Output is correct |
4 |
Correct |
17 ms |
34808 KB |
Output is correct |
5 |
Correct |
16 ms |
34916 KB |
Output is correct |
6 |
Correct |
17 ms |
34892 KB |
Output is correct |
7 |
Correct |
19 ms |
34984 KB |
Output is correct |
8 |
Correct |
17 ms |
34960 KB |
Output is correct |
9 |
Correct |
17 ms |
34948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
34892 KB |
Output is correct |
2 |
Correct |
18 ms |
34936 KB |
Output is correct |
3 |
Correct |
17 ms |
34840 KB |
Output is correct |
4 |
Correct |
17 ms |
34808 KB |
Output is correct |
5 |
Correct |
16 ms |
34916 KB |
Output is correct |
6 |
Correct |
17 ms |
34892 KB |
Output is correct |
7 |
Correct |
19 ms |
34984 KB |
Output is correct |
8 |
Correct |
17 ms |
34960 KB |
Output is correct |
9 |
Correct |
17 ms |
34948 KB |
Output is correct |
10 |
Correct |
21 ms |
36672 KB |
Output is correct |
11 |
Correct |
23 ms |
36552 KB |
Output is correct |
12 |
Correct |
22 ms |
36560 KB |
Output is correct |
13 |
Correct |
24 ms |
36732 KB |
Output is correct |
14 |
Correct |
23 ms |
36720 KB |
Output is correct |
15 |
Correct |
23 ms |
36724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1114 ms |
147504 KB |
Output is correct |
2 |
Correct |
1133 ms |
160688 KB |
Output is correct |
3 |
Correct |
1060 ms |
157592 KB |
Output is correct |
4 |
Correct |
992 ms |
156140 KB |
Output is correct |
5 |
Correct |
1016 ms |
156224 KB |
Output is correct |
6 |
Correct |
1126 ms |
156004 KB |
Output is correct |
7 |
Correct |
1008 ms |
155924 KB |
Output is correct |
8 |
Correct |
955 ms |
160516 KB |
Output is correct |
9 |
Correct |
696 ms |
157548 KB |
Output is correct |
10 |
Correct |
597 ms |
156216 KB |
Output is correct |
11 |
Correct |
665 ms |
156192 KB |
Output is correct |
12 |
Correct |
594 ms |
155944 KB |
Output is correct |
13 |
Correct |
1104 ms |
160624 KB |
Output is correct |
14 |
Correct |
1122 ms |
160548 KB |
Output is correct |
15 |
Correct |
1138 ms |
160644 KB |
Output is correct |
16 |
Correct |
1231 ms |
160716 KB |
Output is correct |
17 |
Correct |
1029 ms |
155928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
34892 KB |
Output is correct |
2 |
Correct |
18 ms |
34936 KB |
Output is correct |
3 |
Correct |
17 ms |
34840 KB |
Output is correct |
4 |
Correct |
17 ms |
34808 KB |
Output is correct |
5 |
Correct |
16 ms |
34916 KB |
Output is correct |
6 |
Correct |
17 ms |
34892 KB |
Output is correct |
7 |
Correct |
19 ms |
34984 KB |
Output is correct |
8 |
Correct |
17 ms |
34960 KB |
Output is correct |
9 |
Correct |
17 ms |
34948 KB |
Output is correct |
10 |
Correct |
21 ms |
36672 KB |
Output is correct |
11 |
Correct |
23 ms |
36552 KB |
Output is correct |
12 |
Correct |
22 ms |
36560 KB |
Output is correct |
13 |
Correct |
24 ms |
36732 KB |
Output is correct |
14 |
Correct |
23 ms |
36720 KB |
Output is correct |
15 |
Correct |
23 ms |
36724 KB |
Output is correct |
16 |
Correct |
1114 ms |
147504 KB |
Output is correct |
17 |
Correct |
1133 ms |
160688 KB |
Output is correct |
18 |
Correct |
1060 ms |
157592 KB |
Output is correct |
19 |
Correct |
992 ms |
156140 KB |
Output is correct |
20 |
Correct |
1016 ms |
156224 KB |
Output is correct |
21 |
Correct |
1126 ms |
156004 KB |
Output is correct |
22 |
Correct |
1008 ms |
155924 KB |
Output is correct |
23 |
Correct |
955 ms |
160516 KB |
Output is correct |
24 |
Correct |
696 ms |
157548 KB |
Output is correct |
25 |
Correct |
597 ms |
156216 KB |
Output is correct |
26 |
Correct |
665 ms |
156192 KB |
Output is correct |
27 |
Correct |
594 ms |
155944 KB |
Output is correct |
28 |
Correct |
1104 ms |
160624 KB |
Output is correct |
29 |
Correct |
1122 ms |
160548 KB |
Output is correct |
30 |
Correct |
1138 ms |
160644 KB |
Output is correct |
31 |
Correct |
1231 ms |
160716 KB |
Output is correct |
32 |
Correct |
1029 ms |
155928 KB |
Output is correct |
33 |
Correct |
1336 ms |
157348 KB |
Output is correct |
34 |
Correct |
339 ms |
78264 KB |
Output is correct |
35 |
Correct |
1269 ms |
160680 KB |
Output is correct |
36 |
Correct |
1247 ms |
156660 KB |
Output is correct |
37 |
Correct |
1248 ms |
159804 KB |
Output is correct |
38 |
Correct |
1233 ms |
157408 KB |
Output is correct |
39 |
Correct |
1148 ms |
165572 KB |
Output is correct |
40 |
Correct |
1121 ms |
165664 KB |
Output is correct |
41 |
Correct |
898 ms |
159240 KB |
Output is correct |
42 |
Correct |
630 ms |
156744 KB |
Output is correct |
43 |
Correct |
1090 ms |
164468 KB |
Output is correct |
44 |
Correct |
1198 ms |
159760 KB |
Output is correct |
45 |
Correct |
887 ms |
165968 KB |
Output is correct |
46 |
Correct |
920 ms |
165516 KB |
Output is correct |
47 |
Correct |
1049 ms |
160692 KB |
Output is correct |
48 |
Correct |
1042 ms |
160628 KB |
Output is correct |
49 |
Correct |
1131 ms |
160804 KB |
Output is correct |
50 |
Correct |
1102 ms |
160764 KB |
Output is correct |
51 |
Correct |
867 ms |
166520 KB |
Output is correct |
52 |
Correct |
876 ms |
166444 KB |
Output is correct |