CodeForces 85D Sum of Medians Splay | 线段树
题解:
对于这个题目,先想到是建立5棵Splay,然后每次更新把后面一段区间的树切下来,然后再转圈圈把切下来的树和别的树合并。
但是感觉写起来太麻烦就放弃了。
建立5棵线段树。
然后 seg[rt][i]代表的是只考虑当前所管辖的区间中的情况下, 下标对5取余之后为 i 的那些值的和。
最重要的一点是更新。
for(int i = ; i < ; ++i)
seg[i][rt] = seg[i][rt<<] + seg[((i-sz[rt<<]%)+)%][rt<<|];
我们可以通过左边的sz,来找到右边的数如果加上前面的sz之后,第i个值是哪里过来的。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
int sz[N<<];
LL seg[][N<<];
int op[N], v[N];
int b[N], m;
char s[N];
int id(int x){
return lower_bound(b+, b++m, x) - b;
}
void Updata(int L, int v, int l, int r, int rt){
if(l == r){
seg[][rt] += b[L] * v;
sz[rt] += v;
return ;
}
int m = l+r >> ;
if(L <= m) Updata(L, v, lson);
else Updata(L, v, rson);
sz[rt] += v;
for(int i = ; i < ; ++i)
seg[i][rt] = seg[i][rt<<] + seg[((i-sz[rt<<]%)+)%][rt<<|];
}
int main(){
int q;
scanf("%d", &q);
for(int i = ; i <= q; ++i){
scanf("%s", s);
if(s[] == 'a') {
op[i] = ;
scanf("%d", &v[i]);
b[++m] = v[i];
}
else if(s[] == 'd'){
op[i] = -;
scanf("%d", &v[i]);
}
}
sort(b+, b++m);
m = unique(b+, b++m) - (b+);
for(int i = ; i <= q; ++i){
if(!op[i]) printf("%lld\n", seg[][]);
else
Updata(id(v[i]), op[i], , m, );
}
return ;
}
/*
4
add 1
add 2
add 3
sum */
CodeForces 85D Sum of Medians Splay | 线段树的更多相关文章
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
- 数据结构(线段树):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 ...
- Coderforces 85 D. Sum of Medians(线段树单点修改)
D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
- 2018.12.15 codeforces 920F. SUM and REPLACE(线段树)
传送门 线段树入门题. 给你一个序列:支持区间修改成自己的约数个数,区间求和. 实际上跟区间开方一个道理. 2的约数个数为2,1的约数个数为1,因此只要区间的最大值小于3就不用修改否则就暴力修改. 因 ...
- CodeForces - 920F SUM and REPLACE (线段树)
题意:给N个数M次操作,(1<=N,M<=3e5, 1<=ai<=1e6),1是使[L,R]中的每个元素变成其因子的个数之和:2是求[L,R]区间之和 分析:看上去就很线段树的 ...
- Codeforces 920F - SUM and REPLACE 【线段树】
<题目链接> 题目大意: 给你一个序列,有两个操作,一个是求区间 l - r 的和,另一个是对区间l-r的元素修改值,x=d(x),d(x)为x的因子个数. 解题分析: 因为可能有多次修改 ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- POJ 3415 Max Sum of Max-K-sub-sequence (线段树+dp思想)
Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
随机推荐
- codeforces 327 B. Hungry Sequence
题目链接 题目就是让你输出n个数的序列,要保证该序列是递增的,并且第i个数的前面不能保护它的约数,我直接先对前100000的素数打表,然后输出前n个,so easy. //cf 191 B #incl ...
- Web前端开发——Ionic 3.0【爱创课堂专业前端培训】
前端开发——Ionic 3.0 一.Ionic 移动端有三种开发方向 源生APP开发, 移动端web开发 混合开发(介于以上两者之间的) 类微信小程序 reactNative,用react语法,开发a ...
- kylin Retrieving hive dependency...
由于公司环境配置hive默认连接hiveserver2 ,不管hive cli 还是beeline cli都默认使用beeline cli,连接hive需要输入账号密码; 启动kylin 时会Retr ...
- ubuntu安装伪分布式Hadoop3.1.2
作业要求:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3223 本文是基于已经安装好的ubuntu环境上搭建伪分布式hadoop,在 ...
- 邻域保持嵌入(NPE)
传统的线性降维方法,如主成分分析(PCA).因子分析(FA)等,关注的是样本的方差,能学习线性流形的结构,却无法学习非线性流形.而经典的流形学习方法虽然能够学习非线性流形结构,但由于本身属于直推学习, ...
- Pandas 库之 DataFrame
How to use DataFrame ? 简介 创建 DataFrame 查看与筛选数据:行列选取 DataFrame 数据操作:增删改 一.About DataFrame DataFrame 是 ...
- 非常实用的select下拉框-Select2.js
在Web开发中,Select下拉框是常用的输入组件.由于原生的Select几乎很难通过CSS样式控制.一些好看的Select下拉框就只能通过模拟来实现.PHP程序员雷雪松给大家推荐一筐款不错的Sele ...
- 搭建Springboot网站有感
最近心血来潮,搭建了个人网站,一方面想学习下新的知识,另一方面也想有个作品,在这分享下自己的体会,先不说知识点. 建站容易吗,因人而异,而我在完成这个最最简单的工作时起码经历了3个阶段不同的心理变化, ...
- 201412-2 Z字形扫描(c语言)
问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...
- cs231n---强化学习
介绍了基于价值函数和基于策略梯度的两种强化学习框架,并介绍了四种强化学习算法:Q-learning,DQN,REINFORCE,Actot-Critic 1 强化学习问题建模 上图中,智能体agent ...