Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <math.h>
- #include <stack>
- #include <vector>
- #include <memory>
- using namespace std;
- template<typename t>
- class Treap {
- private:
- struct Node{
- int cnt;
- int prior;
- t value;
- shared_ptr<Node> left;
- shared_ptr<Node> right;
- Node (): cnt(-1), prior(-1), left(nullptr), right(nullptr) { }
- Node ( int cnt, int prior ) : cnt(cnt), prior(prior), left(nullptr), right(nullptr) { }
- };
- shared_ptr<Node> root;
- shared_ptr<Node> new_node(t val){
- shared_ptr<Node> res(new Node);
- res->prior = rand();
- res->cnt = 1;
- res->value = val;
- res->left = res->right = nullptr;
- return res;
- }
- void merge (Node* &node, Node* l, Node* r);
- void split ( Node* node, int cnt, Node* & left, Node* & right );
- void delete_tree ( Node* node );
- int cnt (Node* node) {
- return node ? node->cnt : 0;
- }
- void update_cnt (Node* node) {
- if (node)
- node->cnt = cnt(node->left) + cnt(node->right) + 1;
- }
- public:
- Treap ( );
- ~Treap ( );
- void add ( int pos, t val );
- void remove ( int pos );
- t get_value (Node* node, int pos );
- };
- template<typename t>
- Treap<t> :: Treap ( ) {
- root = nullptr;
- }
- template<typename t>
- void Treap<t> :: merge (Node* &node, Node* t1, Node* t2) {
- if (t1 == nullptr)
- node = t2;
- if (t2 == nullptr)
- node = t1;
- if (t1->prior > t2->prior) {
- merge(t1->right, t1->right, t2);
- update_cnt(t1);
- node = t1;
- }
- else {
- merge(t2->left, t1, t2->left);
- update_cnt(t2);
- node = t2;
- }
- }
- template<typename t>
- void Treap<t> :: split ( Node* node, int key, Node *&t1, Node *&t2 ) {
- if (node == nullptr) {
- t1 = t2 = nullptr;
- return;
- }
- if (cnt(node->left) < key){
- split(node->right, key - cnt(node->left) - 1, node->right, t2);
- t1 = node;
- }
- else {
- split(node->left, key, t1, node->left);
- t2 = node;
- }
- update_cnt(node);
- }
- template<typename t>
- void Treap<t> :: add ( int pos, t val ) {
- Node *t1, *t2, *t3;
- split(root, pos, t1, t2);
- Node* new_tree = new_node(val);
- merge(t3, t1, new_tree);
- merge(root, t3, t2);
- }
- template<typename t>
- void Treap<t> ::remove ( int pos ){
- Node *t1, *t2, *t3, *t4;
- split(root, pos, t1, t2);
- split(t2, pos + 1, t3, t4);
- merge(root, t1, t4);
- delete t3;
- }
- template<typename t>
- t Treap<t> :: get_value (Node* node, int pos ){
- if ( !node )
- node = root;
- int my_idx = cnt ( node->left );
- if ( pos < my_idx )
- return get_value ( node->left, pos );
- else if ( pos == my_idx )
- return node->value;
- else
- return get_value(node->right, pos - my_idx - 1);
- }
- class ArrayString {
- public:
- ArrayString () { }
- void InsertAt( int position, const string& value );
- void DeleteAt( int position );
- string GetAt( int position );
- private:
- Treap<string> tree;
- };
- void ArrayString :: InsertAt( int position, const string& value ){
- tree.add(position, value);
- }
- void ArrayString:: DeleteAt( int position ){
- tree.remove(position);
- }
- string ArrayString:: GetAt( int position ){
- return tree.get_value(nullptr, position);
- }
- int main(){
- ArrayString ars;
- int k = 0;
- cin >> k;
- for(int i = 0; i < k; ++i){
- string s;
- cin >> s;
- if(s[0] == '+'){
- int n = 0;
- int j = 2;
- string val;
- while(s[j] != ' '){
- n += int(s[j]) * pow(10, j - 2);
- ++j;
- }
- for(; j < s.size(); ++j)
- val.push_back(s[j]);
- ars.InsertAt(n, val);
- }
- else{
- int n = 0, j = 2;
- while ( j < s.size() ){
- n += (s[j] - 64) * pow(10, j);
- ++j;
- }
- if(s[0] == '-')
- ars.DeleteAt(n);
- else
- ars.GetAt(n);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement