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;
}

Hegseth Hails US Allies...

In a striking shift of foreign policy rhetoric, newly...

Labour markets may risk...

The Reality Check of AI in the Modern Labor...

Why Everyone is Obsessed with the Universal Charging Cable

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Fridge Odor Eliminator

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Hegseth Hails US Allies in Asia, Hits Out at Europe Partners

In a striking shift of foreign policy rhetoric, newly appointed U.S. Defense Secretary Pete Hegseth has praised America's allies in Asia while delivering sharp...

Why Everyone is Obsessed with the Reusable Silicone Food Bags

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Motion Sensor Night Lights

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Labour markets may risk a milder shock than AI fantasies suggest, but that’s only partial relief

The Reality Check of AI in the Modern Labor Market For the past few years, the dominant narrative surrounding artificial intelligence (AI) has been one...

Why Everyone is Obsessed with the TSA Approved Toiletry Bags

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Phone Screen Cleaner Spray

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Edmonton hospital using medical technology to offer non-surgical weight loss procedure

The Royal Alexandra Hospital in Edmonton is pioneering a new era of healthcare in Alberta by offering an innovative, non-surgical weight loss procedure. This...