codeforces 284 C. Cows and Sequence(线段树)
题目链接:http://codeforces.com/contest/284/problem/C
题意:就是给出3个操作
1)是将前i 个数加x
2)在数组最后添加一个数x
3)删除数组最后的那个数
题意:简单的线段树操作
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
const int M = 2e5 + 10;
struct TnT {
int l , r , lazy;
ll sum;
}T[M << 2];
void build(int l , int r , int i) {
int mid = (l + r) >> 1;
T[i].l = l , T[i].r = r , T[i].sum = 0 , T[i].lazy = 0;
if(l == r)
return ;
build(l , mid , i << 1);
build(mid + 1 , r , (i << 1) | 1);
}
void pushup(int i) {
T[i].sum = T[i << 1].sum + T[(i << 1) | 1].sum;
}
void pushdown(int i) {
if(T[i].l != T[i].r) {
if(T[i].lazy) {
int ad = T[i].lazy;
T[i << 1].sum += (ll)ad * (T[i << 1].r - T[i << 1].l + 1);
T[(i << 1) | 1].sum += (ll)ad * (T[(i << 1) | 1].r - T[(i << 1) | 1].l + 1);
T[i << 1].lazy += ad;
T[(i << 1) | 1].lazy += ad;
T[i].lazy = 0;
}
}
}
void updata1(int i , int pos , int ad) {
int mid = (T[i].l + T[i].r) >> 1;
if(T[i].l == pos && T[i].r == pos) {
T[i].sum = (ll)ad;
return ;
}
pushdown(i);
if(mid < pos) {
updata1((i << 1) | 1 , pos , ad);
}
else {
updata1(i << 1 , pos , ad);
}
pushup(i);
}
void updata2(int i , int l , int r , int ad) {
int mid = (T[i].l + T[i].r) >> 1;
if(T[i].l == l && T[i].r == r) {
T[i].sum += (ll)ad * (T[i].r - T[i].l + 1);
T[i].lazy += ad;
return ;
}
pushdown(i);
if(mid < l) {
updata2((i << 1) | 1 , l , r , ad);
}
else if(mid >= r) {
updata2(i << 1 , l , r , ad);
}
else {
updata2(i << 1 , l , mid , ad) , updata2((i << 1) | 1 , mid + 1 , r , ad);
}
pushup(i);
}
long long query(int i , int l , int r) {
int mid = (T[i].l + T[i].r) >> 1;
if(T[i].l == l && T[i].r == r) {
return T[i].sum;
}
pushdown(i);
pushup(i);
if(mid < l) {
return query((i << 1) | 1 , l , r);
}
else if(mid >= r) {
return query(i << 1 , l , r);
}
else {
return query(i << 1 , l , mid) + query((i << 1) | 1 , mid + 1 , r);
}
}
int main() {
int n;
scanf("%d" , &n);
build(1 , M , 1);
int pos = 1 ;
long long sum;
while(n--) {
int t , a , x;
scanf("%d" , &t);
if(t == 1) {
scanf("%d%d" , &a , &x);
updata2(1 , 1 , a , x);
sum = query(1 , 1 , max(pos , 1));
}
if(t == 2) {
scanf("%d" , &a);
updata1(1 , pos + 1 , a);
pos++;
sum = query(1 , 1 , pos);
}
if(t == 3) {
updata1(1 , max(pos , 1) , 0);
pos--;
sum = query(1 , 1 , max(pos , 1));
}
//cout << sum << endl;
printf("%.6lf\n" , (double)sum / pos);
}
return 0;
}
codeforces 284 C. Cows and Sequence(线段树)的更多相关文章
- Codeforces 438D The Child and Sequence - 线段树
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...
- CodeForces 438D The Child and Sequence (线段树 暴力)
传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces 486E LIS of Sequence(线段树+LIS)
题目链接:Codeforces 486E LIS of Sequence 题目大意:给定一个数组.如今要确定每一个位置上的数属于哪一种类型. 解题思路:先求出每一个位置选的情况下的最长LIS,由于開始 ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模
D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his h ...
随机推荐
- go 学习笔记之工作空间
搭建好 Go 的基本环境后,现在可以正式开始 Go 语言的学习之旅,初学时建议在默认的 GOPATH 工作空间规范编写代码,基本目录结构大概是这个样子. . |-- bin | `-- hello.e ...
- 【简洁易懂】CF372C Watching Fireworks is Fun dp + 单调队列优化 dp优化 ACM codeforces
题目大意 一条街道有$n$个区域. 从左到右编号为$1$到$n$. 相邻区域之间的距离为$1$. 在节日期间,有$m$次烟花要燃放. 第$i$次烟花燃放区域为$a_i$ ,幸福属性为$b_i$,时间为 ...
- 集成方法 Ensemble
一.bagging 用于基础模型复杂.容易过拟合的情况,用来减小 variance(比如决策树).基础模型之间没有太多联系(相对于boosting),训练可以并行.但用 bagging 并不能有助于把 ...
- Redis的分布式和主备配置调研
目前Redis实现集群的方法主要是采用一致性哈稀分片(Shard),将不同的key分配到不同的redis server上,达到横向扩展的目的. 对于一致性哈稀分片的算法,Jedis-2.0.0已经提供 ...
- 章节十五、8-配置文件File Logging
一.如何将log输出到文件中? 1.配置xml文件 <?xml version="1.0" encoding="UTF-8"?> <Confi ...
- Linux安装nfs共享文件
简介nfs nfs网络文件系统常用于共享音视频,图片等静态资源.将需要共享的资源放到NFS里的共享目录,通过服务器挂载实现访问. 服务端安装: yum install -y nfs-utils rpc ...
- Docker 方式部署 Solo 博客系统总结
此篇为Docker部署方式,另有Tomcat部署方式,请参考文章<Tomcat 方式部署 Solo 博客系统总结> 最近搭建了一个博客系统,作为自己的主页,方便记录一些平时所见所闻 ...
- 驰骋工作流引擎ccflow-流转自定义功能使用说明
流转自定义功能使用说明 关键字: 驰骋工作流程快速开发平台 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 节点跳转 节点流转自定义 应用背景: 有一些流程在运行过程中是 ...
- Oracle中的转换函数
Oracle中的转换函数有三个,分别为to_char(),to_date(),to_number() 1.to_char()的用法 格式化当前的日期时间 select sysdate,to_char( ...
- spring-boot-plus更新日志 CHANGELOG(九)
spring-boot-plus更新日志 CHANGELOG [V1.2.0-RELEASE] 2019.08.06