# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
205333 | medk | Werewolf (IOI18_werewolf) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "werewolf.h"
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define x first
#define y second
using namespace std;
vector<vector<int>> g(200001);
vector<bool> vis(200001);
vector<int> ln,pos(200001);
void dfs1(int u, int l)
{
st.insert(u);
for(int v:g[u])
{
if(v<l || st.count(v)) continue;
dfs1(v,l);
}
}
int dfs2(int u, int r)
{
int ret=st.count(u);
vis[u]=1;
for(int v:g[u])
{
if(v>r || vis[v]) continue;
ret|=dfs2(v,r);
}
return ret;
}
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++) g[x[i]].pb(y[i]), g[y[i]].pb(x[i]);
if(n<=3000 && m<=6000 && q<=3000)
{
vector<int> ans;
for(int i=0;i<q;i++)
{
st.clear();
dfs1(s[i],l[i]);
for(int j=0;j<n;j++) vis[j]=0;
ans.pb(dfs2(e[i],r[i]));
}
return ans;
}
int leaf;
for(int i=0;i<n;i++) if((int)g[i].size()==1) {leaf=i; break;}
while((int)ln.size()<n)
{
pos[leaf]=(int)ln.size();
ln.pb(leaf);
vis[leaf]=1;
for(int v:g[leaf])
{
if(vis[v]) continue;
leaf=v;
}
}
vector<pair<int,int>> L,R;
for(int i=0;i<q;i++) L.pb({-l[i],i}), R.pb({r[i],i});
sort(L.begin(),L.end());
sort(R.begin(),R.end());
vector<pair<int,int>> lftmost(q);
int ptr=n-1;
set<pair<int,int>> st;
set<pair<int,int>>::iterator it;
for(int i=0;i<q;i++)
{
while(ptr>=-L[i].x)
{
int ps=pos[ptr];
it=st.upper_bound({ps,ps});
pair<int,int> p={ps,ps};
if(it!=st.end())
if((*it).x==ps+1) p.y=(*it).y, st.erase(it);
it=st.upper_bound({ps,ps});
if((int)st.size()) if(it!=st.begin())
if((*prev(it)).y==ps-1) p.x=(*prev(it)).x, st.erase(prev(it));
st.insert(p);
ptr--;
}
it=prev(st.upper_bound({pos[s[L[i].y]]+1,-1}));
lftmost[L[i].y]=*it;
}
ptr=0;
st.clear();
vector<int> ans(q);
for(int i=0;i<q;i++)
{
while(ptr<=R[i].x)
{
int ps=pos[ptr];
it=st.upper_bound({ps,ps});
pair<int,int> p={ps,ps};
if(it!=st.end())
if((*it).x==ps+1) p.y=(*it).y, st.erase(it);
it=st.upper_bound({ps,ps});
if((int)st.size()) if(it!=st.begin())
if((*prev(it)).y==ps-1) p.x=(*prev(it)).x, st.erase(prev(it));
st.insert(p);
ptr++;
}
it=prev(st.upper_bound({pos[e[R[i].y]]+1,-1}));
ans[R[i].y]=!((*it).y<lftmost[R[i].y].x || (*it).x>lftmost[R[i].y].y);
}
return ans;
}