These days exams in the heart rather quiet, so I came to learn the weight of the line segment tree, this is the first question I played ~
Test instructions
There are three ways to do this:
1:in a put a on the team.
2:out Remove the element that was first put in
3:query median of elements in the output team
#多组数据
#中位数: N Number of ordinal number (n/2+1)
Ideas:
This topic online Many God Ben said is the use of set, then here mainly introduces the weighted value segment tree solution.
First of all, to find out the difference between the weight segment tree and the Ordinary line segment tree: The weight segment tree is based on the element weights (sometimes discretization), and the normal segment tree is divided according to the element number, so it is
very easy
to find the median number in the tree of weight line. So here's the question: how to maintain the median of a dynamic interval.
1: We take note of all operations pre[1~n], by the way save all the elements (good order) now[1~sum];
#sum: In the number
2: Then, build tree[1~maxn<<2]
#tree [i] is tree[i] The number of elements in this interval is
2: Then, in the order of operation: in
(Pre[i] >=0): Find the inserted number (Pre[i]) in the position of the tree (lower_bound) tmp;
TMP position +1 on the tree, indicating a number of TMP positions;
push_up;
Out (Pre[i]==-1): Find the position of the team Head (Q.front ()) on the tree tmp;
The TMP position on the tree-1, indicates that the number on the TMP location is gone;
push_up;
Q.pop ();
Query (pre[i]==-2): Look for elements in the middle of the team (Q.size ()/2+1) position on the tree, ZZ;
Output NOW[ZZ];
Well, the nonsense is not much to say, next attach the code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include < algorithm> #include <queue> #define for (AA,BB,CC) for (int aa=bb;aa<=cc;++aa) #define LS node<<1 #
Define RS node<<1|1 #define MAXN 100005 using namespace std;
int now[maxn],pre[maxn],tree[maxn<<2];
int case,n;
void push_up (int node) {Tree[node]=tree[ls]+tree[rs];}
void build (int node,int l,int R) {if (l==r) {tree[node]=0;
return;
} int mid= (L+R) >>1;
Build (Ls,l,mid);
Build (Rs,mid+1,r);
PUSH_UP (node);
} void Updata (int node,int l,int r,int tmp,int flag) {if (l==r) {tree[node]+=flag;
Return
} int mid= (L+R) >>1;
if (tmp<=mid) Updata (Ls,l,mid,tmp,flag);
else Updata (Rs,mid+1,r,tmp,flag);
PUSH_UP (node);
} int query (int node,int l,int r,int tmp) {if (l==r) return l;
int mid= (L+R) >>1;
if (tree[ls]>=tmp) return query (LS,L,MID,TMP); else REturn query (Rs,mid+1,r,tmp-tree[ls]);
} void Work () {++case;
printf ("Case #%d:\n", case);
Queue <int> q;
for (i,1,n) {char s[10];
scanf ("%s", &s);
if (s[0]== ' I ') scanf ("%d", &pre[i]);
else if (s[0]== ' o ') pre[i]=-1;
else pre[i]=-2;
} int sum=0;
for (I,1,n) {if (pre[i]<0) continue;
Now[++sum]=pre[i];
} sort (now+1,now+sum+1);
Build (1,1,sum);
for (I,1,n) {if (pre[i]>=0) {int tmp=lower_bound (now+1,now+sum+1,pre[i])-now;
Updata (1,1,sum,tmp,1);
Q.push (Pre[i]);
} else if (pre[i]==-1) {int Ret=q.front ();
int Tmp=lower_bound (Now+1,now+sum+1,ret)-now;
Updata (1,1,sum,tmp,-1);
Q.pop ();
} else{int Zz=query (1,1,sum,q.size ()/2+1);
printf ("%d\n", Now[zz]);
}}} int main () {while (~SCANF ("%d", &n));
return 0; }
If you want a more intuitive understanding of the weight segment tree, see this (weight segment tree for reverse order) ":
http://blog.csdn.net/Monkey_king2017cn/article/details/60323601