#include "werewolf.h"
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt,sse4")
using namespace std;
#define pii pair<int,int>
#define fs first
#define sc second
const int mxn = 2e5+10;
vector<int> ans;
struct SEG{
#define ls now*2+1
#define rs now*2+2
#define mid ((l+r)>>1)
int seg[mxn*4];
SEG(){
memset(seg,-1,sizeof(seg));
}
void modify(int now,int l,int r,int p,int v){
if(l==r){
seg[now] = v;
return;
}
if(mid>=p)modify(ls,l,mid,p,v);
else modify(rs,mid+1,r,p,v);
seg[now] = max(seg[ls],seg[rs]);
}
int getval(int now,int l,int r,int s,int e){
if(l>=s&&e>=r)return seg[now];
if(mid>=e)return getval(ls,l,mid,s,e);
else if(mid<s)return getval(rs,mid+1,r,s,e);
return max(getval(ls,l,mid,s,e),getval(rs,mid+1,r,s,e));
}
#undef ls
#undef rs
#undef mid
};
struct DSU{
int mx[mxn],dsu[mxn],sz[mxn],mn[mxn];
DSU(){}
void init(int n){
for(int i = 0;i<=n;i++){
dsu[i] = mx[i] = mn[i] = i;
sz[i] = 1;
}
return;
}
int root(int k){
return k == dsu[k]?k:dsu[k] = root(dsu[k]);
}
void onion(int a,int b){
a = root(a),b = root(b);
if(a == b)return;
if(sz[a]<sz[b])swap(a,b);
mx[a] = max(mx[a],mx[b]);
mn[a] = min(mn[a],mn[b]);
dsu[b] = a;
sz[a] += sz[b];
return;
}
};
struct Tree{
vector<int> tree[mxn];
int par[mxn][20];
pii eul[mxn];
int ptr;
Tree(){ptr = -1;}
void add(int a,int b){
tree[a].push_back(b);
}
void dfs(int now,int fa = 0){
eul[now].fs = ++ptr;
par[now][0] = fa;
for(int i = 1;i<20;i++)par[now][i] = par[par[now][i-1]][i-1];
for(auto nxt:tree[now]){
if(nxt == par[now][0])continue;
dfs(nxt,now);
}
eul[now].sc = ptr;
//cerr<<now<<":"<<eul[now].fs<<','<<eul[now].sc<<endl;
return;
}
};
struct D{//compare by x and y,data is a,b
int x,y,a,b,c;
D(int xx = 0,int yy = 0,int aa =0 ,int bb = 0,int cc = 0):x(xx),y(yy),a(aa),b(bb),c(cc){}
bool operator<(D b)const{
return x == b.x?y<b.y:x<b.x;
}
};
vector<int> g[mxn];
D req[mxn];//s,e,l,r,id
int N,Q;
Tree mntr,mxtr;
namespace TREE{
DSU dsu;
void MAKE_MX(){
dsu.init(N);
for(int i = 0;i<N;i++){
int now = i;
for(auto nxt:g[now]){
if(nxt>now||dsu.root(nxt) == dsu.root(now))continue;
int tmp = dsu.mx[dsu.root(nxt)];
mxtr.add(now,tmp);
dsu.onion(now,nxt);
}
}
mxtr.dfs(N-1,N-1);
}
void MAKE_MN(){
dsu.init(N);
for(int i = N-1;i>=0;i--){
int now = i;
for(auto nxt:g[now]){
if(nxt<now||dsu.root(nxt) == dsu.root(now))continue;
int tmp = dsu.mn[dsu.root(nxt)];
mntr.add(now,tmp);
dsu.onion(now,nxt);
}
}
mntr.dfs(0,0);
return;
}
}
namespace SWEEP{
vector<D> op;
SEG seg;
D trans(D now){
int s = now.x,e = now.y,l = now.a,r = now.b,id = now.c;
swap(s,e);
if(s>r||e<l){
ans[id] = 0;
return D(-1);
}
for(int i = 19;i>=0;i--){
if(mxtr.par[s][i] <= r)s = mxtr.par[s][i];
if(mntr.par[e][i] >= l)e = mntr.par[e][i];
}
D re;
re.x = mxtr.eul[s].sc,re.y = id;
re.a = mntr.eul[e].fs,re.b = mntr.eul[e].sc;
re.c = mxtr.eul[s].fs;
return re;
}
void INIT(){
for(int i = 0;i<N;i++){
op.push_back(D(mxtr.eul[i].fs,-1,mntr.eul[i].fs));
}
for(int i = 0;i<Q;i++){
auto tmp = trans(req[i]);
if(tmp.x != -1)op.push_back(trans(req[i]));
//cerr<<tmp.x<<' '<<tmp.y<<' '<<tmp.a<<' '<<tmp.b<<' '<<tmp.c<<endl;
}
return;
}
void GO(){
sort(op.begin(),op.end());
for(auto now:op){
//cerr<<now.x<<','<<now.y<<' '<<now.a<<' '<<now.b<<' '<<now.c<<endl;
if(now.y == -1){
seg.modify(0,0,N-1,now.a,now.x);
}
else{
int id = now.y;
int s = now.a,e = now.b,up = now.c;
if(seg.getval(0,0,N-1,s,e)>=up)ans[id] = 1;
else ans[id] = 0;
}
}
}
}
std::vector<int> check_validity(int NN, std::vector<int> X, std::vector<int> Y,
std::vector<int> S, std::vector<int> E,
std::vector<int> L, std::vector<int> R) {
for(int i = 0;i<X.size();i++){
int a = X[i],b = Y[i];
g[a].push_back(b);
g[b].push_back(a);
}
N = NN;
Q = S.size();
ans.resize(Q);
for(int i = 0;i<Q;i++){
req[i].x = S[i],req[i].y = E[i],req[i].a = L[i],req[i].b = R[i],req[i].c = i;
}
TREE::MAKE_MX();
cerr<<"MX DONE!"<<endl;
TREE::MAKE_MN();
cerr<<"MN DONE!"<<endl;
SWEEP::INIT();
cerr<<"SWEEP INIT!"<<endl;
SWEEP::GO();
cerr<<"SWEEP DONE!"<<endl;
return ans;
}
Compilation message
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:185:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
185 | for(int i = 0;i<X.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
32344 KB |
Output is correct |
2 |
Correct |
7 ms |
32348 KB |
Output is correct |
3 |
Correct |
7 ms |
32348 KB |
Output is correct |
4 |
Correct |
7 ms |
32488 KB |
Output is correct |
5 |
Correct |
7 ms |
32348 KB |
Output is correct |
6 |
Correct |
6 ms |
32288 KB |
Output is correct |
7 |
Correct |
9 ms |
32348 KB |
Output is correct |
8 |
Correct |
7 ms |
32348 KB |
Output is correct |
9 |
Correct |
8 ms |
32348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
32344 KB |
Output is correct |
2 |
Correct |
7 ms |
32348 KB |
Output is correct |
3 |
Correct |
7 ms |
32348 KB |
Output is correct |
4 |
Correct |
7 ms |
32488 KB |
Output is correct |
5 |
Correct |
7 ms |
32348 KB |
Output is correct |
6 |
Correct |
6 ms |
32288 KB |
Output is correct |
7 |
Correct |
9 ms |
32348 KB |
Output is correct |
8 |
Correct |
7 ms |
32348 KB |
Output is correct |
9 |
Correct |
8 ms |
32348 KB |
Output is correct |
10 |
Correct |
13 ms |
35160 KB |
Output is correct |
11 |
Correct |
11 ms |
35412 KB |
Output is correct |
12 |
Correct |
13 ms |
35164 KB |
Output is correct |
13 |
Correct |
12 ms |
35388 KB |
Output is correct |
14 |
Correct |
12 ms |
35384 KB |
Output is correct |
15 |
Correct |
11 ms |
35388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
430 ms |
102584 KB |
Output is correct |
2 |
Correct |
481 ms |
105740 KB |
Output is correct |
3 |
Correct |
416 ms |
105148 KB |
Output is correct |
4 |
Correct |
399 ms |
104636 KB |
Output is correct |
5 |
Correct |
405 ms |
103172 KB |
Output is correct |
6 |
Correct |
428 ms |
102924 KB |
Output is correct |
7 |
Correct |
440 ms |
103356 KB |
Output is correct |
8 |
Correct |
428 ms |
106424 KB |
Output is correct |
9 |
Correct |
337 ms |
104384 KB |
Output is correct |
10 |
Correct |
327 ms |
104716 KB |
Output is correct |
11 |
Correct |
339 ms |
102672 KB |
Output is correct |
12 |
Correct |
340 ms |
102932 KB |
Output is correct |
13 |
Correct |
532 ms |
112072 KB |
Output is correct |
14 |
Correct |
512 ms |
111596 KB |
Output is correct |
15 |
Correct |
512 ms |
112044 KB |
Output is correct |
16 |
Correct |
530 ms |
112272 KB |
Output is correct |
17 |
Correct |
403 ms |
102932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
32344 KB |
Output is correct |
2 |
Correct |
7 ms |
32348 KB |
Output is correct |
3 |
Correct |
7 ms |
32348 KB |
Output is correct |
4 |
Correct |
7 ms |
32488 KB |
Output is correct |
5 |
Correct |
7 ms |
32348 KB |
Output is correct |
6 |
Correct |
6 ms |
32288 KB |
Output is correct |
7 |
Correct |
9 ms |
32348 KB |
Output is correct |
8 |
Correct |
7 ms |
32348 KB |
Output is correct |
9 |
Correct |
8 ms |
32348 KB |
Output is correct |
10 |
Correct |
13 ms |
35160 KB |
Output is correct |
11 |
Correct |
11 ms |
35412 KB |
Output is correct |
12 |
Correct |
13 ms |
35164 KB |
Output is correct |
13 |
Correct |
12 ms |
35388 KB |
Output is correct |
14 |
Correct |
12 ms |
35384 KB |
Output is correct |
15 |
Correct |
11 ms |
35388 KB |
Output is correct |
16 |
Correct |
430 ms |
102584 KB |
Output is correct |
17 |
Correct |
481 ms |
105740 KB |
Output is correct |
18 |
Correct |
416 ms |
105148 KB |
Output is correct |
19 |
Correct |
399 ms |
104636 KB |
Output is correct |
20 |
Correct |
405 ms |
103172 KB |
Output is correct |
21 |
Correct |
428 ms |
102924 KB |
Output is correct |
22 |
Correct |
440 ms |
103356 KB |
Output is correct |
23 |
Correct |
428 ms |
106424 KB |
Output is correct |
24 |
Correct |
337 ms |
104384 KB |
Output is correct |
25 |
Correct |
327 ms |
104716 KB |
Output is correct |
26 |
Correct |
339 ms |
102672 KB |
Output is correct |
27 |
Correct |
340 ms |
102932 KB |
Output is correct |
28 |
Correct |
532 ms |
112072 KB |
Output is correct |
29 |
Correct |
512 ms |
111596 KB |
Output is correct |
30 |
Correct |
512 ms |
112044 KB |
Output is correct |
31 |
Correct |
530 ms |
112272 KB |
Output is correct |
32 |
Correct |
403 ms |
102932 KB |
Output is correct |
33 |
Correct |
489 ms |
104644 KB |
Output is correct |
34 |
Correct |
199 ms |
72640 KB |
Output is correct |
35 |
Correct |
478 ms |
106404 KB |
Output is correct |
36 |
Correct |
452 ms |
103468 KB |
Output is correct |
37 |
Correct |
462 ms |
105756 KB |
Output is correct |
38 |
Correct |
492 ms |
105816 KB |
Output is correct |
39 |
Correct |
450 ms |
113296 KB |
Output is correct |
40 |
Correct |
598 ms |
113344 KB |
Output is correct |
41 |
Correct |
398 ms |
105924 KB |
Output is correct |
42 |
Correct |
359 ms |
105152 KB |
Output is correct |
43 |
Correct |
478 ms |
111248 KB |
Output is correct |
44 |
Correct |
426 ms |
104892 KB |
Output is correct |
45 |
Correct |
409 ms |
114028 KB |
Output is correct |
46 |
Correct |
428 ms |
113680 KB |
Output is correct |
47 |
Correct |
468 ms |
111080 KB |
Output is correct |
48 |
Correct |
530 ms |
112800 KB |
Output is correct |
49 |
Correct |
508 ms |
112060 KB |
Output is correct |
50 |
Correct |
484 ms |
112316 KB |
Output is correct |
51 |
Correct |
475 ms |
113340 KB |
Output is correct |
52 |
Correct |
467 ms |
113084 KB |
Output is correct |