hdu5249-- weights segment tree

Weight tree line, based on a weight of the subject under tree line, as unlike ordinary tree line.
But for large data, we need to solve the problem on discrete space.
Weight segment tree so that is a bit like the feeling of the Chairman of the tree. The space is also better than the President of the trees. But it does nothing to help the sub-intervals. It is quite easy to use for a query on the entire range of some dynamic.
Like hdu5249 median is dynamic, so it can be handled.

Attach Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define For(aa,bb,cc) for(int aa=bb;aa<=cc;++aa)
#define Set(aa,bb) memset(aa,bb,sizeof(aa))
#define ls node<<1
#define rs node<<1|1
using namespace std;
const int maxn=10010;
int pre[maxn],now[maxn],tree[maxn<<2];
int n,Case;

void pushup(int node){
    tree[node]=tree[ls]+tree[rs];
}

void create_tree(int node,int l,int r){
    if(l==r){
        tree[node]=0;
        return ;
    }
    int mid=(l+r)>>1;
    create_tree(ls,l,mid);
    create_tree(rs,mid+1,r);
    pushup(node);
    return ;
}

void update(int node,int l,int r,int w,int flag){
    if(l==r){
        tree[node]+=flag;
        return ; 
    }
    int mid=(l+r)>>1;
    if(w<=mid) update(ls,l,mid,w,flag);
    else update(rs,mid+1,r,w,flag);
    pushup(node);
}

int query(int node,int l,int r,int flag){
    if(l==r) return l;
    int mid=(l+r)>>1;
    if(flag<=tree[ls]) return query(ls,l,mid,flag);
    else return query(rs,mid+1,r,flag-tree[ls]);
}

void work(){
    queue<int>q;
    ++Case;
    printf("Case #%d:\n",Case);
    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) now[++sum]=pre[i];
    }
    sort(now+1,now+1+sum);
    sum=unique(now+1,now+sum+1)-(now+1);
    create_tree(1,1,sum);
    For(i,1,n){
        if(pre[i]>=0){
            int tmp=lower_bound(now+1,now+1+sum,pre[i])-now;
            update(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+1+sum,ret)-now;
            update(1,1,sum,tmp,-1);
            q.pop();
        }
        else{
            int md=query(1,1,sum,(q.size()/2)+1);
            printf("%d\n",now[md]);
        }
    }
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    while(~scanf("%d",&n)) work();
    return 0;
}
Published 51 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_35776579/article/details/55004134