题目链接:Codeforces 85D - Sum of Medians

题目大意:N个操作,add x:向集合中加入x;del x:删除集合中的x;sum:将集合排序后,将集合中全部下标i % 5 = 3的元素累加求和。

解题思路:线段树单点更新,每一个点维护5个值。分别表示从该段区间中i % 5 = t的和。然后两端区间合并时仅仅须要依据左孩子中元素的个数合并。所以有一个c表示区间上元素的个数。

由于有同样的数。所以要离线操做,将全部的数映射成位置,可是对于del则不须要映射,由于集合中肯定有才干减掉。那么add和sum操作都是能够搞定了,仅仅剩下del操作,对于del x,x肯定在集合中出现过。所以每次删除第一个x就可以,假设高速查找,要借助map和一个辅助数组,由于删除一个后要又一次映射,所以借助辅助数组。

#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm> using namespace std; typedef long long ll;
const int mod = 5;
const int maxn = 1e5+5; int N, M, pos[maxn], v[maxn];
map<ll, int> G; #define lson(x) ((x)<<1)
#define rson(x) (((x)<<1)|1)
int lc[maxn << 2], rc[maxn << 2], c[maxn << 2];
ll s[maxn << 2][6]; inline void maintain (int u, int d) {
c[u] += d;
memset(s[u], 0, sizeof(s[u]));
s[u][0] = (c[u] ? pos[lc[u]] : 0);
} inline void pushup(int u) {
int t = c[lson(u)] % mod;
c[u] = c[lson(u)] + c[rson(u)];
for (int i = 0; i < mod; i++)
s[u][i] = s[lson(u)][i] + s[rson(u)][(i + mod - t) % mod];
} void build (int u, int l, int r) {
lc[u] = l;
rc[u] = r;
c[u] = 0;
memset(s[u], 0, sizeof(s[u])); if (l == r)
return;
int mid = (l + r) / 2;
build(lson(u), l, mid);
build(rson(u), mid + 1, r);
pushup(u);
} void modify (int u, int x, int d) {
if (lc[u] == x && rc[u] == x) {
maintain(u, d);
return;
} int mid = (lc[u] + rc[u]) / 2;
if (x <= mid)
modify(lson(u), x, d);
else
modify(rson(u), x, d);
pushup(u);
} struct OP {
int p, k, id;
OP (int k = 0, int p = 0, int id = 0) {
this->k = k;
this->p = p;
this->id = id;
}
friend bool operator < (const OP& a, const OP& b) {
if (a.k == 0)
return false;
if (b.k == 0)
return true;
if (a.p != b.p)
return a.p < b.p;
return a.id < b.id;
}
}; inline bool cmp (const OP& a, const OP& b) {
return a.id < b.id;
} vector<OP> vec; void init () {
scanf("%d", &N);
char op[5];
int x; for (int i = 1; i <= N; i++) {
scanf("%s", op);
if (op[0] == 's')
vec.push_back(OP(0, 0, i));
else {
scanf("%d", &x);
vec.push_back(OP(op[0] == 'a' ? 1 : -1, x, i));
}
} M = 1;
sort(vec.begin(), vec.end());
for (int i = 0; i < N; i++) {
if (vec[i].k < 0)
continue;
if (vec[i].k == 0)
break; pos[M] = vec[i].p;
vec[i].p = M++;
}
build(1, 1, M);
} void solve () {
sort(vec.begin(), vec.end(), cmp);
for (int i = 0; i < N; i++) {
//printf("%d %d!\n", vec[i].k, pos[vec[i].p]);
if (vec[i].k == 0)
printf("%lld\n", s[1][2]);
else if (vec[i].k == -1) {
int tmp = vec[i].p;
v[G[tmp]] = 0;
modify(1, G[tmp], -1); if (G[tmp] <= N && v[G[tmp]+1] && pos[G[tmp]] == pos[G[tmp]+1])
G[tmp]++;
else
G[tmp] = 0;
} else {
int tmp = pos[vec[i].p];
v[vec[i].p] = 1;
modify(1, vec[i].p, 1);
if (G[tmp] == 0)
G[tmp] = vec[i].p;
}
}
} int main () {
init();
solve();
return 0;
}

Codeforces 85D Sum of Medians(线段树)的更多相关文章

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

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

  2. codeforces 85D D. Sum of Medians 线段树

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

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

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

  4. Codeforces 85D Sum of Medians

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

  5. Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树

    题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of find ...

  6. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  7. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  8. CF 85D Sum of Medians (五颗线段树)

    http://codeforces.com/problemset/problem/85/D 题意: 给你N(0<N<1e5)次操作,每次操作有3种方式, 1.向集合里加一个数a(0< ...

  9. 【Educational Codeforces Round 37】F. SUM and REPLACE 线段树+线性筛

    题意 给定序列$a_n$,每次将$[L,R]$区间内的数$a_i$替换为$d(a_i)$,或者询问区间和 这题和区间开方有相同的操作 对于$a_i \in (1,10^6)$,$10$次$d(a_i) ...

随机推荐

  1. ThinkPHP 3.1.2 查询方式的一般使用2

    //select id1> and id2< 默认是and $data['id']=array(array('gt',$id1),array('lt',$id2)); // $data[' ...

  2. HDU 2227 Find the nondecreasing subsequences

    题目大意:给定一个序列,求出其所有的上升子序列. 题解:一开始我以为是动态规划,后来发现离散后树状数组很好做,首先,c保存的是第i位上升子系列有几个,那么树状数组的sum就直接是现在的答案了,不过更新 ...

  3. STM32关于优先级设定的理解 NVIC_SetPriority()

    Systick模块初始化配置函数(Systick_config)中设定模块中断优先级的函数为: NVIC_SetPriority((SysTick_IRQn, (1<<__NVIC_PRI ...

  4. WordPress创建过程

    最近php需要用php开发项目, 闲来无事,就研究了php,但是发了WordPress模板,这模板真心强大,简单易学好用, 好了直接正如正题 首先第一步 登录官网WordPress http://cn ...

  5. java中常见的单例模式详解

    很多求职者在面试过程中都被问到了单例模式,最常见的问题,比如,每种单例模式的区别是什么?哪些模式是线程安全的?你们项目里用的哪种单例模式?原来没有注意这个问题,回来赶紧打开项目查看了一下代码,才发现我 ...

  6. 条款05:了解C++默默编写并调用哪些函数

    每一个class都会有一个或多个构造函数.一个析构函数.一个copy assignment操作符.这些控制着基础操作,像是产出新对象并确保它被初始化.摆脱旧对象并确保它被适当清理.以及赋予对象新值. ...

  7. 基于RSA securID的Radius二次验证java实现(PAP验证方式)

    基于rsa SecurID的二次验证.RSA server自身可以作为Radius服务器,RSA也可以和其他的软件集合,使用其他的server作为Radius服务器. radius的验证的一般流程如下 ...

  8. 基于百度地图api + AngularJS 的入门地图

    转载请注明地址:http://www.cnblogs.com/enzozo/p/4368081.html 简介: 此入门地图为简易的“广州大学城”公交寻路地图,采用很少量的AngularJS进行inp ...

  9. mysql支持emoji解决办法

    mysql显示不了emoji表情或者显示??,原因这里不解释,直接说解决办法.(主要就是修改utf8mb4) 1.修改表 ALTER TABLE `TABLE_NAME` CHARACTER SET ...

  10. C语言回溯算法解决N皇后问题

    回溯算法的模型是 x++, not satisfy ? x-- : continue. 代码中x作列号,y[x]保存第x列上皇后放置的位置. #include<stdio.h> #incl ...