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个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...
 
随机推荐
- jQuery ajax 传递数组到struts2
			
使用jQuery的$.ajax()方法进行异步交互时,如果传递的数据有数组(例如传输checkbox数据),Action中经常会接受不到数据. 此时应该注意一下data中数组的写法,例如: //组合成 ...
 - Bash算术计算
			
1:$(( )) 2:$[ ] 3:`expr $x + $y` 4:bc命令 #!/bin/bash x= y= echo $(( x + y )) echo $[ $x + $y ] echo ` ...
 - Android开发手记(31) 使用MediaRecorder录音
			
使用Android手机的时候,有时我们会用到录音功能,本文简单的介绍了如何使用MediaRecorder通过手机自带麦克风进行录音. 首先,既然是录音,我们需要录音和写外存的权限: <uses- ...
 - CentOS 6.5下搭建NFS文件服务器
			
环境介绍:服务器: 192.168.0.1客户机: 192.168.0.2安装软件包:服务器和客户机都要安装nfs 和 rpcbind 软件包:yum -y install nfs-utils rpc ...
 - oracle 的 startup,startup mount,startup nomount之间的区别
			
startup,startup mount,startup nomount之间的区别 startup nomount选项:(读初始化参数文件,启动实例) startup nomount选项启动实例 ...
 - oracle数据库读取操作系统的物理文件-转载,待完善
			
--源地址不详 --创建目录SQL> create directory dir_xls as '/home/oracle'; Directory created. --给用户授权SQL> ...
 - IOS DLNA开发(CyberLink和PlatinumKit)
			
1.CyberLink 和 PlatinumKit 两者的比较 CyberLink大概在2010年之后功能就没有更新,部分功能不够完善,网上有下载地址 http://www.pudn.com/down ...
 - 仿小米网jQuery全屏滚动插件fullPage.js
			
演 示 下 载 简介 如今我们经常能见到全屏网站,尤其是国外网站.这些网站用几幅很大的图片或色块做背景,再添加一些简单的内容,显得格外的高端大气上档次.比如 iPhone 5C 的介绍页面,QQ浏 ...
 - phpexcel 一些基本的设置 (表格的基本属性)
			
网址是:http://www.thinkphp.cn/code/1893.html
 - LI 导航
			
HTML: <div class="my_nav"> <ul> <li class="tiao cur" id="ord ...