D. 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.

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).

Examples
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   这道题目不难,注意去重,还要防止爆int。
 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int hsh[maxn],tot,Q;
long long ans[maxn<<][];
int sum[maxn<<],tp[maxn],num[maxn]; void Push_up(int x){
int l=x<<,r=x<<|;
sum[x]=sum[l]+sum[r];
for(int i=;i<=;i++)
ans[x][i]=ans[l][i]+ans[r][((i-sum[l])%+)%];
} void Insert(int x,int l,int r,int g,int d){
if(l==r){
ans[x][]+=hsh[l]*d;
sum[x]+=d;
return;
}
int mid=(l+r)>>;
if(mid>=g)Insert(x<<,l,mid,g,d);
else Insert(x<<|,mid+,r,g,d);
Push_up(x);
} char op[];
int main(){
scanf("%d",&Q);
for(int q=;q<=Q;q++){
scanf("%s",op);
if(op[]=='a')tp[q]=;
else if(op[]=='d')tp[q]=-;
else continue;
scanf("%d",&num[q]);
if(tp[q]==){++tot;hsh[tot]=num[q];}
} sort(hsh+,hsh+tot+);
tot=unique(hsh+,hsh+tot+)-hsh-; for(int q=;q<=Q;q++){
if(tp[q]==){
int p=lower_bound(hsh+,hsh+tot+,num[q])-hsh;
Insert(,,tot,p,);
}
else if(tp[q]==-){
int p=lower_bound(hsh+,hsh+tot+,num[q])-hsh;
Insert(,,tot,p,-);
}
else
printf("%I64d\n",ans[][]);
}
return ;
}

数据结构(线段树):CodeForces 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 Splay | 线段树

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

  3. Codeforces 85D Sum of Medians

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

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

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

  5. 算法手记 之 数据结构(线段树详解)(POJ 3468)

    依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...

  6. 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations

    题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...

  7. 85D Sum of Medians

    传送门 题目 In one well-known algorithm of finding the k-th order statistics we should divide all element ...

  8. ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)

    这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...

  9. set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

    题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...

随机推荐

  1. LDAP7卸载

    3 Uninstalling Directory Server Enterprise Edition This chapter provides instructions for uninstalli ...

  2. Creating a web application.

    About creating web GIS applications As you learn and use ArcGIS for Server, you'll probably reach th ...

  3. copssh加bitvise

    只是简单记录下自己在成功使用的方案: 目的:为了突破公司对网站和qq的限制 具备的条件:一台云服务器.Copssh_4.1.0.bitvise ssh client 4.62.公司电脑客户端 一.首先 ...

  4. 安装SQL Server2008时 检测时有“重启计算机”失败

    第一种解决方案: 在学校的时候 遇到这种问题的解决办法是: 卸载VS,先安装SQL Server 2008 再安装VS 就行了: 第二种解决方案: 如果已经安装过VS,在安装SQL Server200 ...

  5. SQL某个字段在原内容上增加固定内容或replace查找替换内容

    今天正好遇到一个SQL小问题,特做备注 在原有的表中数据如pic 在不动原内容的基础上增加../路径,但不能修改原数据值 原数据 SQL: pic字段 需要增加'../'的内容 update Bmps ...

  6. [转]Delphi I/O Errors

    The following are the Windows API (and former DOS) IO errors, which are also the IO errors often ret ...

  7. TCP/UDP基本概念部分

    最近在读<Unix网络编程>和<TCP/IP详解>两本书,有了一些自己的心得与体会,总结下其中典型的问题. 1. 为什么建立连接需要三次握手? 谢希仁的<计算机网络> ...

  8. Jquery实现图片左右滚动(自动)

    <!DOCTYPE HTML><html><head><title>基于jQuery的控制左右滚动效果_自动滚动版本</title>< ...

  9. Poj 3667

    这是第一题线段树的区间合并的题: 这类的题用于求连续的最长长度什么的: 这题我看的是一篇比较不错的博客: 我把我的理解注释在代码里了: #include <iostream>#includ ...

  10. HTML5画布