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;
- struct Node{
- int cnt;
- int prior;
- string value;
- Node* left;
- Node* right;
- Node (): cnt(-1), prior(-1), left(NULL), right(NULL) { }
- Node ( int cnt, int prior ) : cnt(cnt), prior(prior), left(NULL), right(NULL) { }
- };
- class Treap {
- private:
- Node* root;
- Node* new_node(string val){
- Node* res = new Node();
- res->prior = rand();
- res->cnt = 1;
- res->value = val;
- res->left = res->right = NULL;
- return res;
- }
- Node* merge (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, string val );
- void remove ( int pos );
- string get_value (Node* node, int pos );
- };
- Treap :: Treap ( ) {
- root = NULL;
- }
- Treap::~Treap()
- {
- clear(root);
- }
- void Treap::clear(Node* node)
- {
- if (node == nullptr)
- return;
- clear(node->left);
- clear(node->right);
- delete node;
- }
- Node* Treap :: merge ( Node* t1, Node* t2 ) {
- if (t1 == NULL) { return t2; }
- if (t2 == NULL) { return t1; }
- if (t1->prior > t2->prior)
- {
- t1->right = merge(t1->right, t2);
- update_cnt(t1);
- return t1;
- }
- else
- {
- t2->left = merge(t1, t2->left);
- update_cnt(t2);
- return t2;
- }
- }
- void Treap :: split ( Node* node, int key, Node *&t1, Node *&t2 ) {
- if (node == NULL) {
- t1 = t2 = NULL;
- 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);
- }
- void Treap :: add ( int pos, string val ) {
- Node *t1, *t2;
- split(root, pos, t1, t2);
- Node* new_tree = new_node(val);
- root = merge(merge(t1, new_tree), t2);
- }
- void Treap ::remove ( int pos ){
- Node *t1, *t2, *t3, *t4;
- split(root, pos, t1, t2);
- split(t2, pos + 1, t3, t4);
- root = merge(t1, t4);
- delete t3;
- }
- string Treap :: 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 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(NULL, 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