Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <assert.h>
- #include <unordered_map>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- typedef vector < long long > vll;
- typedef pair < long long, long long > pll;
- typedef pair < int, int > pii;
- typedef vector < int > vii;
- #define csl ios_base::sync_with_stdio(false); cin.tie(NULL)
- #define l(x) (((x) << 1) | 1)
- #define r(x) ((l(x)) + 1)
- #define mp make_pair
- #define fst first
- #define snd second
- ll t, n, u, v, m, q, r, ql, qr, k, l, s, x, y, w;
- const int N = 1e6 + 500;
- const int LN = 23;
- const long long mod = 1e9 + 7;
- const long long INF = 1LL << 57LL;
- const bool JUDGE = false;
- int h = 1;
- struct node{
- char letter;
- int time;
- int parent;
- int L[LN];
- node () {
- parent = -1;
- time = 0;
- letter = '$';
- memset(L, -1, sizeof(L));
- }
- node (char letter, int time, int parent) {
- this -> letter = letter;
- this -> time = time;
- this -> parent = parent;
- memset(L, -1, sizeof(L));
- }
- }trie[N];
- inline void typeletter(char l) {
- trie[h].letter = l;
- trie[h].time = trie[h - 1].time + 1;
- trie[h].parent = h - 1;
- trie[h].L[0] = trie[h].parent;
- for (int i = 0; i < LN - 1; ++i) {
- if (trie[h].L[i] == -1) break;
- trie[h].L[i + 1] = trie[trie[h].L[i]].L[i];
- }
- h++;
- }
- inline void undo(int x) {
- trie[h] = trie[h - x - 1];
- h++;
- }
- char getletter(int x) {
- int j = trie[h - 1].time;
- j = j - x;
- x = h - 1;
- for (int i = 0; i < LN; ++i) {
- if (j & (1 << i)) {
- //cout << x << " " ;
- x = trie[x].L[i];
- //cout << x << '\n';
- }
- }
- return trie[x].letter;
- }
- char str;
- int main(){
- csl;
- if (JUDGE) {
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- }
- cin >> n;
- for (int i = 0; i < n; ++i) {
- cin >> str;
- if (str == 'T') {
- char x;
- cin >> x;
- typeletter(x);
- }
- else if (str == 'P') {
- cin >> u;
- cout << getletter(int(u) + 1) << '\n';
- }
- else {
- cin >> u;
- undo(u);
- }
- /* for (int i = 1; i < h; ++i) {
- cout << trie[i].letter << " " << trie[i].time << " " << trie[i].parent << '\n';
- }*/
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement