If the range of numbers is small
1 Data range: O (n) 2 I:log (n) (number of tree array queries less than or equal to i) *log (n) (binary found i) 3 added: Log (n) (tree array) 4 Delete: Log (n) (tree array)
Group Programming Ladder Race l3-002. Stack
1 /*data range: O (n)2 I:log (N) (number of tree array queries less than or equal to i) *log (n) (two points found i)3 Add: Log (n) (tree-like array)4 Delete: Log (n) (tree-like array)5 */ 6#include <cstdio>7#include <cstdlib>8#include <cmath>9#include <cstring>Ten#include <Set> One#include <vector> A#include <stack> -#include <map> -#include <queue> the#include <algorithm> -#include <iostream> - using namespacestd; - #defineMAXN 100000 + - Longa[100005]; + Longst[100005]; A at intMain () - { - LongN,d,l,r,mid,i,g,c=0; - Chars[ -]; - for(i=0; i<=maxn;i++) -a[i]=0; inscanf"%ld",&n); - while(n) to { +n--; -scanf"%s", s); the if(strcmp (s),"Pop")==0) * { $ if(c==0)Panax Notoginseng { -printf"invalid\n"); the Continue; + } AD=St[c]; theprintf"%ld\n", d); + while(d<=MAXN) - { $a[d]--; $d+= (D & (-d)); - } -c--; the } - Else if(strcmp (s),"Push")==0)Wuyi { thescanf"%ld",&d); -C++; Wust[c]=D; - while(d<=MAXN) About { $a[d]++; -d+= (D & (-d)); - } - } A Else + { theD= (c+1) >>1; - if(c==0) $ { theprintf"invalid\n"); the Continue; the } theL=1; R=MAXN; - while(l<=R) in { theMid= (l+r) >>1; theg=0; AboutI=mid; the while(i>=1) the { theg+=A[i]; +i-= (I & (-i)); - } the if(g>=d)Bayir=mid-1; the Else theL=mid+1; - } -printf"%ld\n", L); the } the } the return 0; the}
Tree array + binary answer query K-Large number (group programming ladder l3-002. Stacks)