Advertisement
rotti321

Untitled

Mar 3rd, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct nod{
  5.     unsigned int time, prior, lazy = 0;
  6.     nod *st, *dr;
  7.     nod(){}
  8.     nod(const int a, const int b, const int c, nod* x, nod *y):
  9.         time(a), prior(b), lazy(c), st(x), dr(y){} } *nil = new nod (0, 0, 0, 0, 0);
  10.  
  11. nod *apply_prop(nod *cur, const int delta){
  12.     nod *rrez = new nod;
  13.     *rrez = *cur;
  14.     rrez->lazy += delta;
  15.     return rrez; }
  16.  
  17. nod *prop(nod *cur){
  18.     nod *rrez = new nod;
  19.     *rrez = *cur;
  20.     rrez->st = apply_prop(rrez->st, rrez->lazy);
  21.     rrez->dr = apply_prop(rrez->dr, rrez->lazy);
  22.     rrez->lazy = 0;
  23.     return rrez; }
  24.  
  25. nod *mod_fiu(nod *cur, const int care, nod *fiu){
  26.     nod *rrez = new nod;
  27.     *rrez = *cur;
  28.     (care ? rrez->dr : rrez->st) = fiu;
  29.     return rrez; }
  30.  
  31. nod *join(nod *st, nod *dr){
  32.     if(st == nil) return dr;
  33.     if(dr == nil) return st;
  34.     st = prop(st), dr = prop(dr);
  35.     return (st->prior > dr->prior) ?
  36.         mod_fiu(st, 1, join(st->dr, dr)) :
  37.         mod_fiu(dr, 0, join(st, dr->st)); }
  38.  
  39. pair<nod*, nod*> split(nod *n, const int middle){
  40.     if(n == nil) return make_pair(n, n);
  41.     n = prop(n);
  42.     if(n->time < middle){
  43.         auto p = split(n->dr, middle);
  44.         return make_pair(mod_fiu(n, 1, p.first), p.second); }
  45.     else{
  46.         auto p = split(n->st, middle);
  47.         return make_pair(p.first, mod_fiu(n, 0, p.second)); } }
  48.  
  49. nod *update(nod *n, const int st, const int dr, const int delta){
  50.     pair<nod*, nod*> tmp = split(n, st), tmp_ = split(tmp.second, dr);
  51.     tmp_.first = apply_prop(tmp_.first, delta);
  52.     return join(tmp.first, join(tmp_.first, tmp_.second)); }
  53.  
  54. int query(nod *n, const int val){
  55.     if(n == nil) return 0;
  56.     else if(n->time == val) return n->lazy;
  57.     else if(n->time < val) return n->lazy + query(n->dr, val);
  58.     else return n->lazy + query(n->st, val); }
  59.  
  60. int main(){
  61.     map<int, nod*> rads = {{0, nil}};
  62.     map<int, int> prev_ap = {};
  63.     while(true){
  64.         string str;
  65.         int a, b;
  66.         cin >> str >> a >> b;
  67.         if(str == "ADD_USER"){
  68.             // a = timp, distincte, a > 0
  69.             // b = user_index
  70.             auto last_rad = rads.rbegin()->second;
  71.             rads[a] = join(last_rad, new nod (a, rand(), 0, nil, nil ));
  72.             rads[a] = update(rads[a], prev_ap[b] + 1, a, 1);
  73.             prev_ap[b] = a; }
  74.         else{
  75.             cout << query(rads[b], a) << endl;
  76.         }
  77.     }
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement