An alternative solution to IOI 2012 D1Q3

Revision en1, by heavylightdecomp, 2022-04-09 16:41:29

Hello Codeforces,

//1-based indexing for everything
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000005; //just an arbitrarily high limit 
struct Segtree{
    int vcnt, root[MAX_N], rootCnt;
	char f[MAX_N * 22];
    int lc[MAX_N * 22], rc[MAX_N * 22];
	void createNode(int &x){ x = ++vcnt; }
    void build(int &x, int l, int r){
        if(x == 0) createNode(x);
        if(l == r){
            f[x] = 0;
            return;
        }
        int mid = (l + r) / 2;
        build(lc[x], l, mid);
        build(rc[x], mid + 1, r);
    }
    void update(int &x, int l, int r, int oldX, int pos, int val){
        if(x == 0) createNode(x);
        if(l == r){
            f[x] = val; //Set leaf node to new value
            return;
        }
        int mid = (l + r) / 2;
        if(pos <= mid){
            update(lc[x], l, mid, lc[oldX], pos, val);
            rc[x] = rc[oldX];
        } else {
            update(rc[x], mid + 1, r, rc[oldX], pos, val);
            lc[x] = lc[oldX];
        }
    }
    char query(int x, int l, int r, int q){
        if(x == 0) return 0;
		if(l == r) return f[x];
		int mid = (l + r) / 2;
		if(q <= mid) return query(lc[x], l, mid, q); else return query(rc[x], mid+1, r, q);
    }
};
int chs[MAX_N]; //count of chars in i'th revision, also index of last char because of 1-based indexing
Segtree T;
int sz = 1000001; //size of our "char array"
void Init() {
	T.build(T.root[++T.rootCnt], 1ll, sz);
}
void TypeLetter(char x) {
	T.rootCnt++;
	T.update(T.root[T.rootCnt], 1, sz, T.root[T.rootCnt-1], chs[T.rootCnt-1]+1, x);
	chs[T.rootCnt] = chs[T.rootCnt-1] + 1;
}
void UndoCommands(signed g) {
	T.rootCnt++;
	T.root[T.rootCnt] = T.root[T.rootCnt-g-1];
	chs[T.rootCnt] = chs[T.rootCnt-g-1];
} 
char GetLetter(signed p) {
	return T.query(T.root[T.rootCnt], 1, sz, p+1);
}
Tags ioi, segtree, persistent segment trees, ioi2012, data structures

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en16 English heavylightdecomp 2022-04-09 18:06:40 3268 Reverted to en14
en15 English heavylightdecomp 2022-04-09 18:06:30 3268 Reverted to en1
en14 English heavylightdecomp 2022-04-09 18:04:52 5 (published)
en13 English heavylightdecomp 2022-04-09 18:01:14 4
en12 English heavylightdecomp 2022-04-09 18:00:24 12
en11 English heavylightdecomp 2022-04-09 17:55:38 12 Tiny change: 'etty cool and concise so I deci' -> 'etty cool so I deci'
en10 English heavylightdecomp 2022-04-09 17:55:16 37 Tiny change: 'ng problem with many different methods to solve. I think ' -> 'ng problem. I think '
en9 English heavylightdecomp 2022-04-09 17:51:40 6
en8 English heavylightdecomp 2022-04-09 17:51:03 2 Tiny change: 'revision $$O(1)$$. Point u' -> 'revision $O(1)$. Point u'
en7 English heavylightdecomp 2022-04-09 17:50:14 2974
en6 English heavylightdecomp 2022-04-09 17:48:56 46
en5 English heavylightdecomp 2022-04-09 17:39:39 15 Tiny change: 'pdf)).\n\n<spoil' -> 'pdf)).\n\n\n\n\n<spoil'
en4 English heavylightdecomp 2022-04-09 17:38:54 78
en3 English heavylightdecomp 2022-04-09 17:37:52 627
en2 English heavylightdecomp 2022-04-09 17:28:27 2610
en1 English heavylightdecomp 2022-04-09 16:41:29 1947 Initial revision (saved to drafts)