Question link: Click the open link
The minimum x value for maintaining the Y value greater than the Val value in the line segment tree
# Include <cstdio> # include <cstring> # include <algorithm> # include <vector> # include <iostream> # include <map> # include <set> # include <math. h> using namespace STD; # define INF 1000000010 # define ll int # define n 200005 # define L (x) (x <1) # define R (X) (x <1 | 1) inline ll mid (ll a, LL B) {return (a + B)> 1 ;}ll N; struct node {ll X, y; node (ll a = 0, LL B = 0): X (A), y (B) {} bool operator <(const node &) const {if (. X = x) return. Y> Y; return. x> X;} ll op;} in [N]; set <node> myset; set <node >:: iterator P; struct edge {int L, R, ID; int Maxx, lval; set <ll> st;} tree [n <4]; set <ll >:: iterator pp; set <ll> lisan; void build (int l, int R, int ID) {tree [ID]. L = L, tree [ID]. R = r; tree [ID]. maxx = tree [ID]. lval =-1; tree [ID]. st. clear (); tree [ID]. st. insert (-1); If (L = r) return; int mid = mid (L, R); Build (L, mid, L (ID )); build (Mid + 1, R, R (ID);} void updata (LL POs, LL ID, ll Val, ll o) {// o = 1 indicates inserting if (tree [ID]. L = tree [ID]. r) {If (O = 1) {tree [ID]. st. insert (VAL); tree [ID]. lval = tree [ID]. maxx = max (tree [ID]. maxx, Val); return;} else tree [ID]. st. erase (VAL); pp = tree [ID]. st. end (); pp --; tree [ID]. lval = tree [ID]. maxx = * PP; return;} ll mid = mid (tree [ID]. l, tree [ID]. r); If (mid <POS) updata (Pos, R (ID), Val, O); else updata (Pos, L (ID), Val, O ); tree [ID]. lval = tree [L (ID)]. lval; tree [ID]. Maxx = max (tree [L (ID)]. maxx, tree [R (ID)]. maxx);} ll query (ll l, ll R, ll ID, ll Val) {If (L <= tree [ID]. L & tree [ID]. r <= r) {If (tree [ID]. maxx <= Val) Return-1; if (tree [ID]. lval> Val) return tree [ID]. l;} ll mid = mid (tree [ID]. l, tree [ID]. r); If (mid <L) return query (L, R, R (ID), Val); else if (r <= mid) return query (L, R, L (ID), Val); LL ans = query (L, mid, L (ID), Val); If (ANS! =-1) return ans; return query (Mid + 1, R, R (ID), Val);} ll POS [n <1]; Map <LL, ll> MP; int main () {ll X, Y; while (~ Scanf ("% d", & N) {myset. clear (); MP. clear (); lisan. clear (); For (ll I = 0; I <n; I ++) {char s [10]; scanf ("% s", S ); scanf ("% d", & in [I]. x, & in [I]. y); lisan. insert (in [I]. x); If (s [0] = 'A') in [I]. OP = 1; else if (s [0] = 'R') in [I]. OP = 2; else in [I]. OP = 3;} ll siz = 1; for (Pp = lisan. begin (); pp! = Lisan. end (); pp ++) {pos [siz] = * PP; MP. insert (pair <LL, ll> (* PP, siz); siz ++;} For (ll I = 0; I <n; I ++) in [I]. X = MP. find (in [I]. x)-> second; build (1, siz, 1); For (ll I = 0; I <n; I ++) {x = in [I]. x, Y = in [I]. y; If (in [I]. OP = 1) {myset. insert (node (Pos [X], y); updata (in [I]. x, 1, in [I]. y, 1);} else if (in [I]. OP = 2) {myset. erase (node (Pos [X], y); updata (in [I]. x, 1, in [I]. y, 0);} else {ll he = query (in [I]. X + 1, siz, 1, in [I]. y); If (He =-1) {puts ("-1"); continue;} printf ("% d \ n", POS [he], myset. lower_bound (node (Pos [he], in [I]. Y + 1)-> Y) ;}} return 0 ;}