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"
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define pb push_back
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define F first
#define S second
#define ET cout << "\n"
#define MP make_pair
#define MEM(i,j) memset(i,j,sizeof i)
#define ALL(v) v.begin(),v.end()
#define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
vector<int> C,X,Y,pl[100005];
bool state[400005];
const int INF=1e9;
int newnode()
{
X.pb(INF),Y.pb(INF);
return -(int)(X.size());
}
int node(int x)
{
if(x==2) return newnode();
int tmp=newnode();
int a=node(x/2),b=node(x/2);
X[-tmp-1]=a,Y[-tmp-1]=b;
return tmp;
}
void walk(vector<int> &v,int st)
{
//DB(X,0,X.size())
//DB(Y,0,Y.size())
//cout << "walk " << v.size() << " " << st << "\n";
int k=0;
int pos=st;
while(k<v.size())
{
//cout << pos << " ";
state[-pos] = !state[-pos];
if(state[-pos])
if(X[-(1+pos)]!=INF) pos=X[-(1+pos)];
else X[-(1+pos)]=v[k++],pos=st;
else
if(Y[-(1+pos)]!=INF) pos=Y[-(1+pos)];
else Y[-(1+pos)]=v[k++],pos=st;
}
//ET;
//cout << "walk " << v.size() << " end\n";
}
int r_node(vector<int> &v)
{
int tmp,sz=v.size();
if(!(sz&(sz-1))) tmp=node(sz);
else
{
int nw=tmp=newnode(),lb=sz&-sz,a;
for(int i=1<<__lg(sz);i>lb;i>>=1)
{
//cout << nw << " ";
if(v.size()&i)
a=node(i),X[-nw-1]=a;
else
X[-nw-1]=tmp;
a=newnode(),Y[-nw-1]=a,nw=Y[-nw-1];
}
X[-nw-1]=tmp;
if(lb>1) a=node(lb),Y[-nw-1]=a;
}
//cout << "r_node end\n";
walk(v,tmp);
return tmp;
}
void create_circuit(int M, std::vector<int> A) {
C.resize(M+1),A.pb(0);
for(int i=0;i<A.size();++i)
pl[A[i]].pb(i);
C[0]=A[0];
for(int i=1;i<=M;++i)
if(pl[i].size()==1)
C[i]=A[pl[i][0]+1];
else if(pl[i].size()>1)
{
for(int &j:pl[i])
j=A[j+1];
C[i]=r_node(pl[i]);
}
answer(C,X,Y);
}
Compilation message (stderr)
doll.cpp: In function 'void walk(std::vector<int>&, int)':
doll.cpp:44:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | while(k<v.size())
| ~^~~~~~~~~
doll.cpp: In function 'void create_circuit(int, std::vector<int>)':
doll.cpp:85:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i=0;i<A.size();++i)
| ~^~~~~~~~~| # | 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... |