Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define endl "\n"
- using namespace std;
- typedef long long ll;
- struct node{
- node * l = nullptr, * r = nullptr;
- int cnt=0;
- int dif=0;
- int uni=0;
- node(){}
- node(node * left, node * right){
- l = left;
- r = right;
- cnt = left->cnt + right->cnt;
- dif = left->dif + right->dif;
- uni = left->uni + right->uni;
- }
- node(int c){
- cnt = c;
- dif = 1;
- uni = c == 1;
- }
- };
- typedef node* pnode;
- vector<pnode> versions;
- pnode build(int tl, int tr){
- if(tl == tr){
- return new node();
- } else {
- int tm = (tl + tr) / 2;
- return new node(build(tl, tm), build(tm+1, tr));
- }
- }
- int different(int v){
- return versions[v]->dif;
- }
- int unique(int v){
- return versions[v]->uni;
- }
- int count(pnode v, int tl, int tr, int val){
- if(tl == tr){
- return v->cnt;
- } else {
- int tm = (tl + tr) / 2;
- if(tm >= val) {
- return count(v->l, tl, tm, val);
- } else {
- return count(v->r, tm + 1, tr, val);
- }
- }
- }
- pnode add(pnode v, int tl, int tr, int val){
- if(tl == tr){
- return new node(v->cnt + 1);
- } else {
- int tm = (tl + tr) / 2;
- if(tm >= val) {
- return new node(add(v->l, tl, tm, val), v->r);
- } else {
- return new node(v->l,add(v->r, tm+1, tr, val));
- }
- }
- }
- pnode remove(pnode v, int tl, int tr, int val){
- if(tl == tr){
- if(v->cnt == 0) return nullptr;
- return new node(v->cnt - 1);
- } else {
- int tm = (tl + tr) / 2;
- if(tm >= val) {
- pnode nxt_l = remove(v->l, tl, tm, val);
- if(nxt_l == nullptr) return nullptr;
- return new node(nxt_l, v->r);
- } else {
- pnode nxt_r = remove(v->r, tm+1, tr, val);
- if(nxt_r == nullptr) return nullptr;
- return new node(v->l, nxt_r);
- }
- }
- }
- int n,m;
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cin>>n>>m;
- versions.push_back(build(1, m));
- int s = 0;
- string type;
- int v,x,y;
- int value;
- for(int i=1;i<=n;i++){
- cin>>type;
- if(type[0]=='a'){
- cin>>x;
- versions.push_back(add(versions[i-1], 1, m, x));
- } else if (type[0]=='r') {
- cin>>x;
- pnode ans = remove(versions[i-1], 1, m, x);
- if(ans == nullptr){
- versions.push_back(versions[i-1]);
- } else {
- versions.push_back(ans);
- }
- } else if (type[0]=='d') {
- cin>>y;
- v = (y + s) % i;
- v = different(v);
- s+=v;
- cout<<v<<endl;
- versions.push_back(versions[i-1]);
- } else if (type[0]=='u') {
- cin>>y;
- v = (y + s) % i;
- v = unique(v);
- s+=v;
- cout<<v<<endl;
- versions.push_back(versions[i-1]);
- } else if (type[0]=='c') {
- cin>>x>>y;
- v = (y + s) % i;
- v = count(versions[v], 1, m, x);
- s += v;
- cout<<v<<endl;
- versions.push_back(versions[i-1]);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement