Submission #153044

#TimeUsernameProblemLanguageResultExecution timeMemory
153044popovicirobertCrayfish scrivener (IOI12_scrivener)C++14
34 / 100
1083 ms157560 KiB
#include <bits/stdc++.h>

using namespace std;

static const int MAXN = (int) 1e6;

static int anc_let[20][MAXN + 1], anc[20][MAXN + 1];
static int len[MAXN + 1], cur, sz;
static char let[MAXN + 1];

void Init() {
    cur = sz = 0;
    len[0] = 0;
}

void TypeLetter(char L) {
    sz++;
    let[sz] = L;
    len[sz] = len[cur] + 1;

    anc_let[0][sz] = cur;
    anc[0][sz] = cur;

    for(int bit = 1; bit < 20; bit++) {
        anc[bit][sz] = anc[bit - 1][anc[bit - 1][sz]];
        anc_let[bit][sz] = anc_let[bit - 1][anc_let[bit - 1][sz]];
    }

    cur = sz;
}

void UndoCommands(int U) {
    int last = cur;
    for(int bit = 0; bit < 20; bit++) {
        if(U & (1 << bit)) {
            cur = anc[bit][cur];
        }
    }
    sz++;
    let[sz] = let[cur];
    len[sz] = len[cur];

    cur = anc_let[0][cur];

    anc[0][sz] = last;
    anc_let[0][sz] = cur;

    for(int bit = 1; bit < 20; bit++) {
        anc[bit][sz] = anc[bit - 1][anc[bit - 1][sz]];
        anc_let[bit][sz] = anc_let[bit - 1][anc_let[bit - 1][sz]];
    }

    cur = sz;
}

char GetLetter(int P) {
    int ans = cur;
    P = len[cur] - P - 1;
    for(int bit = 0; bit < 20; bit++) {
        if(P & (1 << bit)) {
            ans = anc_let[bit][ans];
        }
    }
    return let[ans];
}
#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...