数据结构(线段树):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中 查找左右相 ...
随机推荐
- 用GitHub Pages免费空间搭建Blog
前言 其实之前就知道可以用GitHub Pages搭建静态博客,不过之前一直忙着爬手册撸代码==,昨天终于把前端各种手册里的入门教程撸的差不多了(CSS布局撸的我要吐了好嘛),于是把代码什么的放一 ...
- Simple screenshot that explains the non-static invocation.
Here is the code: /* Instance invocation in the memory: */ package kju.obj; import static kju.print. ...
- 在iframe中获取iframe外的对象
parent.document.getElementById("dom ID"); $($(parent.document.getElementById("video-i ...
- 在C语言中使用scanf语句时遇到的问题总结
在使用visual studio2013编写c语言代码时,遇到了这样的几个小问题,进行如下的总结. 1, 关于使用scanf语句报错的解决方案1 #include <stdio.h> in ...
- xcode中如何安装多个版本的模拟器
在xcode里面,安装的时间默认自带的有模拟器,有时间为了调试需要使用个多个版本的模拟器 在xcode -> preference 里面 选择download,这里你可下载你需要的模拟器
- QT QSettings 操作(导入导出、保存获取信息)*.ini文件详解
1.QSettings基本使用 1.1.生成.ini文件,来点实用的代码吧. QString fileName;fileName = QCoreApplication::applicationDirP ...
- 求fibonacci数列 java
java 和 c 差不多.但是java可以根据需求定义数组. 我还不会java的函数调用,所以用数组的方法. import java.util.Scanner; public class fibon{ ...
- Win异常: 除了chrome浏览器外,所有安装的软件都连不上网
经查找资料,是LSP被篡改,恢复后使用正常. 百度百科 LSP: Layered Service Provider, 即分层服务提程序,Winsock 作为应用程序的 Windows 的网络套接字工 ...
- 转载-Linux下svn搭建配置流程
Linux下svn搭建配置流程 一. 源文件编译安装.源文件共两个,为: 1. 下载subversion源文件 subversion-1.6.1.tar.gz http://d136 ...
- centos 7 samba相关命令
1.安装相关包 yum install samba samba-client samba-common 2.启动smb的命令 systemctl enable smb.service systemct ...