Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct nod{
- unsigned int time, prior, lazy = 0;
- nod *st, *dr;
- nod(){}
- nod(const int a, const int b, const int c, nod* x, nod *y):
- time(a), prior(b), lazy(c), st(x), dr(y){} } *nil = new nod (0, 0, 0, 0, 0);
- nod *apply_prop(nod *cur, const int delta){
- nod *rrez = new nod;
- *rrez = *cur;
- rrez->lazy += delta;
- return rrez; }
- nod *prop(nod *cur){
- nod *rrez = new nod;
- *rrez = *cur;
- rrez->st = apply_prop(rrez->st, rrez->lazy);
- rrez->dr = apply_prop(rrez->dr, rrez->lazy);
- rrez->lazy = 0;
- return rrez; }
- nod *mod_fiu(nod *cur, const int care, nod *fiu){
- nod *rrez = new nod;
- *rrez = *cur;
- (care ? rrez->dr : rrez->st) = fiu;
- return rrez; }
- nod *join(nod *st, nod *dr){
- if(st == nil) return dr;
- if(dr == nil) return st;
- st = prop(st), dr = prop(dr);
- return (st->prior > dr->prior) ?
- mod_fiu(st, 1, join(st->dr, dr)) :
- mod_fiu(dr, 0, join(st, dr->st)); }
- pair<nod*, nod*> split(nod *n, const int middle){
- if(n == nil) return make_pair(n, n);
- n = prop(n);
- if(n->time < middle){
- auto p = split(n->dr, middle);
- return make_pair(mod_fiu(n, 1, p.first), p.second); }
- else{
- auto p = split(n->st, middle);
- return make_pair(p.first, mod_fiu(n, 0, p.second)); } }
- nod *update(nod *n, const int st, const int dr, const int delta){
- pair<nod*, nod*> tmp = split(n, st), tmp_ = split(tmp.second, dr);
- tmp_.first = apply_prop(tmp_.first, delta);
- return join(tmp.first, join(tmp_.first, tmp_.second)); }
- int query(nod *n, const int val){
- if(n == nil) return 0;
- else if(n->time == val) return n->lazy;
- else if(n->time < val) return n->lazy + query(n->dr, val);
- else return n->lazy + query(n->st, val); }
- int main(){
- map<int, nod*> rads = {{0, nil}};
- map<int, int> prev_ap = {};
- while(true){
- string str;
- int a, b;
- cin >> str >> a >> b;
- if(str == "ADD_USER"){
- // a = timp, distincte, a > 0
- // b = user_index
- auto last_rad = rads.rbegin()->second;
- rads[a] = join(last_rad, new nod (a, rand(), 0, nil, nil ));
- rads[a] = update(rads[a], prev_ap[b] + 1, a, 1);
- prev_ap[b] = a; }
- else{
- cout << query(rads[b], a) << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement