Cod sursa(job #2901061)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 12 mai 2022 20:17:41
Problema Secv8 Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.78 kb
/// always:
#include <cstdio>
#include <string>

/// optional:
#include <random>
#include <iostream>
#include <vector>
#include <algorithm>

bool home = 1;
using namespace std;
const string TASKNAME="secv8";

mt19937 rng(7);
vector<int> ord;

struct Vertex {
  int sz;
  int key;
  int value;
  int lazy;
  Vertex* lft;
  Vertex* rgh;
  Vertex() {
    key=ord.back();
    ord.pop_back();
    value=0;
    sz=0;
    lazy=0;
    lft=nullptr;
    rgh=nullptr;
  }
};

void push(Vertex* vertex) {
  if (vertex&&vertex->lazy) {
    swap(vertex->lft,vertex->rgh);

    if (vertex->lft) vertex->lft->lazy^=1;
    if (vertex->rgh) vertex->rgh->lazy^=1;

    vertex->lazy=0;
  }
}


void print(Vertex* vertex) {
  push(vertex);
  if(!vertex) return;
  print(vertex->lft);
  cout<<vertex->value<<" ";
  print(vertex->rgh);
}


void printall(Vertex*root) {
  ///cout<<"-----> ";
  print(root);
  cout<<"\n";
}


Vertex* build(Vertex* lft,Vertex* root,Vertex*rgh) {
  root->lft=lft;
  root->rgh=rgh;
  root->sz=1;
  if (root->lft) root->sz+=root->lft->sz;
  if (root->rgh) root->sz+=root->rgh->sz;

  return root;
}

pair<Vertex*, Vertex*> split(Vertex* vertex, int pos) {
  push(vertex);

  if (!vertex) {
    return make_pair(nullptr,nullptr);
  }
  int sz_lft=((vertex->lft==nullptr)?(0):(vertex->lft->sz));
  if (pos<=sz_lft) {
    auto it=split(vertex->lft, pos);
    return make_pair(it.first,build(it.second,vertex,vertex->rgh));
  }else{
    auto it=split(vertex->rgh,pos-sz_lft-1);
    return make_pair(build(vertex->lft,vertex,it.first),it.second);
  }
}

Vertex* join(Vertex* a, Vertex* b) {
  push(a);
  push(b);

  if (!a) return b;
  if (!b) return a;
  if (a->key>b->key) {
    return build(a->lft,a,join(a->rgh,b));
  }else{
    return build(join(a,b->lft),b,b->rgh);
  }
}

Vertex* inner_ins(Vertex* vertex,int pos,Vertex* e){
  push(vertex);
  if(vertex==nullptr){
    e->sz=1;
    return e;
  }

///  cout<<pos<<" vs "<<vertex->sz<<"\n";
///  cout<<"aici\n";

  if (e->key>vertex->key) {
    auto it=split(vertex,pos);
    return build(it.first,e,it.second);
  }
  int sz_lft=((vertex->lft==nullptr)?(0):(vertex->lft->sz));
  if (pos<=sz_lft) {
    return build(inner_ins(vertex->lft,pos,e),vertex,vertex->rgh);
  }else{
    return build(vertex->lft,vertex,inner_ins(vertex->rgh,pos-sz_lft-1,e));
  }
}

int get(Vertex* vertex, int pos) {
  push(vertex);
  int sz_lft=((vertex->lft==nullptr)?(0):(vertex->lft->sz));

  if (pos==sz_lft+1) {
    return vertex->value;
  }
  if (pos<=sz_lft) {
    return get(vertex->lft,pos);
  }else{
    return get(vertex->rgh,pos-(sz_lft+1));
  }
}

Vertex* ins(Vertex* root, int pos,int e) {
  Vertex* vertex=new Vertex;
  vertex->value=e;
  return inner_ins(root,pos,vertex);
}

signed main() {
#ifdef INFOARENA
  home = 0;
#endif

///  ios::sync_with_stdio(0); cin.tie(0);

  if(!home) {
    freopen((TASKNAME + ".in").c_str(), "r", stdin);

    freopen((TASKNAME + ".out").c_str(), "w", stdout);
  }else{
    freopen ("I_am_iron_man", "r", stdin);
  }

  Vertex* root=nullptr;

  ord.resize(250000+7);
  iota(ord.begin(),ord.end(),1);
  shuffle(ord.begin(),ord.end(),rng);
 /// printall(root);


 /** root=ins(root,0,10);
  root=ins(root,1,7);**/
  ///root=ins(root,0,4);
  ///cout<<(root!=nullptr)<<"\n";
 /** cout<<root->sz<<"\n";

  auto it=split(root,1);
  cout<<(it.first!=nullptr)<<" and "<<(it.second!=nullptr)<<"\n";
  cout<<it.first->sz<<" "<<it.second->sz<<"\n";
  exit(0);**/

  int q,_;
  cin>>q>>_;

  for (int iq=1;iq<=q;iq++) {
    string type;
    cin>>type;
    if (type=="I") {
      int pos,e;
      cin>>pos>>e;
      root=ins(root,pos-1,e);
    }
    if (type=="A") {
      int pos;
      cin>>pos;
      cout<<get(root,pos)<<"\n";
    }
    if (type=="R") {
      int l,r;
      cin>>l>>r;
      auto it=split(root,r);
     /// cout<<(it.first!=nullptr) <<" and "<<it.first->sz<<"\n";
      auto it2=split(it.first,l-1);
     /// cout<<(it2.first!=nullptr)<<" and "<<it2.first->sz<<"\n";
     /// cout<<(it2.second!=nullptr)<<" and "<<it2.second->sz<<"\n";

     /// exit(0);
     /// exit(0);
      it2.second->lazy^=1;
     /// cout<<(it2.second!=nullptr)<<" "<<it2.second->sz<<"\n";
     /// cout<<(it.second!=nullptr)<<"\n";

     /// exit(0);
      root=join(join(it2.first,it2.second),it.second);
    }
    if (type=="D") {
      int l,r;
      cin>>l>>r;
      auto it=split(root,r);
      auto it2=split(it.first,l-1);
      root=join(it2.first,it.second);
    }
  }
  printall(root);

  /**for (auto &x : ord) {
    cout<<x<<"\n";
  }
  cout<<"\n";**/
}

/// masina Lastun
/**
 1,
 1 2,
 1 2 3,
 1 3 2,
 1 3 2 4,
 1 3 5 2 4,
 5 3 1 2 4,
 5 2 4,
 5 1 2 4,
 2 1 5 4,
 2 1 4.
**/