题目链接: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);elsemodify(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]++;elseG[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(线段树or分块)

    题目链接:codeforces 85D. Sum of Medians 题意: add x 表示向集合中添加x(添加x的时候保证x是第一次被添加入集合) del x 表示从集合中删除x (删除x的时候 ...

  2. Codeforces 85D Sum of Medians[线段树]

    题意:给了一个set,有n个操作,有三种操作 1. add  x 把 x 放入set中: 2. del  x 把 x 从set中删去: 3. sum 求set中,第n大的数,n%5==3,的总和. 分 ...

  3. codeforces 85D. Sum of Medians

    二次联通门 : codeforces 85D. Sum of Medians /*codeforces 85D. Sum of Medians正解线段树或是平衡树结果用vector暴力卡过去了 */ ...

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

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

  5. Codeforces 85D Sum of Medians

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

  6. Codeforces 444C DZY Loves Colors 线段树区间更新

    // Codeforces 444C DZY Loves Colors 线段树区间更新// 题目链接:// http://codeforces.com/problemset/problem/444/C ...

  7. hdoj 4288coder cf 85d Sum of Medians

    题目链接 这两个题目是一样的,大概题意是有3个操作 add x, 在集合中加入x, del x 是删除x, sum 是求出由小到大排序后所有下标mod5等于3的数的和. 这个在hdoj上面,这个题给的 ...

  8. codeforces 581B Luxurious Houses(线段树点更新,区间查询)

    题目链接: http://codeforces.com/problemset/problem/581/B 题目大意: 给n个不同高度的房子,要求当对于第i个房子来说,他要严格的比后面的房子都高. 思路 ...

  9. CodeForces - 1557D Ezzat and Grid(线段树+dp)

    题目链接:点击查看 题目大意:给出 nnn 个 010101 串,现在问最少需要删掉多少个串,才能使得剩下的串拼起来是连通的 规定两个 010101 串是连通的,当且仅当存在至少一列,在两个串中都为 ...

最新文章

  1. android自定义文件选择,关于安卓自定义本地文件选择库的实现
  2. java中位与运算符_Java中位运算符和的区别
  3. android java.lang.IllegalArgumentException: The observer is null.异常解决
  4. python定制框架知识点_我的第一个python web开发框架(25)——定制ORM(一)
  5. 七个步骤,带你快速读懂 RPC 框架原理
  6. php prettyprinter,gdb运行时错误:prettyprinter已注册:libstdc++v6
  7. Fibonacci数列整除性质的组合证明
  8. 利用jieba进行中文分词并进行词频统计
  9. 计算机专业代码qian,专业分类号及学科代码对照表.doc
  10. java生成xps文件_Java 将 Excel 转为PDF、图片、html、XPS、XML、CSV
  11. 微信小程序直播有哪些推广技巧?
  12. [游戏] 星际争霸2:一个新的传奇?
  13. SAP中有些物料凭证不能用MBST冲销的原因分析
  14. 3dmax2014【3dsmax2014】官方简体中文(64位)安装图文教程、破解注册方法
  15. Python数据处理——pandas
  16. nodejs微信公众号开发第一步(接入指南)--wechat模块
  17. 无监督低照度图像增强网络ZeroDCE和SCI介绍
  18. linux系统如何安装mtk驱动程序,模块编译问题 给MTK芯片的wifi网卡编译linux驱动 系统是mint...
  19. etcher制作linux启动盘,使用Etcher来创建可启动盘的方法
  20. [Java进阶]学习笔记2:毫秒值的概念和作用

热门文章

  1. html如何自己做一个背景特效,背景效果实现方法总结
  2. 大话2点卡稳定服务器,大话西游2新区点卡比例增涨太快,这究竟是什么原因
  3. 全国青少年编程等级考试scratch四级真题2021年9月(含题库答题软件账号)
  4. 通过windows自带远程桌面,实现不同局域网的电脑相互访问(默默P2P远程桌面管理工具-直接内网穿透)
  5. 微信网页授权校验文件
  6. 什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
  7. 服务器客户端传输文件,服务器传输文件到客户端
  8. esx 主机cli命令行简单介绍
  9. web前端需要学习什么?需要掌握什么技术
  10. python 遍历目录