Submission #1004723

#TimeUsernameProblemLanguageResultExecution timeMemory
1004723Ausp3xCrayfish scrivener (IOI12_scrivener)C++17
12 / 100
728 ms125364 KiB
// 人外有人,天外有天
// author: Ausp3x

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

typedef long long             lng;
typedef unsigned int          uint;
typedef unsigned long long    ulng;
using namespace std;
using namespace __gnu_pbds;

int const INF32 = 0x3f3f3f3f;
lng const INF64 = 0x3f3f3f3f3f3f3f3f;

unordered_map<lng, int> cmps;
vector<lng> expd, history(1, -1);
vector<vector<int>> Trie;

int getLen(lng c) {
    c = cmps[c];

    int len = 0;
    while (c != 0) {
        len += 1 << Trie[c].size() - 1;
        c = Trie[c][Trie[c].size() - 1];
    }

    return len;
}

lng queryUp(lng c, int q) {
    c = cmps[c];

    int i = 0;
    while (q > 0) {
        if (i >= Trie[c].size())
            return -INF64;

        if (q & 1)
            c = Trie[c][i];
        
        i++;
        q >>= 1;
    }

    return expd[c];
}

void debug() {
    cout << endl;
    cout << "cmps: " << endl;
    for (auto [k, v] : cmps)
        cout << k << ": " << v << endl;
    cout << endl;
    cout << "Trie: " << endl;
    for (int i = 0; i < Trie.size(); i++) {
        cout << expd[i] << ": ";
        for (int x : Trie[i])
            cout << expd[x] << ' ';
        cout << endl;
    }
    cout << endl;
    cout << "history: " << endl;
    for (lng x : history)
        cout << x << ' ';
    cout << endl;
    cout << endl;

    return;
}

void Init() {
    cmps[-1] = 0;
    expd.push_back(-1);
    Trie.push_back({});
    
    return;
}

void TypeLetter(char l) {
    lng prv = history.back(), p_lv = (prv != -1 ? abs(prv) % 100000000 / 26 : -1);
    lng cur = pow(-1, prv == -1) * (abs(prv) % 100000000 * 100000000 + (p_lv + 1) * 26 + (l - 'a'));
    history.push_back(cur);
    if (cmps.find(cur) == cmps.end()) {
        cmps[cur] = Trie.size();
        expd.push_back(cur);
        
        prv = cmps[prv];
        cur = cmps[cur];
        Trie.push_back({prv});
        for (int i = 0; i < 19; i++) {
            if (i >= Trie[prv].size())
                break;
            
            prv = Trie[prv][i];
            Trie[cur].push_back(prv);
        }
    }

    return;
}

void UndoCommands(int u) {
    history.push_back(history[history.size() - u - 1]);

    return;
}

char GetLetter(int p) {
    int len = getLen(history.back());

    return 'a' + (abs(queryUp(history.back(), len - p - 1)) % 100000000 % 26);
}

/*
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    Init();
    TypeLetter('a');
    TypeLetter('b');
    cout << GetLetter(1) << endl;
    TypeLetter('d');
    UndoCommands(2);
    UndoCommands(1);
    cout << GetLetter(2) << endl;
    TypeLetter('e');
    UndoCommands(1);
    UndoCommands(5);
    TypeLetter('c');
    cout << GetLetter(2) << endl;
    UndoCommands(2);
    cout << GetLetter(2) << endl;
    for (int i = 0; i < 500000; i++)
        TypeLetter('z');
    for (int i = 0; i < 500000; i++)
        cout << GetLetter(10) << endl;

    return 0;
}
//*/

Compilation message (stderr)

scrivener.cpp: In function 'int getLen(lng)':
scrivener.cpp:25:36: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   25 |         len += 1 << Trie[c].size() - 1;
      |                     ~~~~~~~~~~~~~~~^~~
scrivener.cpp: In function 'lng queryUp(lng, int)':
scrivener.cpp:37:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |         if (i >= Trie[c].size())
      |             ~~^~~~~~~~~~~~~~~~~
scrivener.cpp: In function 'void debug()':
scrivener.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for (int i = 0; i < Trie.size(); i++) {
      |                     ~~^~~~~~~~~~~~~
scrivener.cpp: In function 'void TypeLetter(char)':
scrivener.cpp:91:25: warning: narrowing conversion of 'prv' from 'lng' {aka 'long long int'} to 'int' [-Wnarrowing]
   91 |         Trie.push_back({prv});
      |                         ^~~
scrivener.cpp:91:25: warning: narrowing conversion of 'prv' from 'lng' {aka 'long long int'} to 'int' [-Wnarrowing]
scrivener.cpp:93:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |             if (i >= Trie[prv].size())
      |                 ~~^~~~~~~~~~~~~~~~~~~
#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...