CF-85D-Sum of Medians(线段树)

本文介绍了一种高效算法来计算数组中任意k个元素的中位数之和,通过将数组划分为五元组并找到每个五元组的中位数,然后计算这些中位数的总和。该方法特别适用于现代图形卡的加速应用。此外,文章提供了一个输入输出示例,并解释了如何快速实现这个算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance speed on a modern video card, you should be able to find a sum of medians in each five of the array.

sum of medians of a sorted k-element set S = {a1, a2, ..., ak}, where a1 < a2 < a3 < ... < ak, will be understood by as

The  operator stands for taking the remainder, that is  stands for the remainder of dividing x by y.

To organize exercise testing quickly calculating the sum of medians for a changing set was needed.

Input

The first line contains number n (1 ≤ n ≤ 105), the number of operations performed.

Then each of n lines contains the description of one of the three operations:

  • add x — add the element x to the set;
  • del x — delete the element x from the set;
  • sum — find the sum of medians of the set.

For any add x operation it is true that the element x is not included in the set directly before the operation.

For any del x operation it is true that the element x is included in the set directly before the operation.

All the numbers in the input are positive integers, not exceeding 109.

Output

For each operation sum print on the single line the sum of medians of the current set. If the set is empty, print 0.

Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cincout streams (also you may use the %I64d specificator).

Sample test(s)
input
6
add 4
add 5
add 1
add 2
add 3
sum
output
3
input
14
add 1
add 7
add 2
add 5
sum
add 6
add 8
add 9
add 3
add 4
add 10
sum
del 1
sum
output
5
11
13

思路:跟这题完全一样。点击打开链接  数据比HDU的强。


#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;

struct S{
char op[5];
int x,id;
}e[100005];

bool cmpval(S a,S b)
{
    if(a.x==b.x)  return a.op[0]<b.op[0];

    return a.x<b.x;
}

bool cmpid(S a,S b)
{
    return a.id<b.id;
}

int n,cnt,val[100005],num[400005];
long long sum[400005][5];

void build(int idx,int s,int e)
{
    if(s!=e)
    {
        int mid=(s+e)>>1;

        build(idx<<1,s,mid);
        build(idx<<1|1,mid+1,e);
    }

    num[idx]=0;

    for(int i=0;i<5;i++) sum[idx][i]=0;
}

void update(int idx,int s,int e,int pos,int flag)
{
    num[idx]+=flag;

    if(s==e)
    {
        sum[idx][1]+=val[s]*flag;

        return;
    }

    int mid=(s+e)>>1;

    if(pos<=mid) update(idx<<1,s,mid,pos,flag);
    else update(idx<<1|1,mid+1,e,pos,flag);

    for(int i=0;i<5;i++) sum[idx][i]=sum[idx<<1][i]+sum[idx<<1|1][i-num[idx<<1]%5>=0?i-num[idx<<1]%5:i-num[idx<<1]%5+5];//更新对应区间内下标为模5等于i数的和
}

long long query(int idx,int s,int e,int mod)
{
    return sum[idx<<1][mod]+sum[idx<<1|1][mod-num[idx<<1]%5>=0?mod-num[idx<<1]%5:mod-num[idx<<1]%5+5];
}

int main()
{
    int i;
    long long ans;

    while(~scanf("%d",&n))
    {
        map<int,int>mp;//用于离散化过程中判重,有可能存在先add a,再del a,再 add a的情况

        for(i=0;i<n;i++)
        {
            scanf("%s",e[i].op);

            if(e[i].op[0]=='s') e[i].x=0;
            else scanf("%d",&e[i].x);

            e[i].id=i;
        }

        sort(e,e+n,cmpval);

        cnt=1;

        for(i=0;i<n;i++)//离散化
        {
            if(e[i].op[0]=='a')
            {
                if(!mp[e[i].x]) mp[e[i].x]=cnt++;

                val[mp[e[i].x]]=e[i].x;
                e[i].x=mp[e[i].x];

            }
            else if(e[i].op[0]=='d')
            {
                e[i].x=mp[e[i].x];
            }
        }

        sort(e,e+n,cmpid);

        cnt--;

        if(!cnt)//特判
        {
            for(i=0;i<n;i++)
            {
                if(e[i].op[0]=='s')
                {
                    printf("0\n");
                }
            }

            continue;
        }

        build(1,1,cnt);

        for(i=0;i<n;i++)
        {
            if(e[i].op[0]=='a') update(1,1,cnt,e[i].x,1);
            else if(e[i].op[0]=='d') update(1,1,cnt,e[i].x,-1);
            else printf("%I64d\n",query(1,1,n,3));
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值