85D Sum of Medians
题目
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).
题目大意
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的更多相关文章
- 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
传送门 D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- 数据结构(线段树):CodeForces 85D Sum of Medians
D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
- CodeForces 85D Sum of Medians Splay | 线段树
Sum of Medians 题解: 对于这个题目,先想到是建立5棵Splay,然后每次更新把后面一段区间的树切下来,然后再转圈圈把切下来的树和别的树合并. 但是感觉写起来太麻烦就放弃了. 建立5棵线 ...
- CF 85D Sum of Medians (五颗线段树)
http://codeforces.com/problemset/problem/85/D 题意: 给你N(0<N<1e5)次操作,每次操作有3种方式, 1.向集合里加一个数a(0< ...
- 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 ...
- codeforces 85D D. Sum of Medians Vector的妙用
D. Sum of Medians Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/prob ...
- Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树
题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of find ...
- 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 ...
随机推荐
- hdoj-1032-The 3n + 1 problem(坑题)
题目链接 //巨坑的一道题,输入的m,n要判断大小,输出还要按照原来的顺序,范围还是i<=n<=j #include <iostream> #include <cstd ...
- signal 信号具体含义解释~
) SIGHUP 本信号在用户终端连接(正常或非正常)结束时发出,通常是在终端的控 制进程结束时, 通知同一session内的各个作业,这时它们与控制终端不再关联. ) SIGINT 程序终止(int ...
- Mayor's posters (线段树加离散化)
个人心得:线段树也有了一定的掌握,线段树对于区间问题的高效性还是挺好的,不过当区间过大时就需要离散化了,一直不了解离散化是什么鬼,后面去看了下 离散化,把无限空间中有限的个体映射到有限的空间中去,以此 ...
- Android Studio导入项目,报错 Error:Unsupported method: BaseConfig.getApplicationIdSuffix().
从GitHub上clone下来的第三方库,由于时间间隔很长,gradle的版本和本机的版本不一致,导入到Android Studio中会报错,错误信息如下: Error:Unsupported met ...
- mysql之 double write 浅析
http://blog.itpub.net/22664653/viewspace-1140915/ 介绍double write之前我们有必要了解partial page write 问题 : ...
- 四、Jmeter--参数化
一.CSV 参数化 1.我们做性能测试需要并发多个用户,为了真实模拟用户行为,我们需要模拟多个不同的用户登录,这是我们就需要进行参数化.这里我们选择比较常用的参数化方法-CSV Data Set Co ...
- 1、Monkey入门准备教程
1.前提需要Android环境 ADT:链接: https://pan.baidu.com/s/1QN6EJh46cJGvUBaMZjtiWw 密码: a7zu Eclipse:https://www ...
- Visualforce入门第三篇_2017.3.2
Visualforce实现显示Record List(列表) 详细见链接:https://trailhead.salesforce.com/modules/visualforce_fundamenta ...
- WPF中ItemsControl绑定到Google ProtocolBuffer的结构体时的性能问题
背景: 最近遇到一个DataGrid的性能问题:里面大概有4000个数据, 绑定的ItemSource的类也只有一层数据,即简单的List(里面每个是Protocol Buffer自动产生的一个类,1 ...
- Redis事务和watch
redis的事务 严格意义来讲,redis的事务和我们理解的传统数据库(如mysql)的事务是不一样的. redis中的事务定义 Redis中的事务(transaction)是一组命令的集合. 事务同 ...