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 ...
随机推荐
- Java内存模型的基础
Java内存模型的基础 并发编程模型的两个关键问题 在并发编程中,需要处理两个关键问题:线程之间如何通信及线程之间如何同步(这里的线程是指并发执行的活动实体).通信是指线程之间以何种机制来交换信息.在 ...
- 重启iis的命令是什么?三种简单的重启方式
第一种.界面操作 打开“控制面板”->“管理工具”->“服务”.找到“IIS Admin Service” 右键点击“重新启动” 弹出 “停止其它服务” 窗口,点击“是”. 第二种.Net ...
- oracle 断电启动失败:ORA-00600: internal error code, arguments
转载地址: http://www.2cto.com/database/201312/261602.html 由于服务器断电,启动 oracle 时报 ORA-00600 错误 查看 oracle tr ...
- 单元测试python unittest
记录自己学习单元测试框架的一篇博客 菜鸟的学习之路比较艰辛到处找资料一把辛酸泪啊 1.首先是创建一个类里面设计一些简单的函数方便写用例: 原谅我蹩脚的英文直接用拼音命名了 : 2.接着就是创建用例文件 ...
- Hyper-v设置linux固定ip
一.创建CentOS 7专用的虚拟交换机 打开Hyper-v控制面板,找到右边的“虚拟交换机管理器” 进去后,点击“新建虚拟网络交换机”,填写名称后,选择“内部” 打开网络中心,修改配置如下图,注意i ...
- 教老婆学Linux运维(一)初识Linux
零.前言 之一 为什么写这个系列?为什么是Linux? 老婆自从怀孕以后,辞职在家待了好几年了,现在时常感觉与社会脱节.所以想找个工作. 做了多年程序员,有点人脉也都基本是在IT圈子里,只能帮忙找找I ...
- struts的上传下载
文件上传 添加jar包 commons-io-1.3.2.jar commons-fileupload-1.2.1.jar 前台页面 form表单 method值为post 添加"encty ...
- 有关element 的一些问题(随时更新)
<el-input></el-input> input 组件中官方自带的change时间是监听失去焦点之后的value变化,要想一只监听value的值变化的话需要使用 @i ...
- JS判断字符串长度,结合element el-input el-form 表单验证(英文占1个字符,中文汉字占2个字符)
首先看看判断字符串长度的几种方法(英文占1个字符,中文汉字占2个字符) 方法一: function strlen(str) { var len = 0; for (var i = 0; i < ...
- Linux--shel的if判断语句--05
if条件语句的使用格式: 1.单分支语句 if [ 条件 ];then 执行语句 fi 注意:[ 条件 ] :条件与中括号要用空格分割:下面的语句同理. 2.双分支语句 if [ 条件 ];then ...