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

Why Electric Cleaning Tools...

📢 As an Amazon Associate, I earn from qualifying...

Everything You Need to...

📢 As an Amazon Associate, I earn from qualifying...

China’s largest memory chipmaker...

China's semiconductor ambitions are facing a fresh financial dilemma...

The Real Truth About...

📢 As an Amazon Associate, I earn from qualifying...

Is Desk Productivity Tools...

📢 As an Amazon Associate, I earn from qualifying...

The Outer Worlds 2...

Gamers and RPG enthusiasts have reason to celebrate as...

Why Electric Cleaning Tools Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Electric Cleaning Tools?The Electric Cleaning Tools...

Everything You Need to Know About Portable Cooling Gear Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Portable Cooling Gear?The Portable Cooling Gear...

China’s largest memory chipmaker sparks fears of a cash drain before blockbuster IPO

China's semiconductor ambitions are facing a fresh financial dilemma as ChangXin Memory Technologies (CXMT), the nation's premier DRAM chipmaker, prepares for a highly anticipated...

The Real Truth About Bedroom Aesthetic Decor Before You Buy

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Bedroom Aesthetic Decor?The Bedroom Aesthetic Decor...

Is Desk Productivity Tools Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Desk Productivity Tools?The Desk Productivity Tools...

The Outer Worlds 2 v1.2.0.1-TENOKE

Gamers and RPG enthusiasts have reason to celebrate as Obsidian Entertainment's acclaimed franchise returns with new developments. The highly anticipated sequel to the award-winning...

Why Travel Organization Gear Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Travel Organization Gear?The Travel Organization Gear...

Why Everyone Is Talking About the Car Detailing Essentials

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Car Detailing Essentials?The Car Detailing Essentials...

Why Everyone Is Talking About the Smart Home Hacks

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Smart Home Hacks?The Smart Home Hacks...