#include "werewolf.h"
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define X first
#define Y second
#define ub upper_bound
#define lb lower_bound
using namespace std;
/*
- Make Reachability Tree by sorting indexes and DSU
- Subtree of Vertex includes all Vertices reachable from that vertex
- Connect new vertex to parent of new connections
- Then use binary lifting to find highest vertex in subtree with val <= to query
- Finally use euler tour pre/ post vals to find rectangle needed to be queried
- Offline + Fenwick Tree to check if there is a vertex/point in rectangle
*/
struct disj{
vector<int> par;
disj(int n){
for(int i = 0; i<n; i++)par.pb(i);
}
int fi(int a){return (a == par[a]) ? a : par[a] = fi(par[a]);}
void me(int a, int b){
a = fi(a);
b = fi(b);
par[b] = a;
}
};
struct ft{
vector<int> fen;
ft(int n){
fen.assign(n+1, 0);
}
int qer(int a){
a++;
int ans = 0;
for(; a > 0; a-=a&(-a))ans+=fen[a];
return ans;
}
void upd(int a, int b){
a++;
for(; a < fen.size(); a+=a&(-a))fen[a]+=b;
}
};
void print(vector<vector<int>>& g2){
int k = 0;
for(auto i : g2){
cout << k <<':';k++;
for(auto j : i){
cout << j << ' ';
}
cout << '\n';
}
}
void dfs(int v, int p, int& counter, vector<vector<int>>& g, vector<vector<int>>& up, vector<pii>& time){
up[v][0] = p;
time[v].X = counter++;
for(int i = 1; i < 30; i++){
up[v][i] = up[up[v][i-1]][i-1];
}
for(auto ch : g[v])dfs(ch, v, counter, g, up, time);
time[v].Y = counter++;
}
int jump1(int v, int val, vector<vector<int>>& up){
for(int i = 29; i >= 0; i--){
if(up[v][i] <= val){
v = up[v][i];
}
}
return v;
}
int jump2(int v, int val, vector<vector<int>>& up){
for(int i = 29; i >= 0; i--){
if(up[v][i] >= val){
v = up[v][i];
}
}
return v;
}
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 Q = S.size(); int M = X.size();
vector<vector<int>> g;
g.assign(N, vector<int>());
for(int i = 0; i<M; i++){
g[X[i]].pb(Y[i]);
g[Y[i]].pb(X[i]);
}
disj sml(N), big(N);
vector<vector<int>> g1, g2, up1, up2;
vector<pii> time1, time2;
g1.assign(N, vector<int>());
g2.assign(N, vector<int>());
up1.assign(N, vector<int>(30));
up2.assign(N, vector<int>(30));
time1.assign(N, {0, 0});
time2.assign(N, {0, 0});
for(int i = 0; i < N; i++){
for(auto j : g[i]){
if(j < i && big.fi(i) != big.fi(j)){
g1[i].pb(big.fi(j));
big.me(i, j);
}
}
}
for(int i = N-1; i >= 0; i--){
for(auto j : g[i]){
if(j > i && sml.fi(i) != sml.fi(j)){
g2[i].pb(sml.fi(j));
sml.me(i, j);
}
}
}
int counter = 0;
dfs(N-1, N-1, counter, g1, up1, time1);counter = 0;
dfs(0, 0, counter, g2, up2, time2);
vector<vector<int>> events;
for(int i = 0; i< N; i++){
events.pb({time1[i].X, 0, time2[i].X, -1, -1});
}
for(int i = 0; i< Q; i++){
int tmp1 = jump1(E[i], R[i], up1);
int tmp2 = jump2(S[i], L[i], up2);
events.pb({time1[tmp1].X, -1, time2[tmp2].X, time2[tmp2].Y, i});
events.pb({time1[tmp1].Y, 1, time2[tmp2].X, time2[tmp2].Y, i});
}
//print(events);
sort(events.begin(), events.end());
ft fen(2*N+5);
vector<int> A(Q);
for(auto i : events){
if(i[1] == 0){
fen.upd(i[2], 1);
}else{
int ans = fen.qer(i[3])-fen.qer(i[2]-1);
A[i[4]] += ans*i[1];
}
}
//for(auto i : A)cout << i << ' ';
for (int i = 0; i < Q; ++i){
if(A[i]>0)A[i] = 1;
}
return A;
}
Compilation message
werewolf.cpp: In member function 'void ft::upd(int, int)':
werewolf.cpp:46:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; a < fen.size(); a+=a&(-a))fen[a]+=b;
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
12 ms |
2688 KB |
Output is correct |
11 |
Correct |
11 ms |
2560 KB |
Output is correct |
12 |
Correct |
10 ms |
2560 KB |
Output is correct |
13 |
Correct |
12 ms |
2816 KB |
Output is correct |
14 |
Correct |
12 ms |
2816 KB |
Output is correct |
15 |
Correct |
13 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1407 ms |
148868 KB |
Output is correct |
2 |
Correct |
1744 ms |
153288 KB |
Output is correct |
3 |
Correct |
1390 ms |
148808 KB |
Output is correct |
4 |
Correct |
1338 ms |
147708 KB |
Output is correct |
5 |
Correct |
1341 ms |
147656 KB |
Output is correct |
6 |
Correct |
1435 ms |
147400 KB |
Output is correct |
7 |
Correct |
1517 ms |
147248 KB |
Output is correct |
8 |
Correct |
1735 ms |
152784 KB |
Output is correct |
9 |
Correct |
1322 ms |
148808 KB |
Output is correct |
10 |
Correct |
1256 ms |
147860 KB |
Output is correct |
11 |
Correct |
1140 ms |
147148 KB |
Output is correct |
12 |
Correct |
1278 ms |
147112 KB |
Output is correct |
13 |
Correct |
2063 ms |
158964 KB |
Output is correct |
14 |
Correct |
2167 ms |
158920 KB |
Output is correct |
15 |
Correct |
2040 ms |
158964 KB |
Output is correct |
16 |
Correct |
2058 ms |
158928 KB |
Output is correct |
17 |
Correct |
1446 ms |
146776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
12 ms |
2688 KB |
Output is correct |
11 |
Correct |
11 ms |
2560 KB |
Output is correct |
12 |
Correct |
10 ms |
2560 KB |
Output is correct |
13 |
Correct |
12 ms |
2816 KB |
Output is correct |
14 |
Correct |
12 ms |
2816 KB |
Output is correct |
15 |
Correct |
13 ms |
2688 KB |
Output is correct |
16 |
Correct |
1407 ms |
148868 KB |
Output is correct |
17 |
Correct |
1744 ms |
153288 KB |
Output is correct |
18 |
Correct |
1390 ms |
148808 KB |
Output is correct |
19 |
Correct |
1338 ms |
147708 KB |
Output is correct |
20 |
Correct |
1341 ms |
147656 KB |
Output is correct |
21 |
Correct |
1435 ms |
147400 KB |
Output is correct |
22 |
Correct |
1517 ms |
147248 KB |
Output is correct |
23 |
Correct |
1735 ms |
152784 KB |
Output is correct |
24 |
Correct |
1322 ms |
148808 KB |
Output is correct |
25 |
Correct |
1256 ms |
147860 KB |
Output is correct |
26 |
Correct |
1140 ms |
147148 KB |
Output is correct |
27 |
Correct |
1278 ms |
147112 KB |
Output is correct |
28 |
Correct |
2063 ms |
158964 KB |
Output is correct |
29 |
Correct |
2167 ms |
158920 KB |
Output is correct |
30 |
Correct |
2040 ms |
158964 KB |
Output is correct |
31 |
Correct |
2058 ms |
158928 KB |
Output is correct |
32 |
Correct |
1446 ms |
146776 KB |
Output is correct |
33 |
Correct |
1664 ms |
148100 KB |
Output is correct |
34 |
Correct |
687 ms |
45088 KB |
Output is correct |
35 |
Correct |
1892 ms |
148560 KB |
Output is correct |
36 |
Correct |
1509 ms |
144840 KB |
Output is correct |
37 |
Correct |
1760 ms |
147656 KB |
Output is correct |
38 |
Correct |
1656 ms |
145608 KB |
Output is correct |
39 |
Correct |
1554 ms |
161008 KB |
Output is correct |
40 |
Correct |
1955 ms |
153032 KB |
Output is correct |
41 |
Correct |
1474 ms |
146756 KB |
Output is correct |
42 |
Correct |
1274 ms |
144844 KB |
Output is correct |
43 |
Correct |
1912 ms |
153556 KB |
Output is correct |
44 |
Correct |
1762 ms |
147528 KB |
Output is correct |
45 |
Correct |
1364 ms |
161096 KB |
Output is correct |
46 |
Correct |
1463 ms |
160924 KB |
Output is correct |
47 |
Correct |
1882 ms |
155860 KB |
Output is correct |
48 |
Correct |
1957 ms |
155764 KB |
Output is correct |
49 |
Correct |
1950 ms |
155880 KB |
Output is correct |
50 |
Correct |
2045 ms |
156072 KB |
Output is correct |
51 |
Correct |
1767 ms |
152908 KB |
Output is correct |
52 |
Correct |
1797 ms |
152912 KB |
Output is correct |