Submission #109411

#TimeUsernameProblemLanguageResultExecution timeMemory
109411nvmdavaMechanical Doll (IOI18_doll)C++17
85.55 / 100
169 ms16768 KiB
//#include "doll.h"
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
vector<int> aft[200005], C, X, Y, st;
int sz = 0;
int t;
void create_circuit(int M, std::vector<int> A);

void answer(std::vector<int> C, std::vector<int> X, std::vector<int> Y);
void go(int id, int root, int v, int cnt, int dep){
	dep--;
	if(dep == 0){
		if(cnt == 1){
			Y[-1 - v] = 1;
			X[-1 - v] = root;
//			cerr<<v<<' '<<X[-1 - v]<<' '<<Y[-1 - v]<<'\n';
		} else if(cnt == 2){
			X[-1 - v] = 1;
			Y[-1 - v] = 1;
//			cerr<<v<<' '<<X[-1 - v]<<' '<<Y[-1 - v]<<'\n';
		}

		return;
	}
    Y[-1 - v] = --sz;
	X[-1 - v] = cnt - (1 << dep) > 0 ? --sz : root;
	X.pb(0);
	Y.pb(0);
	st.pb(0);
	if(X[-1 - v] != root){
        st.pb(0);
		X.pb(0);
		Y.pb(0);
	}
	go(id, root, Y[-1 - v], min(1 << dep, cnt), dep);
	if(X[-1 - v] != root){
		go(id, root, X[-1 - v], cnt - (1 << dep), dep);
	}
}

void trav(int root, int id, int idd){
//    cerr<<id<<' '<<X[-1-id]<<' '<<Y[-1-id]<<'\n';
	if(st[-1 - id] == 0){
		if(X[-1 - id] == 1){
			st[-1 - id] = 1;
			X[-1 - id] = aft[idd][t++];
			if(t == aft[idd].size()) return;
			trav(root, root, idd);
		} else {
			st[-1 - id] = 1;
			trav(root, X[-1 - id], idd);
		}
	} else {
		if(Y[-1 - id] == 1){
			st[-1 - id] = 0;
			Y[-1 - id] = aft[idd][t++];
			if(t == aft[idd].size()) return;
			trav(root, root, idd);
		} else {
			st[-1 - id] = 0;
			trav(root, Y[-1 - id], idd);
		}
	}
}

void solve(int id){
	if(!aft[id].size())
		C[id] = 0;
	else if(aft[id].size() == 1)
		C[id] = aft[id][0];
	else {
		t = 0;
		C[id] = --sz;
		X.pb(0);
		st.pb(0);
		Y.pb(0);
		int dep = ceil(log2(aft[id].size()));
//		cerr<<"GO BEGIN\n";
		go(id, C[id], C[id], aft[id].size(), dep);
//		cerr<<"GO DONE\n";
//		cerr<<"TRAV BEGIN\n";
		trav(C[id], C[id], id);
//		cerr<<"TRAV DONE\n";
	}
}

void create_circuit(int M, vector<int> A) {
	C.resize(M + 1);
	aft[0].pb(A[0]);
	for(int i = 1; i < A.size(); i++)
		aft[A[i - 1]].pb(A[i]);
	aft[A.back()].pb(0);
	for(int i = 0; i <= M; i++)
		solve(i);
	answer(C, X, Y);
}

Compilation message (stderr)

doll.cpp: In function 'void trav(int, int, int)':
doll.cpp:48:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |    if(t == aft[idd].size()) return;
      |       ~~^~~~~~~~~~~~~~~~~~
doll.cpp:58:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |    if(t == aft[idd].size()) return;
      |       ~~^~~~~~~~~~~~~~~~~~
doll.cpp: In function 'void create_circuit(int, std::vector<int>)':
doll.cpp:91:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |  for(int i = 1; i < A.size(); i++)
      |                 ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...