数据结构(线段树):CodeForces 85D Sum of Medians
3 seconds
256 megabytes
standard input
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.
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.
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).
6
add 4
add 5
add 1
add 2
add 3
sum
3
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
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的更多相关文章
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
- CodeForces 85D Sum of Medians Splay | 线段树
Sum of Medians 题解: 对于这个题目,先想到是建立5棵Splay,然后每次更新把后面一段区间的树切下来,然后再转圈圈把切下来的树和别的树合并. 但是感觉写起来太麻烦就放弃了. 建立5棵线 ...
- Codeforces 85D Sum of Medians
传送门 D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- CF 85D Sum of Medians (五颗线段树)
http://codeforces.com/problemset/problem/85/D 题意: 给你N(0<N<1e5)次操作,每次操作有3种方式, 1.向集合里加一个数a(0< ...
- 算法手记 之 数据结构(线段树详解)(POJ 3468)
依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...
- 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...
- 85D Sum of Medians
传送门 题目 In one well-known algorithm of finding the k-th order statistics we should divide all element ...
- ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)
这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
随机推荐
- node 搭建开发框架express
参考地址: http://www.itnose.net/detail/6095003.html 开发环境 E:\project> node -v v0.10.30 E:\project> ...
- JAVA JDK 1.6 API中文版.CHM打开chm提示,“ 已取消到该网页的导航”
JAVA JDK 1.6 API中文版.CHM打开chm提示,“ 已取消到该网页的导航” silent fish 装了win7后,打开chm文件,发现很多在xp系统打开正常的chm文件竟然出现问题, ...
- ajax xmlhttp下open方法POST、GET参数的区别
1. get是从服务器上获取数据(会暴露客户端ip),post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看 ...
- 由App的启动说起(转)
The two most important days in your life are the day you are born and the day you find out why. -- M ...
- 不用Google Adsense的84个赚钱方法
这是一个关于网络广告商和网络销售的汇总列表,可以用来为您的网站或博客赚点钱.广告商都是英文的,加入广告请确认其是否支持中国地区支持,不支持的话就不必加入了. Chitika : 购物中心旗帜广告. ( ...
- 【BZOJ1050】【枚举+并查集】旅行comf
Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...
- fish code
<embed width="272" height="180" type="application/x-shockwave-flash" ...
- 控制寄存器 CR*
控制寄存器(CR0-CR3)用于控制和确定处理器的操作模式以及当前执行任务的特性,如图4-3所示.CR0中含有控制处理器操作模式和状态的系统控制标志:CR1保留不用:CR2含有导致页错误的线性地址:C ...
- 通过C# 打开一个应用程序
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo(); //设置外部程序名 Info ...
- 织梦dedecms后台发布文章不自动更新首页与栏目列表页
dedecms发文章不自动更新首页也列表页解决办法如下: 登陆dedecms后台,找到“系统”“系统基本参数”“性能选项”,把“arclist标签调用缓存”设置成0,然后把“发布文章后马上更新网站主页 ...