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. ref和out的区别在c#中 总结

     ref 关键字使参数按引用传递.其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中.简单点说就是,使用了ref和out的效果就几乎和C中使用了指针变量一样.它能够让你 ...

  2. 转载:JAVA 正则表达式 (超详细)

    在Sun的JavaJDK 1.40版本中,Java自带了支持正则表达式的包,本文就抛砖引玉地介绍了如何使用Java.util.regex包. 可粗略估计一下,除了偶尔用Linux的外,其他Linu x ...

  3. 在Java控制台模拟dos命令下操作MySQL

    一.导入mysql的驱动:"com.mysql.jdbc.Driver", 必须将mysql的驱动 包(jar文件)导入到本工程中. Class.forName("com ...

  4. [转帖]FPGA--Vivado

    来源:http://home.eeworld.com.cn/my/space-uid-639749-blogid-267593.html 一般的,在Verilog中最常用的编码方式有二进制编码(Bin ...

  5. 【POJ2352】【树状数组】Stars

    Description Astronomers often examine star maps where stars are represented by points on a plane and ...

  6. 简单高效读写修改整个文本Slurp

    语法: use File::Slurp; #标量环境下一次读取所有文本内容到标量中. my $text = read_file( 'filename' ) ; #  读取文本的所有行到数组中. my ...

  7. zoj3839-Poker Face

    #include<cstdio>int n;void P(int i,int j,int n,int f){ if(i==n){ for(int k=1;k<=n;k++)print ...

  8. 浅谈Chrome V8引擎中的垃圾回收机制

    垃圾回收器 JavaScript的垃圾回收器 JavaScript使用垃圾回收机制来自动管理内存.垃圾回收是一把双刃剑,其好处是可以大幅简化程序的内存管理代码,降低程序员的负担,减少因 长时间运转而带 ...

  9. PHP获取客户端和服务器端IP

    客户端的ip变量: $_SERVER['REMOTE_ADDR'] :客户端IP,也有可能是代理IP $_SERVER['HTTP_CLIENT_IP']:代理端的IP,可能存在,也可能伪造 $_SE ...

  10. you need to be root to perform this command linux

    获得root权限如何获得:打开终端,输入su回车 然后输入密码回车就行了