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 (五颗线段树)的更多相关文章

  1. Codeforces 85D Sum of Medians(线段树)

    题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...

  2. CF240F (26颗线段树计数)

    题目链接:Topcoder----洛谷 题目大意: 给定一个长为n的由a到z组成的字符串,有m次操作,每次操作将[l,r]这些位置的字符进行重排,得到字典序最小的回文字符串,如果无法操作就不进行. 思 ...

  3. ACM-ICPC 2018 徐州赛区网络预赛 G Trace(逆向,两颗线段树写法)

    https://nanti.jisuanke.com/t/31459 思路 凡是后面的轨迹对前面的轨迹有影响的,可以尝试从后往前扫 区间修改需要push_down,单点更新所以不需要push_up(用 ...

  4. HDU 4267 A Simple Problem with Integers(2012年长春网络赛A 多颗线段树+单点查询)

    以前似乎做过类似的不过当时完全不会.现在看到就有点思路了,开始还有洋洋得意得觉得自己有不小的进步了,结果思路错了...改了很久后测试数据过了还果断爆空间... 给你一串数字A,然后是两种操作: &qu ...

  5. CodeForces 85D Sum of Medians Splay | 线段树

    Sum of Medians 题解: 对于这个题目,先想到是建立5棵Splay,然后每次更新把后面一段区间的树切下来,然后再转圈圈把切下来的树和别的树合并. 但是感觉写起来太麻烦就放弃了. 建立5棵线 ...

  6. 数据结构(线段树):CodeForces 85D Sum of Medians

    D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  7. Codeforces 85D Sum of Medians

    传送门 D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  8. 85D Sum of Medians

    传送门 题目 In one well-known algorithm of finding the k-th order statistics we should divide all element ...

  9. CF 552(div 3) E Two Teams 线段树,模拟链表

    题目链接:http://codeforces.com/contest/1154/problem/E 题意:两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...

随机推荐

  1. xcode升级或者重新安装后不能编译的解决方法

    昨天由于xcode有一些问题,因此进行了重新安装,结果安装好后进行编译,没有进行任何改动的代码出现了两个fatal error 查看错误信息为什么的header has allready build, ...

  2. 权限系统与RBAC模型概述

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3793894.html ...

  3. HTML5之填写个人信息

  4. asp.net 事件模型

    asp.net的原始设计构想,就是要让开发人员能够像 VB 开发工具那样,可以使用事件驱动式程序开发模式 (Event-Driven Programming Model) 的方法来开发网页与应用程序, ...

  5. c# 二维码 显示

    需要引用 ThoughtWorks.QRCode.dll 网上可下载 //方法 public ActionResult GenerateQRCode(string content) { try { _ ...

  6. linux工具问题,tail -f 失效

    最近发现一个很奇怪问题: tail -f 不能实时的输出日志

  7. HTML5 文件域+FileReader 分段读取文件并上传到服务器(六)

    说明:使用Ajax方式上传,文件不能过大,最好小于三四百兆,因为过多的连续Ajax请求会使后台崩溃,获取InputStream中数据会为空,尤其在Google浏览器测试过程中. 1.简单分段读取文件为 ...

  8. Android Animation学习 实现 IOS 滤镜退出动画

    IOS的用户体验做的很好,其中一点很重要的地方就是动画效果. 最近在学习Android的Animation,简单实现了一个IOS相机滤镜退出的动画: 布局文件:activity_animation_d ...

  9. Simple screenshot that explains the singleton invocation.

    Here is the code: /* Some class,such as a config file,need to be only one.So we need to control the ...

  10. bash: ./configure: 权限不够 怎么办?

    configure没有执行权限 通过chmod给其加上x权限 chmod +x configure 再在该用户下执行 ./configure