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个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...
随机推荐
- SQL Cursor 基本用法
1 table1结构如下 2 id int 3 name varchar(50) 4 5 declare @id int 6 declare @name varchar(50) ...
- svn 设置钩子将代码同步到web目录下面
首先:确定思路: 要在SVN服务中,找到仓库文件夹的位置,在相应的项目中找到hooks文件夹.在该文件中添加一个post-commit文件: 当有commit动作发生时(提交到SVN服务是就会执行这个 ...
- JS 模拟C# 字符串格式化操作
/*** ** 功能: 字符串格式化替换操作 ***/ String.prototype.format = function () { var args = arguments; return thi ...
- 前端自动化构建工具 Gulp 使用
一个月没写博客了,今天有时间,就写个gulp的入门使用吧.. 简介:gulp是一个前端自动化构建工具,可以实现代码的检查.压缩.合并……等等,gulp是基于Node.js的自动任务运行器 一.安装No ...
- 关于Entity Framework 5 从数据库生成模型时没有字段注释的解决方法!
目前用到了EF5进行模型创建,发现从数据库生成过来的实体中并没有包含字段的说明信息(鄙视下微软,这么简单的问题都不给解决下,太粗枝大叶了),网上找到了EFTSQLDocumentation.Gener ...
- iOS打包ipa安装包的流程
应用的发布也分两种 一种是.打包成ipa上传到国内第3方软件市场,当用户的手机已经JailBreak时,双击下载的ipa文件就可以安装软件 (ipa同android的apk包一样,实质是一个压缩包) ...
- 关键字throw(something)限制
C++函数后加关键字throw(something)限制,是对这个函数的异常安全性作出限制.void f() throw() 表示f不允许抛出任何异常,即f是异常安全的.void f() throw( ...
- 【模板】【凸包】Graham_scan
/* 唐代李白 <江夏别宋之悌> 楚水清若空,遥将碧海通.人分千里外,兴在一杯中. 谷鸟吟晴日,江猿啸晚风.平生不下泪,于此泣无穷. */ #include <iostream> ...
- 用Python高亮org-mode代码块
文章同时可在我的github blog上阅读:http://cheukyin.github.io/python/2014-08/pygments-highlight-src-export-html.h ...
- Application.Exit()结束程序,但线程还在的解决方法
转自:http://bbs.51cto.com/thread-970057-1.html 出现此情况大多原因是使用了多线程编程,或者你所调用的dll使用了多线程. 我们知道,一般情况下的线程执行完指 ...