Submission #641521

#TimeUsernameProblemLanguageResultExecution timeMemory
641521ymmCrayfish scrivener (IOI12_scrivener)C++17
34 / 100
1020 ms156048 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 TypeLetter(char L)
{
	int next = new_node();
	pool[next].par_com[0] = cur;
	pool[next].par_let[0] = cur;
	Loop (i,0,lg-1) {
		pool[next].par_com[i+1] = pool[pool[next].par_com[i]].par_com[i];
		pool[next].par_let[i+1] = pool[pool[next].par_let[i]].par_let[i];
	}
	pool[next].len = pool[pool[next].par_let[0]].len + 1;
	pool[next].let = L;
	cur = next;
}

void UndoCommands(int U)
{
	int sib = cur;
	while (U >= (1 << lg)) {
		sib = pool[sib].par_com[lg-1];
		U -= 1 << (lg-1);
	}
	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];
	Loop (i,0,lg-1) {
		pool[next].par_com[i+1] = pool[pool[next].par_com[i]].par_com[i];
		pool[next].par_let[i+1] = pool[pool[next].par_let[i]].par_let[i];
	}
	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;
	while (P >= (1 << lg)) {
		ans = pool[ans].par_let[lg-1];
		P -= 1 << (lg-1);
	}
	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...