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 "doll.h"
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;
#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)
int s=0, n;
vector<vi> nxt;
vector<set<int> > nxts;
vector<int> C, X, Y;
int b;
int inv(int x){
vi a;
//cout<<x<<" ";
FOR(i,b) a.push_back(x%2), x/=2;
ll ans=0;
FOA(v,a) ans*=2, ans+=v;
//cout<<ans<<endl;
return ans;
}
ii cnt(int x, int l, int r){
set<int> all;
if(nxts[x].size()==1) return {1, *nxts[x].begin()};
FORi(i,l,r+1){
if(inv(i)<(int)nxt[x].size()-1) all.insert(nxt[x][inv(l)]);
else if(i==(1<<b)-1) all.insert(nxt[x].back());
else all.insert(C[x]);
if(all.size()>1) break;
}
if(all.size()>1) return {2,0};
else return {1, *all.begin()};
}
void rec(int x, int l, int r){
//cout<<x<<": "<<l<<" "<<r<<endl;
if(l==r) return;
if(r==l+1){
if(cnt(x,l,r).F==1) return;
if(inv(l)<(int)nxt[x].size()-1) X.push_back(nxt[x][inv(l)]);
else if(l==(1<<b)-1) X.push_back(nxt[x].back());
else X.push_back(C[x]);
l++;
if(inv(l)<(int)nxt[x].size()-1) Y.push_back(nxt[x][inv(l)]);
else if(l==(1<<b)-1) Y.push_back(nxt[x].back());
else Y.push_back(C[x]);
}
else{
ii p = cnt(x,l,MID);
if(p.F==1) X.push_back(p.S);
else X.push_back(-(++s));
int q = Y.size();
Y.push_back(s);
rec(x, l, MID);
p = cnt(x,MID+1,r);
if(p.F==1) Y[q] = (p.S);
else Y[q] = -(++s);
rec(x, MID+1,r);
}
}
void create_circuit(int M, std::vector<int> A) {
n = A.size();
nxt.assign(M+1, vi());
nxts.assign(M+1, set<int>());
C.assign(M+1, 0);
nxt[0].push_back(A[0]);
nxts[0].insert(A[0]);
FORi(i,1,n) nxt[A[i-1]].push_back(A[i]);
FORi(i,1,n) nxts[A[i-1]].insert(A[i]);
nxt[A[n-1]].push_back(0);
nxts[A[n-1]].insert(0);
FOR(x,M+1){
int q = nxt[x].size();
if(q==0) continue;
else if(nxts[x].size()==1) C[x] = nxt[x][0];
else{
int t = q-1;
int l=0;
while(t) l++, t/=2;
b = l;
//cout<<q<<": "<<b<<endl;
C[x] = -(++s);
rec(x, 0, (1<<l)-1);
}
}
#ifdef LOCAL
FOA(v,C) cout<<v<<" ";
cout<<endl;
FOA(v,X) cout<<v<<" ";
cout<<endl;
FOA(v,Y) cout<<v<<" ";
cout<<endl;
#endif
answer(C, X, Y);
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |