Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #define N 111111
- using namespace std;
- struct tree{
- int x,y,ls,rs;
- } t[N];
- int n,c,randarr[N],root;
- void merge(int &x,int left,int right){
- if (!left || !right){
- x=left?left:right;
- return;
- }
- if (t[left].y>t[right].y){
- x=left;
- merge(t[left].rs,t[left].rs,right);
- }else{
- x=right;
- merge(t[right].ls,left,t[right].ls);
- }
- }
- void split(int x,int key,int &left,int &right){
- if (!x){
- left=right=0;
- return;
- }
- if (t[x].x<=key){
- left=x;
- split(t[x].rs,key,t[x].rs,right);
- }else{
- right=x;
- split(t[x].ls,key,left,t[x].ls);
- }
- }
- void insert(int x){
- t[++c]=(tree){x,randarr[c],0,0};
- int l,r,m;
- split(root,x,l,r);
- merge(m,l,c);
- merge(root,m,r);
- }
- void erase(int x){
- int l,r,m,l1;
- split(root,x,l,r);
- split(l,x-1,l1,m);
- merge(root,l1,r);
- }
- void print(int x=root){
- if (!x) return;
- print(t[x].ls);
- cout<<t[x].x<<" ";
- print(t[x].rs);
- }
- void init(){
- cin>>n;
- for (int i=1;i<=n;i++) randarr[i]=i;
- random_shuffle(randarr+1,randarr+n+1);
- for (int i=1;i<=n;i++){
- int x;
- cin>>x;
- insert(x);
- }
- print();
- cin>>n;
- for (int i=1;i<=n;i++){
- int x;
- cin>>x;
- erase(x);
- print();
- }
- }
- int main(){
- init();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment