# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
599029 |
2022-07-19T09:19:16 Z |
fuad27 |
Werewolf (IOI18_werewolf) |
C++17 |
|
3509 ms |
36808 KB |
#include "werewolf.h"
#include<bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> e;
DSU(int N) { e = vector<int>(N, -1); }
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y]; e[y] = x; return true;
}
};
const int NN = 200'010;
vector<int> topo;
vector<int> g[NN];
vector<bool> vis(NN);
int n;
void dfs(int at) {
vis[at]=1;
for(int to:g[at]) {
if(vis[to])continue;
dfs(to);
}
topo.push_back(at);
}
int Tmx[4*NN],Tmn[4*NN];
void build(int v, int tl, int tr, vector<int> &a) {
if(tl == tr) {
Tmx[v]=a[tl];
Tmn[v]=a[tl];
return;
}
int tm=(tl+tr)/2;
build(2*v,tl,tm,a);
build(2*v+1,tm+1,tr,a);
Tmx[v]=max(Tmx[2*v],Tmx[2*v+1]);
Tmn[v]=min(Tmn[2*v],Tmn[2*v+1]);
}
int querymn(int l, int r, int v, int tl, int tr) {
if(l > r)return 1e9;
if(l == tl and r == tr)return Tmn[v];
int tm=(tl+tr)/2;
return min(querymn(l,min(r,tm),2*v,tl,tm),
querymn(max(l,tm+1),r,2*v+1,tm+1,tr));
}
int querymx(int l, int r, int v, int tl, int tr) {
if(l > r)return -1e9;
if(l == tl and r == tr)return Tmx[v];
int tm=(tl+tr)/2;
return max(querymx(l,min(r,tm),2*v,tl,tm),
querymx(max(l,tm+1),r,2*v+1,tm+1,tr));
}
std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y,
std::vector<int> S, std::vector<int> E, std::vector<int> L, std::vector<int> R) {
n=N;
int Q = S.size();
// if(N <= 3000 and Q <= 3000 and Y.size() <= 6000) {
// std::vector<int> A(Q);
// for(int j = 0; j < Q; ++j) {
// DSU l(N+1), r(N+1);
// for(int i = 0;i<(int)X.size();i++) {
// if(X[i] <= R[j] and Y[i]<=R[j])l.unite(X[i], Y[i]);
// if(X[i]>=L[j] and Y[i]>=L[j])r.unite(X[i], Y[i]);
// }
// for(int i = L[j];i<=R[j];i++) {
// if(l.same_set(E[j], i) and r.same_set(S[j],i))A[j]=1;
// }
// }
// return A;
// }
{
for(int i = 0;i<N-1;i++) {
g[Y[i]].push_back(X[i]);
g[X[i]].push_back(Y[i]);
}
for(int i = 0;i<N;i++) {
if(g[i].size() == 1){
dfs(i);
break;
}
}
reverse(topo.begin(), topo.end());
build(1, 0, n-1, topo);
int in[N];
for(int i = 0;i<N;i++) {
in[topo[i]]=i;
}
vector<int> ans(E.size());
for(int i = 0;i<E.size();i++) {
if(S[i]<L[i] or E[i]>R[i]){
continue;
}
int l1,l2,r1,r2;
l1=l2=in[S[i]];
r1=r2=in[E[i]];
int lo =in[S[i]], hi=n-1;
while(lo <= hi) {
int mid = (lo+hi)/2;
if(querymn(in[S[i]], mid, 1, 0, n-1)<L[i]){
hi=mid-1;
}
else {
l2=mid;
lo=mid+1;
}
}
lo=0,hi=in[S[i]];
while(lo <= hi) {
int mid = (lo+hi)/2;
if(querymn(mid,in[S[i]], 1, 0, n-1)<L[i]){
lo=mid+1;
}
else {
l1=mid;
hi=mid-1;
}
}
lo=in[E[i]], hi=n-1;
while(lo <= hi) {
int mid = (lo+hi)/2;
if(querymx(in[E[i]], mid, 1, 0, n-1)>R[i])hi=mid-1;
else {
r2=mid;
lo=mid+1;
}
}
lo=0,hi=in[E[i]];
while(lo <= hi) {
int mid = (lo+hi)/2;
if(querymx(mid, in[E[i]], 1, 0, n-1)>R[i])lo=mid+1;
else {
r2=mid;
hi=mid-1;
}
}
if(l2>r1 and l1>r2) {
ans[i]=1;
}
}
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:93:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int i = 0;i<E.size();i++) {
| ~^~~~~~~~~
werewolf.cpp:60:7: warning: unused variable 'Q' [-Wunused-variable]
60 | int Q = S.size();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3509 ms |
36808 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |