Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class Node{
  7. public:
  8.     int different, unique, count;
  9.     Node *l, *r;
  10.  
  11.     Node(int x = 0) {
  12.         different = unique = count = x;
  13.         l = r = nullptr;
  14.     }
  15.  
  16.     Node(Node* node) {
  17.         different = node->different;
  18.         unique = node->unique;
  19.         count = node->count;
  20.         l = node->l;
  21.         r = node->r;
  22.     }
  23. };
  24.  
  25. vector<Node*> versions;
  26. int m;
  27.  
  28. Node* build(int tl, int tr) {
  29.     if (tl == tr) {
  30.         return new Node();
  31.     } else {
  32.         int mid = (tl + tr) / 2;
  33.  
  34.         Node* tmp = new Node();
  35.  
  36.         tmp->l = build(tl, mid);
  37.         tmp->r = build(mid + 1, tr);
  38.  
  39.         return tmp;
  40.     }
  41. }
  42.  
  43. void recalc(Node* node) {
  44.     if (!node) return;
  45.  
  46.     node->different = (node->l ? node->l->different : 0) + (node->r ? node->r->different : 0);
  47.     node->unique = (node->l ? node->l->unique : 0) + (node->r ? node->r->unique : 0);
  48. }
  49.  
  50. Node* update(Node* v, int tl, int tr, int x, int delta) {
  51.     if (tl == tr) {
  52.         Node* tmp = new Node(v);
  53.  
  54.         tmp->count += delta;
  55.         tmp->different = (tmp->count > 0);
  56.         tmp->unique = (tmp->count == 1);
  57.  
  58.         return tmp;
  59.     } else {
  60.         int mid = (tl + tr) / 2;
  61.  
  62.         Node* tmp = new Node(v);
  63.  
  64.         if (x <= mid) {
  65.             tmp->l = update(tmp->l, tl, mid, x, delta);
  66.         } else {
  67.             tmp->r = update(tmp->r, mid + 1, tr, x, delta);
  68.         }
  69.  
  70.         recalc(tmp);
  71.  
  72.         return tmp;
  73.     }
  74. }
  75.  
  76. int get(Node* v, int tl, int tr, int x) {
  77.     if (tl == tr) {
  78.         return v->count;
  79.     } else {
  80.         int mid = (tl + tr) / 2;
  81.  
  82.         if (x <= mid) {
  83.             return get(v->l, tl, mid, x);
  84.         } else {
  85.             return get(v->r, mid + 1, tr, x);
  86.         }
  87.     }
  88. }
  89.  
  90. int different(int v) {
  91.     versions.push_back(versions.back());
  92.     return versions[v]->different;
  93. }
  94.  
  95. int unique(int v) {
  96.     versions.push_back(versions.back());
  97.     return versions[v]->unique;
  98. }
  99.  
  100. int count(int v, int x) {
  101.     versions.push_back(versions.back());
  102.     return get(versions[v], 1, m, x);
  103. }
  104.  
  105. int main() {
  106.     int n, s = 0;
  107.  
  108.     cin >> n >> m;
  109.  
  110.     versions.push_back(build(1, m));
  111.  
  112.     for (int i = 0; i != n; ++i) {
  113.         string com;
  114.  
  115.         cin >> com;
  116.  
  117.         if (com == "different") {
  118.             int v, ans;
  119.  
  120.             cin >> v;
  121.  
  122.             v = (v + s) % versions.size();
  123.  
  124.             ans = different(v);
  125.             s += ans;
  126.  
  127.             cout << ans << '\n';
  128.         } else if (com == "unique") {
  129.             int v, ans;
  130.  
  131.             cin >> v;
  132.  
  133.             v = (v + s) % versions.size();
  134.  
  135.             ans = unique(v);
  136.             s += ans;
  137.  
  138.             cout << ans << '\n';
  139.         } else if (com == "count") {
  140.             int v, x, ans;
  141.  
  142.             cin >> v >> x;
  143.  
  144.             v = (v + s) % versions.size();
  145.  
  146.             ans = count(v, x);
  147.             s += ans;
  148.  
  149.             cout << ans << '\n';
  150.         } else if (com == "add") {
  151.             int x;
  152.  
  153.             cin >> x;
  154.  
  155.             versions.push_back(update(versions.back(), 1, m, x, 1));
  156.         } else if (com == "remove") {
  157.             int x;
  158.  
  159.             cin >> x;
  160.  
  161.             if (count(versions.size() - 1, x) != 0) {
  162.                 versions.push_back(update(versions.back(), 1, m, x, -1));
  163.             }
  164.         }
  165.     }
  166.  
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement