Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树
题目链接:
Sum of Medians
Time Limit:3000MSMemory Limit:262144KB
#### 问题描述
> In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance speed on a modern video card, you should be able to find a sum of medians in each five of the array.
>
> A sum of medians of a sorted k-element set S = {a1, a2, ..., ak}, where a1
>
> The operator stands for taking the remainder, that is stands for the remainder of dividing x by y.
>
> To organize exercise testing quickly calculating the sum of medians for a changing set was needed.
#### 输入
> The first line contains number n (1 ≤ n ≤ 105), the number of operations performed.
>
> Then each of n lines contains the description of one of the three operations:
>
> add x — add the element x to the set;
> del x — delete the element x from the set;
> sum — find the sum of medians of the set.
> For any add x operation it is true that the element x is not included in the set directly before the operation.
>
> For any del x operation it is true that the element x is included in the set directly before the operation.
>
> All the numbers in the input are positive integers, not exceeding 109.
#### 输出
> For each operation sum print on the single line the sum of medians of the current set. If the set is empty, print 0.
>
> Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator).
#### 样例
> **sample input**
> 14
> add 1
> add 7
> add 2
> add 5
> sum
> add 6
> add 8
> add 9
> add 3
> add 4
> add 10
> sum
> del 1
> sum
>
> **sample output**
> 5
> 11
> 13
题意
求当前有序集合中所有下标%5==3的数字的和。
题解
对于一个区间,我们可以维护相对的%5=x的位置,比如对于区间[a,a],那它%5为1的数为val[a],其他的都为0,这样我们在合并的时候左边的%5==x的位置是不会变的,右边的只要看左边有多少个数就知道要用x’(偏移)取和x合并了。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M l+(r-l)/2
using namespace std;
const int maxn = 1e5 + 10;
typedef __int64 LL;
LL sumv[maxn << 2][5];
int cntv[maxn << 2];
int n;
char cmd[maxn][11];
int val[maxn];
vector<int> ha;
void maintain(int o) {
cntv[o] = cntv[lson] + cntv[rson];
for (int i = 0; i < 5; i++) {
sumv[o][i] = sumv[lson][i] + sumv[rson][((i - cntv[lson]) % 5 + 5) % 5];
}
}
int _p,_type;
void update(int o, int l, int r) {
if (l==r) {
if (_type =='d') {
sumv[o][1] = 0;
cntv[o] = 0;
}
else {
sumv[o][1] = ha[l - 1];
//printf("(%d,%d):%d", l);
cntv[o] = 1;
}
}
else {
if (_p <= M) update(lson, l, M);
else update(rson, M + 1, r);
maintain(o);
}
}
int main() {
scanf("%d", &n);
memset(sumv, 0, sizeof(sumv));
memset(cntv, 0, sizeof(cntv));
for (int i = 0; i < n; i++) {
scanf("%s", cmd[i]);
if (cmd[i][0] != 's') {
scanf("%d", &val[i]);
ha.push_back(val[i]);
}
}
sort(ha.begin(), ha.end());
ha.erase(unique(ha.begin(), ha.end()),ha.end());
for (int i = 0; i < n; i++) {
if (cmd[i][0] !='s') {
_p = lower_bound(ha.begin(), ha.end(), val[i]) - ha.begin() + 1;
_type = cmd[i][0];
update(1, 1, n);
}
else {
printf("%I64d\n", sumv[1][3]);
}
}
return 0;
}
乱起八糟
线段树是递归的思想,所以它区间保存的数据不能是绝对的!只能是相对的!是只对当前这个区间定义的!而不是对整个区间定义的!所以,如果你想用子节点来表示在全局中%5是什么情况,可能就会比较麻烦了吧。
Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树的更多相关文章
- 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(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
- CodeForces 86D(Yandex.Algorithm 2011 Round 2)
思路:莫队算法,离线操作,将所有询问的左端点进行分块(分成sqrt(n) 块每块sqrt(n)个),用左端点的块号进行排序小的在前,块号相等的,右端点小的在前面. 这样要是两个相邻的查询在同一块内左端 ...
- Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队
题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- 【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) ...
- 【BZOJ4262】Sum 单调栈+线段树
[BZOJ4262]Sum Description Input 第一行一个数 t,表示询问组数. 第一行一个数 t,表示询问组数. 接下来 t 行,每行四个数 l_1, r_1, l_2, r_2. ...
- hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]
传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131 ...
- HDU5638 / BestCoder Round #74 (div.1) 1003 Toposort 线段树+拓扑排序
Toposort 问题描述 给出nn个点mm条边的有向无环图. 要求删掉恰好kk条边使得字典序最小的拓扑序列尽可能小. 输入描述 输入包含多组数据. 第一行有一个整数TT, 表示测试数据组数. 对 ...
随机推荐
- POJ C程序设计进阶 编程题#1:单词翻转
编程题#1:单词翻转 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一 ...
- 各种数据处理方案(SQL,NoSQL,其他)的应用场景
综合stackoverflow和linkin上的相关讨论,还有我个人的工作经验: Redis应用场景(大部分场景下memcache可以用Redis代替,所以不单独讨论) 线上业务,读写的高性能要求 ...
- Java 连接MongoDB
1.驱动 通过java连接MongoDB需要一个java版的驱动 下载地址:http://mongodb.github.io/mongo-java-driver/ 2.连接MongoDB 通过 com ...
- ThinkPHP之中的getField、Find、select、返回数据类型详解(ThinkPHP之中所有数据读取了)
小李子:用于演示作用的数据库表:customers 官方解读: “ 读取数据集其实就是获取数据表中的多行记录(以及关联数据),使用select方法 ” $customers=D('customers' ...
- JavaScript相关知识
JavaScript的语法规则 l JavaScript区分大小写 比如变量a和变量A是不一样的变量,要严格区分大小写 l JavaScript脚本程序须嵌入在HTML文件中 因为javascript ...
- PHP实现下载功能之流程分析
客户端从服务端下载文件的流程分析: 浏览器发送一个请求,请求访问服务器中的某个网页(如:down.php),该网页的代码如下. 服务器接受到该请求以后,马上运行该down.php文件 运行该文件的时候 ...
- Hao123这个流氓
Author:KillerLegend Date:2014.2.27 From:http://www.cnblogs.com/killerlegend/p/3572591.html Hao123真让人 ...
- xtrabackup之Innobackupex全备恢复
一.当前环境 [mysql@hadoop1 ~]$ mysql --defaults-/my.cnf -uroot -p123456 -P3306 mysql> show variables l ...
- 关于fseek和文件"ab+"打开方式的问题
这是在写一个文件的的时候发生的一个错误,代码如下 #include<stdio.h> #include <errno.h> #include <string.h> ...
- linux之i2c子系统架构---总线驱动
编写i2c设备驱动(从设备)一般有两种方式: 1.用户自己编写独立的从设备驱动,应用程序直接使用即可. 2.linux内核内部已经实现了一个通用的设备驱动,利用通用设备驱动编写一个应用程序(用户态驱动 ...