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 "game.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 12;
vector<int> g[N],gr[N];
set<int> L[N * 3],R[N * 3];
int k;
int nn;
void build(int v = 1,int tl = 0,int tr = k - 1) {
if(tl > tr) return;
int tm = (tl + tr) >> 1;
for(int i = tl;i <= tr;i++) {
if(i <= tm) {
L[v].insert(i);
}
if(i >= tm) {
R[v].insert(i);
}
}
if(tl == tr) return;
build(v + v,tl,tm - 1);
build(v + v + 1,tm + 1,tr);
}
void init(int n, int K) {
k = K;
nn = n;
for(int i = 0;i <= k - 2;i++) {
g[i].push_back(i + 1);
gr[i + 1].push_back(i);
}
build();
}
bool CYC = false;
vector<int>_r[N];
bool check(int tm,int prev,int v,int val) {
if(prev == -1) return 1;
if(prev < tm && R[v / 2].find(val) == R[v / 2].end()) return false;
if(prev > tm && L[v / 2].find(val) == L[v / 2].end()) return false;
return 1;
}
set<int> bfl[N * 3],bfr[N * 3],nl[N * 3],nr[N * 3];
void add(int x,int y,int &v,int &tl,int &tr,int &prev) {
if(tl > tr) return;
if(CYC) return;
int tm = (tl + tr) >> 1;
function<void(int)> dfs = [&](int u){
R[v].insert(u);
_r[u].push_back(tm);
nr[v].insert(u);
bfr[v].erase(u);
if(L[v].find(u) != L[v].end()) {
CYC = 1;
return;
}
for(int to:g[u]) {
if(R[v].find(to) == R[v].end() && check(tm,prev,v,to)) {
dfs(to);
}
if(CYC) return;
}
if(CYC) return;
};
nr[v].clear();
bool ok = false;
if(R[v].find(x) != R[v].end()){
ok=1;
bfr[v].insert(y);
}
if(prev < tm) {
for(int j:nr[v / 2]) {
if(bfr[v].find(j) != bfr[v].end()) {
dfs(j);
ok = 1;
}
}
}else {
for(int j:nl[v / 2]) {
if(bfr[v].find(j) != bfr[v].end()) {
dfs(j);
ok = 1;
}
}
}
if(ok) {
v = v + v + 1;
tl = tm + 1;
prev = tm;
} else {
v += v;
tr = tm - 1;
prev = tm;
}
}
void addl(int x,int y,int &v,int &tl,int &tr,int &prev) {
if(tl > tr) return;
if(CYC) return;
int tm = (tl + tr) >> 1;
function<void(int)> dfs1 = [&](int u){
if(R[v].find(u) != R[v].end()) {
CYC = 1;
return;
}
L[v].insert(u);
nl[v].insert(u);
for(int to:gr[u]) {
if(L[v].find(to) == L[v].end() && (check(tm,prev,v,to))) {
dfs1(to);
}
if(CYC) return;
}
if(CYC) return;
};
nl[v].clear();
bool ok = false;
if(L[v].find(y) != L[v].end()) {
ok = 1;
bfl[v].insert(x);
}
if(prev < tm) {
for(int j:nr[v / 2]) {
if(bfl[v].find(j) != bfl[v].end()) {
dfs1(j);
ok = 1;
}
}
}else {
for(int j:nl[v / 2]) {
if(bfl[v].find(j) != bfl[v].end()) {
dfs1(j);
ok = 1;
}
}
}
if(ok) {
v += v;
tr = tm - 1;
prev = tm;
} else {
v = v + v + 1;
tl = tm + 1;
prev = tm;
}
}
void _add(int u,int v) {
int v1 = 1,tl1 = 0,tr1 = k - 1,prev1 = -1;
int v2 = 1,tl2 = 0,tr2 = k - 1,prev2 = -1;
nr[0].clear();
nr[0].insert(u);
nr[0].insert(v);
while(tl1 <= tr1 || tl2 <= tr2) {
addl(u,v,v1,tl1,tr1,prev1);
add(u,v,v2,tl2,tr2,prev2);
if(CYC) break;
}
}
int quer = 1;
int add_teleporter(int u, int v) {
quer++;
if(max(u,v) <= k - 1) {
if(u >= v) {
CYC = 1;
return 1;
}
return 0;
}
if(CYC) return 1;
g[u].push_back(v);
gr[v].push_back(u);
_add(u,v);
return CYC;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |