Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 – Final Round Div. 2 Edition), problem: (E) Nikita and stack Solution in C/C++

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5;

int a[maxn];

struct segtree{
int lb, rb;
int mx = 0, d = 0;
segtree *l, *r;
segtree(int _lb, int _rb){
lb = _lb, rb = _rb;
if(lb != rb){
int t = (lb + rb) / 2;
l = new segtree(lb, t);
r = new segtree(t+1, rb);
}
}
void push(){
if(lb != rb){
l->mx += d, l->d += d;
r->mx += d, r->d += d;
d = 0;
}
}
void add(int lq, int rq, int x){
if(rb < lq || lb > rq) return;
if(lb >= lq && rb <= rq) mx += x, d += x;
else{
push();
l->add(lq, rq, x);
r->add(lq, rq, x);
mx = max(l->mx, r->mx);
}
}
int get(){
//cerr << “get ” << lb << ” ” << rb << ” ” << mx << endl;
push();
if(mx <= 0) return -1;
if(lb == rb) return a[lb];
int resp = r->get();
if(resp != -1) return resp;
else return l->get();
}
};

int main(){
ios::sync_with_stdio(0);
cin.tie(0);

int n;
cin >> n;

segtree p(0, n-1);

while(n–){
int k, t;
cin >> k >> t;
k–;
if(!t) p.add(0, k, -1);
else{
p.add(0, k, 1);
int x;
cin >> x;
a[k] = x;
}
cout << p.get() << “\n”;
}

return 0;
}

(Visited 63 times, 1 visits today)

About the Author:

Leave A Comment