CF 85D Sum of Medians (五颗线段树)
http://codeforces.com/problemset/problem/85/D
题意:
给你N(0<N<1e5)次操作,每次操作有3种方式,
1.向集合里加一个数a(0<a<1e9)(增加数a之前的集合一定不存在a)
2.向集合里删一个数a(0<a<1e9)(删除数a之前的集合一定存在a)
3.将下标i%5==3的数相加,计算总和并相加.
分析:
先离散,然后线段树,sum[i]用来标识前i项有几个数,ans[rt][i]表示以rt为根的子树中第i大数的和
// File Name: d.cpp
// Author: Zlbing
// Created Time: 2013/7/18 20:22:40 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--) #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int MAXN=1e5+;
char str[MAXN][];
int d[MAXN];
int hash[MAXN];
int tot; int sum[MAXN<<];
LL ans[MAXN<<][];
int flag; void pushup(int rt)
{
for(int i=;i<;i++)
{
ans[rt][i]=ans[rt<<][i]+ans[rt<<|][(i-sum[rt<<]%+)%];
}
}
void update(int pos,int l,int r,int rt)
{
sum[rt]+=flag*-;
if(l==r)
{
ans[rt][]=hash[pos]*flag;
return;
}
int m=(l+r)>>;
if(pos<=m)update(pos,lson);
else update(pos,rson);
pushup(rt);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
tot=;
REP(i,,n)
{
scanf("%s",str[i]);
if(str[i][]=='a'||str[i][]=='d')
{
scanf("%d",&d[i]);
hash[tot++]=d[i];
}
}
sort(hash,hash+tot);
tot=unique(hash,hash+tot)-hash;
CL(ans,);
CL(sum,);
REP(i,,n)
{
if(str[i][]=='a')
{
flag=;
int pos=lower_bound(hash,hash+tot,d[i])-hash;
update(pos,,tot,);
}
else if(str[i][]=='d')
{
flag=;
int pos=lower_bound(hash,hash+tot,d[i])-hash;
update(pos,,tot,);
}
else {
printf("%I64d\n",ans[][]);
}
}
}
return ;
}
CF 85D Sum of Medians (五颗线段树)的更多相关文章
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
- CF240F (26颗线段树计数)
题目链接:Topcoder----洛谷 题目大意: 给定一个长为n的由a到z组成的字符串,有m次操作,每次操作将[l,r]这些位置的字符进行重排,得到字典序最小的回文字符串,如果无法操作就不进行. 思 ...
- ACM-ICPC 2018 徐州赛区网络预赛 G Trace(逆向,两颗线段树写法)
https://nanti.jisuanke.com/t/31459 思路 凡是后面的轨迹对前面的轨迹有影响的,可以尝试从后往前扫 区间修改需要push_down,单点更新所以不需要push_up(用 ...
- HDU 4267 A Simple Problem with Integers(2012年长春网络赛A 多颗线段树+单点查询)
以前似乎做过类似的不过当时完全不会.现在看到就有点思路了,开始还有洋洋得意得觉得自己有不小的进步了,结果思路错了...改了很久后测试数据过了还果断爆空间... 给你一串数字A,然后是两种操作: &qu ...
- 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 standard i ...
- Codeforces 85D Sum of Medians
传送门 D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- 85D Sum of Medians
传送门 题目 In one well-known algorithm of finding the k-th order statistics we should divide all element ...
- CF 552(div 3) E Two Teams 线段树,模拟链表
题目链接:http://codeforces.com/contest/1154/problem/E 题意:两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...
随机推荐
- [转] Mac OX上安装MongoDb
https://scotch.io/tutorials/an-introduction-to-mongodb MongoDB的安装有好多种安装方法,有普通青年的HomeBrew方式,也有文艺青年的源码 ...
- 利用 Lua 实现 App 动态化方案
因为动态化的东西我第一次看实现方案的源码,而且目前还是大三的学生,缺少很多实践经验说错的地方还请原谅,也希望能指出,被告知.想了很久还是决定写出来,求大神勿喷. 并且我的一个朋友bestswifter ...
- Java——(八)Map之LinkedHashMap、TreeMap、EnumMap实现类
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 1.LinkedHashMap实现类 LinkedHashMap需要维护意识的插入顺序,因此性能 ...
- 《3D数学基础:图形与游戏开发》勘误
最近读这本书,读到四元素的乘法时,自己推导了一下公式,然后懵了,多方查阅,确定是书籍中的笔误(我读的是中文翻译版): 具体在Page147: 上图所示,在第二处明显与第一处的公式不一样. 在维基百科上 ...
- 学习java随笔第二篇:java开发工具——Eclipse
java开发工具有很多这里我使用的是Eclipse. 首先我在官网上下载了Eclipse的软件包,下载地址:http://www.eclipse.org/downloads/,然后有在网上找了一个汉化 ...
- Linux 自动更新时间
1. 从NTP上把时间同步到本地 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 2. 更新本地时间 ntpdate us.pool.ntp.o ...
- ssh配置事务
http://blog.csdn.net/jianxin1009/article/details/9202907(不错)
- C# div、css
目录: 1.Div+Css布局教程(-)CSS必备知识 注:本教程要求对html和css有基础了解. 一.CSS布局属性 Width:设置对象的宽度(width:45px). Height:设置对象的 ...
- AngularJS+NodeJS环境搭建
需要安装的软件: node-v0.12.7-x64.msi python-2.7.10.amd64.msi Git-2.5.1-64-bit.exe (注意:Git安装时,需要选择的步骤) 安装位置 ...
- css3字阴影text-shadow
看到text-shadow这句代码,眼尖的同学是不是觉得很熟悉?没错,前面我们已经学习过<css3基础教程五边框box-shadow>,而且这两者非常相近,只要以前的课程学好了,text- ...