Submission #641524

#TimeUsernameProblemLanguageResultExecution timeMemory
641524ymmCrayfish scrivener (IOI12_scrivener)C++17
100 / 100
880 ms171124 KiB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int lg = 20;

struct node {
	int par_com[lg];
	int par_let[lg];
	int len;
	char let;
};
int cur;

node pool[250'000'000/sizeof(node)];
int next_node = 0;

int new_node()
{
	return next_node++;
}
	

void Init()
{
	cur = new_node();
	Loop (i,0,lg) {
		pool[cur].par_com[i] = cur;
		pool[cur].par_let[i] = cur;
	}
	pool[cur].len = 0;
	pool[cur].let = 0;
}

void calc_par_com(int t)
{
	if (pool[t].par_com[1] != -1)
		return;
	calc_par_com(pool[t].par_com[0]);
	Loop (i,0,lg-1) {
		pool[t].par_com[i+1] = pool[pool[t].par_com[i]].par_com[i];
	}
}

static void calc_par_let(int t)
{
	if (pool[t].par_let[1] != -1)
		return;
	calc_par_let(pool[t].par_let[0]);
	Loop (i,0,lg-1) {
		pool[t].par_let[i+1] = pool[pool[t].par_let[i]].par_let[i];
	}
}

void TypeLetter(char L)
{
	int next = new_node();
	pool[next].par_com[0] = cur;
	pool[next].par_let[0] = cur;
	pool[next].par_com[1] = -1;
	pool[next].par_let[1] = -1;
	pool[next].len = pool[pool[next].par_let[0]].len + 1;
	pool[next].let = L;
	cur = next;
}

void UndoCommands(int U)
{
	int sib = cur;
	calc_par_com(cur);
	Loop (i,0,lg) {
		if (U & (1<<i))
			sib = pool[sib].par_com[i];
	}
	int next = new_node();
	pool[next].par_com[0] = cur;
	pool[next].par_let[0] = pool[sib].par_let[0];
	pool[next].par_com[1] = -1;
	pool[next].par_let[1] = -1;
	pool[next].len = pool[pool[next].par_let[0]].len + 1;
	pool[next].let = pool[sib].let;
	cur = next;
}

char GetLetter(int P)
{
	P = pool[cur].len - 1 - P;
	int ans = cur;
	calc_par_let(cur);
	Loop (i,0,lg) {
		if (P & (1<<i))
			ans = pool[ans].par_let[i];
	}
	return pool[ans].let;
}
#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...