ZOJ-3612-树状数组+第k小数+离散化

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 10009;
const double esp = 1e-6;

double bef[maxn];
double aft[maxn];
char str[maxn][10];
int C[maxn];
int num, nn;

int lowbit(int x)
{
    return x&(-x);
}

int find(double val)
{
    int l = 1, r = num, mid;
    while(1)
    {
        mid = (l+r)/2;
        if(aft[mid]==val) return mid;
        else if(aft[mid]<val) l = mid+1;
        else r = mid-1;
    }
}

void updata(int pos, int tmp)
{
    for(; pos<=num; pos+=lowbit(pos))
        C[pos] += tmp;
}

int query(int pos)
{
    int a = 0;
    for(; pos>0; pos-=lowbit(pos)) a+=C[pos];
    return a;
}

double ans(int x)
{
    int l=1, r=num, mid;
    while(l<r)
    {
        mid = (l+r)/2;
        int tmp = query(mid);
        //if(tmp == x) {cout<<"  mid ++ "<<mid<<endl; return aft[mid];}
        if(tmp<x) l = mid+1;
        else r = mid;
    }
    //cout<<"  l ++ "<<l<<endl;
    return aft[l];
}
void print(double key)
{
    char s[20];
    sprintf(s,"%.6lf",key);
    int len=strlen(s)-1;
    while(s[len]=='0')
    {
        len--;
    }
    if(s[len]=='.')
    {
        s[len]='\0';
    }
    else
    s[len+1]='\0';
    puts(s);
}
void add(double val)
{
    int t = find(val);
    nn++;
    updata(t, 1);
    if(nn&1)
    {
        print(ans(nn/2+1));
    }
    else
    {
        print(( ans(nn/2) + ans(nn/2+1) )/2);
    }
}

void remove(double val)
{
    int t = find(val);
    if( query(t)==query(t-1) )
    {
        puts("Wrong!");
        return;
    }
    nn--;
    updata(t, -1);
    if(nn==0)
    {
        puts("Empty!");
        return;
    }
    else if(nn&1)
    {
        print(ans(nn/2+1));
    }
    else
    {
       print(( ans(nn/2) + ans(nn/2+1) )/2);
    }
}

int main()
{
    int t, n;
    scanf("%d", &t);
    double aa;
    while(cin>>aa)
     cout<<aa<<endl;
    while(t--)
    {
        memset(C, 0, sizeof(C));
        scanf("%d", &n);
        for(int i=1; i<=n; i++)
        {
            scanf("%s%lf", str[i], &bef[i]);
            aft[i] = bef[i];
        }
        num = 1;
        sort(aft+1, aft+n+1);
        for(int i=1; i<=n; i++)
        if(aft[i]!=aft[i-1])
            aft[num++] = aft[i];

        nn = 0;
        for(int i=1; i<=n; i++)
        {
            if(str[i][0]=='a') add(bef[i]);
            else remove(bef[i]);
        }
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值