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 ...
随机推荐
- Loadrunner参数(摘)
一.占有率分析 1. 平均事务响应时间 Average Transaction Response Time 优秀:<2s 良好:2-5s 及格:6-10s 不及格:>10s 2. 每秒点击 ...
- 【有容云案例系列】基于Jenkins和Kubernetes的CI工作流
嘉宾介绍 黄文俊 有容云资深系统架构师 主要负责容器云平台产品架构及设计. 8年工作经验, 有着企业级存储, 云计算解决方案相关理解. 关注于微服务设计思考, 开发流程优化, docker及kuber ...
- selenium定时签到程序
selenium定时签到程序 定时任务 # -*- coding: utf-8 -*- import time import os import sched import datetime from ...
- gitee+hexo搭建个人博客
通过gitee和hexo搭建个人博客 首先准备软件: git (提供命令git) git官网 notepad++(方便编辑)notepad++官网 nodejs(hexo依赖)nodejs官网 7z( ...
- 基于Spring框架应用的权限控制系统的研究和实现
摘 要: Spring框架是一个优秀的多层J2EE系统框架,Spring本身没有提供对系统的安全性支持.Acegi是基于Spring IOC 和 AOP机制实现的一个安全框架.本文探讨了Acegi安全 ...
- 安装CUDA9.0及对应版本的tensorflow-gpu详细过程(Windows server 2012R2版本也可以)
由于最近跑机器学习相关代码的时候CPU运算速度跟不上,这才利用GPU来运算代码,显然使用GPU来运算速度明显要快很多,但是搭配GPU的使用环境是真的麻烦且头疼.网上有很多牛人的搭建过程,虽然他们都成功 ...
- android——SQLite数据库存储(创建)
Android 专门提供了SQLiteOpenHelper帮助类,借助这个类就可以非常简单的对数据库进行创建和升级. 首先SQLiteOpenHelper是一个抽象类,在使用的时候需要创建一个自己的帮 ...
- ZooKeeper系列(五)—— ACL 权限控制
一.前言 为了避免存储在 Zookeeper 上的数据被其他程序或者人为误修改,Zookeeper 提供了 ACL(Access Control Lists) 进行权限控制.只有拥有对应权限的用户才可 ...
- 【C语言基础】unsigned short类型用于循环的一个难点
我在我的知识星球:“C语言解惑课堂”里的第一篇提出一个问题:[第1篇][C语言基础][unsigned short类型用于循环的一个难点]要查看更多的C语言难点解析或者需要提问的同学,微信扫扫文末我的 ...
- gunicorn 基础配置使用
flask 自带的 web 服务器稳定性较差,只能用于测试.最近做的 web 项目,每次启动,需要敲一堆参数文件,今天学习了官方文档里关于配置的说明,记录一下. 创建一个 gunicorn.conf ...