HDU 5249 权值线段树

反向翻译:

After you work, KPIs are all you have. I developed a service and gained great popularity. Billions of requests are pushed to a large pipeline while serving pull requests from the pipe head. Let's define that each request has an important value. My KPI is calculated from the median of the important values requested in the current pipeline. Now giving you the service record, sometimes I want to know the important median value of the request in the current pipeline.

There are about 100 sets of data.

The first line of each group of data has an n (1≤n≤10000), which represents the number of service records.

Next there are n lines, each line has 3 forms
"in x": indicates that a request with an important value of x (0≤x≤109) is pushed into the pipeline.
"out": The request for the header of the pipeline was pulled on behalf of the service.
"query: It means that I want to know the median value of the important value of the request in the current pipeline. That is to say, if there are m requests in the current pipeline, I want to know that the floor (m / 2) + 1th request is important after the ascending order. value.

To keep things simple, all x's are different, and if there are no values in the pipeline, there will be no "out" and "query" operations.

For each set of data, output one line first

Case #i:
Then every time "query", output the middle value of the important value in the current pipeline


动态开点权值线段树模板题

再开个 queue 维护一下管道即可

// This code writed by chtholly_micromaker(MicroMaker)
#include <bits/stdc++.h>
#define reg register
using namespace std;
const int MaxN=1000050;
template <class t> inline void read(t &s)
{
    s=0;
    reg int f=1;
    reg char c=getchar();
    while(!isdigit(c))
    {
        if(c=='-')
            f=-1;
        c=getchar();
    }
    while(isdigit(c))
        s=(s<<3)+(s<<1)+(c^48),c=getchar();
    s*=f;
    return;
}
int val[MaxN],ls[MaxN],rs[MaxN],nd;
int n;
#define lson ls[u]
#define rson rs[u]
inline void Init()
{
    memset(val,0,sizeof val);nd=1;
    memset(ls,0,sizeof ls);
    memset(rs,0,sizeof rs);
    return;
}
inline void pushup(int u)
{
    val[u]=val[lson]+val[rson];
    return;
}
inline void change(int &u,int l,int r,int x,int k)
{
    if(!u)
        u=++nd;
    if(l==r)
    {
        val[u]+=k;
        return;
    }
    reg int mid=(l+r)>>1;
    if(x<=mid)
        change(lson,l,mid,x,k);
    else
        change(rson,mid+1,r,x,k);
    pushup(u);
    return;
}
inline int query(int u,int l,int r,int kth)
{
    if(l==r)
        return l;
    reg int mid=(l+r)>>1;
    if(kth<=val[lson])
        return query(lson,l,mid,kth);
    else
        return query(rson,mid+1,r,kth-val[lson]);
    return -1;
}
inline void work()
{
    Init();
    queue<int> Q;
    char s[11];
    reg int x;
    reg int m=0;
    int rt=1;
    for(int i=1;i<=n;++i)
    {
        scanf("%s",s);
        if(s[0]=='i')
        {
            scanf("%d",&x);++m;
            change(rt,0,1e9+1,x,1);
            Q.push(x);
        }
        else if(s[0]=='o')
        {
            --m;
            x=Q.front();Q.pop();
            change(rt,0,1e9+1,x,-1);
        }
        else
            printf("%d\n",query(rt,0,1e9+1,m/2+1));
    }
    return;
}
signed main(void)
{
    reg int kase=0;
    while(cin>>n)
    {
        printf("Case #%d:\n",++kase);
        work();
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/chinesepikaync/p/12441373.html