CodeForces 85D Sum of Medians(线段树 + 离散化)

Sum of Medians
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

题意:n次操作,有三种操作:加入一个集合中没有的数,删除一个集合中已有的数,求集合中位置模5余3的数之和(集合自动从小到大排序)。

因为操作最多是1e5次,但是数的范围最大到1e9,所以要采用离散化。线段树中每个结点有一个长度为5的数组,来记录模5的余数,还有一个m数组,若在第i个位置插入或删除一个数,则该位置后面的每个结点的m都要加1或者是减1,来记录该结点位置模5后余数的变化。

这道题敲了很久,离散化的时候就把自己绕晕了,之后线段树的pushup函数也让我绕了好一会儿才绕清楚。敲这个题的时候一定要思路清晰呀。。

#include <cstdio>
#include <iostream>
#include <algorithm>
#define maxn 100005
#define ls node <<1
#define rs node << 1 | 1
#define lson l, mid, ls
#define rson mid + 1, r, rs

using namespace std;

int n, cnt, val[maxn], m[maxn << 2];
char str[5];
long long sum[maxn <<2][5];

struct Operation
{
    int o;
    int v;
} op[maxn];

struct add_and_delete
{
    int id;
    int v;
    int r;
} o[maxn];

bool cmp(add_and_delete x, add_and_delete y)
{
    return x.v < y.v;
}

void pushup(int l, int r, int node)
{
    long long t = 0;
    if(l == r)
    {
        for(int i = 0; i < 5; i++)
            if(sum[node][i])
                t = sum[node][i];
        for(int i = 0; i < 5; i++)
            sum[node][i] = 0;
        sum[node][m[node] % 5] = t;;
        return;
    }
    for(int i = 0; i < 5; i++)
        sum[node][(i + m[node]) % 5] = sum[ls][i] + sum[rs][i];
}

void build(int l, int r, int node)
{
    m[node] = 0;
    for(int i = 0; i < 5; i++)
        sum[node][i] = 0;
    if(l == r)
        return;
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
}

void update(int x, int y, int v, int l, int r, int node)
{
    if(x <= l && y >= r)
    {
        m[node] += v;
        pushup(l, r, node);
        return;
    }
    int mid = (l + r) >>1;
    if(x <= mid)
        update(x, y, v, lson);
    if(y > mid)
        update(x, y, v, rson);
    pushup(l, r, node);
}

void recover(int x, int v, int l, int r, int node)
{
    if(l == r)
    {
        for(int i = 0; i < 5; i++)
            sum[node][i] = 0;
        if(v == 1)
            sum[node][m[node] % 5] = val[l];
        return;
    }
    int mid = (l + r) >>1;
    if(x <= mid)
        recover(x, v, lson);
    if(x > mid)
        recover(x, v, rson);
    pushup(l, r, node);
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        cnt = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%s", str);
            if(str[0] == 's')
            {
                op[i].o = 0;
            }
            else if(str[0] == 'a')
            {
                op[i].o = 1;
                scanf("%d", &op[i].v);
                o[cnt].id = i;
                o[cnt++].v = op[i].v;
            }
            else if(str[0] == 'd')
            {
                op[i].o = -1;
                scanf("%d", &op[i].v);
                o[cnt].id = i;
                o[cnt++].v = op[i].v;
            }
        }
        sort(o, o + cnt, cmp);
        o[0].r = 0;
        val[0] = o[0].v;
        for(int  i = 1; i < cnt; i++)
        {
            o[i].r = o[i - 1].r;
            if(o[i].v != o[i - 1].v)
            {
                o[i].r ++;
                val[o[i].r] = o[i].v;
            }
        }
        for(int i = 0; i < cnt; i++)
            op[o[i].id].v = o[i].r;
        build(0, o[cnt - 1].r, 1);
        for(int i = 0; i < n; i++)
        {
            if(op[i].o == 0)
                printf("%lld\n", sum[1][3]);
            else
            {
                update(op[i].v, o[cnt - 1].r, op[i].o, 0, o[cnt - 1].r, 1);
                recover(op[i].v, op[i].o, 0, o[cnt - 1].r, 1);
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值