Codeforces 85D Sum of Medians(线段树)
题目链接: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(线段树)的更多相关文章
- CodeForces 85D Sum of Medians Splay | 线段树
Sum of Medians 题解: 对于这个题目,先想到是建立5棵Splay,然后每次更新把后面一段区间的树切下来,然后再转圈圈把切下来的树和别的树合并. 但是感觉写起来太麻烦就放弃了. 建立5棵线 ...
- 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 ...
- 数据结构(线段树):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 ...
- Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树
题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of find ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- CF 85D Sum of Medians (五颗线段树)
http://codeforces.com/problemset/problem/85/D 题意: 给你N(0<N<1e5)次操作,每次操作有3种方式, 1.向集合里加一个数a(0< ...
- 【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) ...
随机推荐
- java程序获得SqlServer数据表的表结构
/** * 取得一张表的结构信息 * 使用DatabaseMetaData与ResultSetMetaData结合的方式获取全部属性 * @param conn 数据连接 * @p ...
- android adb经常使用的命令
1.adb--- 订购屏幕截取 :adb shell screencap -p | sed 's/\r$//' > outputs.png 2.jni ---命令 :$NDK/ndk-b ...
- Sql缓存依赖--数据库缓存
•依赖于文件内容CacheDependency cDep = new CacheDependency(filePath); •依赖于数据库内容(轮询机制/通知机制)一:轮询机制 1.在数据库新建版本表 ...
- sql 时间和字符串 取到毫秒级
(select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')+(Select ri ...
- 由浅到深讲解Mybatis
Mybatis的简介 什么是Mybatis? Mybatis是支持定制化SQL,存储过程以及高级映射的持久化框架.Mybatis避免了几乎所有的JDBC代码和 手动设置获取结果集.Mybatis可以对 ...
- Servlet转发和重定向的区别
附上视频教学的一张图: 区别: 1.转发产生一次请求,一次响应: 重定向产生2次请求 两次响应 2.转发客户端不可见的: 重定向客户端是可以察觉的. 3.转发时候url不变: 重定向URL会改变 案例 ...
- ThinkPHP第七天(F函数使用,项目分组配置,项目分组模板文件放置规则配置)
1.F(文件名称,写入数据,写入地址),用于将数据写入至磁盘文件中,如F('Data',$arr,'./Data/'),作用是将$arr写入至网站根目录的Data文件夹中的Data.php中. 2.读 ...
- Android 开发笔记 “线程交互(Handler+Thread 和 AsyncTask)”
为什么需要线程 假设需要开发一个联网应用程序,需要从一个网址抓取网页内容,这里读取的网页地址是笔者在本地机器上自己建立的服务器地址.当然在读取网页内容的时候,可以使用HttpClient提供的API, ...
- django最佳实践
导入的时候使用绝对导入或者清晰的相对导入 相对导入用法: from __future__ import absolute_import from .models import what_u_need ...
- 运行mvc报“无法确定存储版本;需要有效的存储连接或版本提示”
解决方法:删除数据库重建