#include "werewolf.h"
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> p2;
const int N=2e5+5;
const int M=4e5+5;
const int K=(1<<19)+5;
int n,m,q;
vector<int> adj[N];
int s[N],e[N],l[N],r[N];
vector<int> ql[N],qr[N];
int pl[N],pr[N];
p2 ranl[N],ranr[N];
int posx[N],posy[N];
vector<int> num[N];
struct dsu_t{
int p[M];
int in[M],out[M];
vector<p2> node;
void init(){
for(int i=0;i<2*n;i++){
p[i]=i;
}
node=vector<p2>(n,p2(-1,-1));
}
int fp(int u){
if(u==p[u])return u;
return p[u]=fp(p[u]);
}
void uni(int u,int v){
u=fp(u),v=fp(v);
if(u==v)return;
int np=node.size();
p[u]=p[v]=np;
node.emplace_back(u,v);
}
void dfs(int u,int &t){
if(u<n){
in[u]=out[u]=++t;
return;
}
auto [l,r]=node[u];
dfs(l,t);
dfs(r,t);
in[u]=min(in[l],in[r]);
out[u]=max(out[l],out[r]);
}
void dfs(){
int t=0;
dfs(node.size()-1,t);
}
}dsu;
struct segtree{
vector<int> t[K];
void build(int l,int r,int i){
if(l==r)return void(t[i]=num[l]);
int m=(l+r)/2;
build(l,m,i*2);
build(m+1,r,i*2+1);
for(auto x:t[i*2])t[i].emplace_back(x);
for(auto x:t[i*2+1])t[i].emplace_back(x);
}
void build(){
build(1,n,1);
}
int query(int l,int r,int i,int x,int y,int a,int b){
if(y<l||r<x)return 0;
if(x<=l&&r<=y)return upper_bound(t[i].begin(),t[i].end(),b)-lower_bound(t[i].begin(),t[i].end(),a);
int m=(l+r)/2;
return query(l,m,i*2,x,y,a,b)+query(m+1,r,i*2+1,x,y,a,b);
}
int query(int x,int y,int a,int b){
return query(1,n,1,x,y,a,b);
}
}seg;
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++){
int u=X[i],v=Y[i];
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
for(int i=0;i<q;i++){
s[i]=S[i];
e[i]=E[i];
l[i]=L[i];
r[i]=R[i];
ql[l[i]].emplace_back(i);
qr[r[i]].emplace_back(i);
}
dsu.init();
for(int u=n-1;u>=0;u--){
for(auto v:adj[u]){
if(v<u)continue;
dsu.uni(u,v);
}
for(auto i:ql[u]){
pl[i]=dsu.fp(s[i]);
}
}
dsu.dfs();
for(int i=0;i<n;i++)posx[i]=dsu.in[i];
for(int i=0;i<q;i++){
int p=pl[i];
ranl[i]=p2(dsu.in[p],dsu.out[p]);
}
dsu.init();
for(int u=0;u<n;u++){
for(auto v:adj[u]){
if(v>u)continue;
dsu.uni(u,v);
}
for(auto i:qr[u]){
pr[i]=dsu.fp(e[i]);
}
}
dsu.dfs();
for(int i=0;i<n;i++)posy[i]=dsu.in[i];
for(int i=0;i<q;i++){
int p=pr[i];
ranr[i]=p2(dsu.in[p],dsu.out[p]);
}
for(int i=0;i<n;i++)num[posx[i]].emplace_back(posy[i]);
for(int i=1;i<=n;i++)sort(num[i].begin(),num[i].end());
seg.build();
vector<int> ans(q);
for(int i=0;i<q;i++){
auto [x,y]=ranl[i];
auto [a,b]=ranr[i];
ans[i]=seg.query(x,y,a,b)>0;
}
return ans;
}
Compilation message
werewolf.cpp: In member function 'void dsu_t::dfs(int, int&)':
werewolf.cpp:47:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
47 | auto [l,r]=node[u];
| ^
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:137:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
137 | auto [x,y]=ranl[i];
| ^
werewolf.cpp:138:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
138 | auto [a,b]=ranr[i];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
31444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
31444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
462 ms |
106060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
31444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |