传送门

题目

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.

A 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 cin, cout streams (also you may use the %I64d specificator).

题目大意

N个操作,add x:向集合中添加x;del x:删除集合中的x;sum:将集合排序后,将集合中所有下标i % 5 = 3的元素累加求和。

分析

首先,我们不难想出最基础思路,即在线段树上记录5个值,分别表示模5余i的位置的和。但是我们知道如果插入一个数x则他后面的数的位置必然集体加一,如果删除一个数则他后面的数的位置必然减一。所以我们在每一次插入或删除之后将此点之后区间的所有线段树节点的5个值交换一下即可。在有了大体思路之后我们再来考虑如何实现交换节点这一操作:我们将所有数离线读入并离散化,在每一次操作用rd数组记录此点是后移还是前移,所以某个节点的余数为i的值即为它的的左儿子余数为i的值+它的右儿子余数为(i-左儿子之后点在原有位置上集体移动的位数)的值。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
long long xx[],kd[],d[][],rd[],b[];
map<long long,long long>id;
inline long long read(){
long long x=,f=;char s=getchar();
while(s<''||s>''){if(s=='-')f=-;s=getchar();}
while(s>=''&&s<=''){x=(x<<)+(x<<)+(s-'');s=getchar();}
return f*x;
}
inline void update(long long le,long long ri,long long pl,long long k,long long wh,long long sum){
if(le==ri){
d[][wh]+=k;
rd[wh]+=sum;
return;
}
long long mid=(le+ri)>>;
if(mid>=pl)update(le,mid,pl,k,wh<<,sum);
else update(mid+,ri,pl,k,wh<<|,sum);
rd[wh]=rd[wh<<]+rd[wh<<|];
for(long long i=;i<;i++)
d[i][wh]=d[i][wh<<]+d[(i-rd[wh<<]%+)%][wh<<|];
}
int main()
{ long long n,m,i,j,tot=,sum=;
n=read();
for(i=;i<=n;i++){
string s;
cin>>s;
if(s[]=='a'){
kd[i]=;
xx[i]=read();
b[++tot]=xx[i];
}else if(s[]=='d'){
kd[i]=;
xx[i]=read();
}else kd[i]=;
}
sort(b+,b+tot+);
for(i=;i<=tot;i++)
if(!id[b[i]]){
id[b[i]]=++sum;
}
for(i=;i<=n;i++){
if(kd[i]<){
update(,n,id[xx[i]],(kd[i]==?xx[i]:-xx[i]),,(kd[i]==?:-));
}else printf("%lld\n",d[][]);
}
return ;
}

85D Sum of Medians的更多相关文章

  1. Codeforces 85D Sum of Medians(线段树)

    题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...

  2. Codeforces 85D Sum of Medians

    传送门 D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  3. 数据结构(线段树):CodeForces 85D Sum of Medians

    D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  4. CodeForces 85D Sum of Medians Splay | 线段树

    Sum of Medians 题解: 对于这个题目,先想到是建立5棵Splay,然后每次更新把后面一段区间的树切下来,然后再转圈圈把切下来的树和别的树合并. 但是感觉写起来太麻烦就放弃了. 建立5棵线 ...

  5. CF 85D Sum of Medians (五颗线段树)

    http://codeforces.com/problemset/problem/85/D 题意: 给你N(0<N<1e5)次操作,每次操作有3种方式, 1.向集合里加一个数a(0< ...

  6. codeforces 85D D. Sum of Medians 线段树

    D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  7. codeforces 85D D. Sum of Medians Vector的妙用

    D. Sum of Medians Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/prob ...

  8. Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树

    题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of find ...

  9. Coderforces 85 D. Sum of Medians(线段树单点修改)

    D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. poj2263 zoj1952 Heavy Cargo(floyd||spfa)

    这道题数据范围小,方法比较多.我用floyd和spfa分别写了一下,spfa明显有时间优势. 一个小技巧在于:把城市名称对应到数字序号,处理是用数字. 方法一:spfa #include<ios ...

  2. bzoj 4299 Codechef FRBSUM

    定义一个集合的神秘数为不能表示成这个集合的某个子集和的最小正整数,给一个数列,多次求区间神秘数 $n \leq 100000$ sol: 考虑这个神秘数的性质,可以发现,如果神秘数是 $x$,那么 $ ...

  3. 每天一个linux命令(16):tail命令

    版权声明更新:2017-05-20博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...

  4. vector的简单运用(士兵队列训练问题)

    某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序 ...

  5. linkedLoop

    public class linkQueue <E>{ private class Node<E>{ E e; Node<E> next; public Node( ...

  6. xj监控端口,模拟登陆脚本

    #!/bin/bash date=`date +%Y%m%d-%H%M` count=0 ip1=124.117.246.195 ip2=124.117.246.194 port1=(443 80 6 ...

  7. Process使用

    最近在一个项目中,需要在C#中调用cmd窗口来执行一个命令,使用到了Process这个类,使用过程中遇到不少问题,好在终于解决了.赶紧记录下来. Process process = new Proce ...

  8. MQTT事件回调流程

    TLS 如下强调: 1.每个IOT设备应该有一对独有的公钥/私钥 2.SERVER的认证通过SERVER的"root certificate" SSL产生过程: $ openssl ...

  9. [转载]PCI/PCIe基础——配置空间

    转载地址:http://blog.csdn.net/jiangwei0512/article/details/51603525 PCI/PCIe设备有自己的独立地址空间,这部分空间会映射到整个系统的地 ...

  10. java代码排序问题

    总结: package com.ja; import java.util.Arrays; import java.util.Collections; public class mili { publi ...