题目链接: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(线段树)的更多相关文章

  1. 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 ...

  2. CodeForces 438D The Child and Sequence (线段树 暴力)

    传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...

  3. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  4. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  5. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  6. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  7. 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 ...

  8. Codeforces 486E LIS of Sequence(线段树+LIS)

    题目链接:Codeforces 486E LIS of Sequence 题目大意:给定一个数组.如今要确定每一个位置上的数属于哪一种类型. 解题思路:先求出每一个位置选的情况下的最长LIS,由于開始 ...

  9. 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 ...

随机推荐

  1. python创建虚拟环境(Windows)

    >>>构建Python虚拟环境的目的是为了防止真实环境被破坏!!! >>>每一个项目建议用一个虚拟环境为了防止软件版本号冲突!!! 1.在终端切换到一个新的磁盘 如 ...

  2. 4. 源码分析---SOFARPC服务端暴露

    服务端的示例 我们首先贴上我们的服务端的示例: public static void main(String[] args) { ServerConfig serverConfig = new Ser ...

  3. 深入Apache NiFi 之源码学习

    前言 要问 Hortonworks 这家公司最有产品力的产品是什么,我觉得是 Apache NiFi.去年Cloudera 和 Hortonworks 合并之后,以 Cloudera 为主,两家公司进 ...

  4. Android 属性动画实战

    什么是属性动画? 属性动画可以通过直接更改 View 的属性来实现 View 动画.例如: 通过不断的更改 View 的坐标来实现让 View 移动的效果: 通过不断的更改 View 的背景来实现让 ...

  5. 【Java例题】5.3 线性表的使用

    3.线性表的使用.使用ArrayList模拟一个一维整数数组.数据由Random类随机产生.进行对输入的一个整数进行顺序查找.并进行冒泡排序. package chapter6; import jav ...

  6. 《HTTP权威指南》--阅读笔记(一)

    HTTP: HyperText Transfer Protocol 测试站点:http://www.joes-hardware.com URI包括URL和URN URI: Uniform Resour ...

  7. Spark 系列(七)—— 基于 ZooKeeper 搭建 Spark 高可用集群

    一.集群规划 这里搭建一个 3 节点的 Spark 集群,其中三台主机上均部署 Worker 服务.同时为了保证高可用,除了在 hadoop001 上部署主 Master 服务外,还在 hadoop0 ...

  8. 还在为垂直居中苦恼?CSS 布局利器 flexbox 轻轻松松帮你搞定

    传统的 CSS 布局方式是基于盒模型(它是根据盒子与父盒子以及兄弟盒子的关系确定大小和位置的算法),实现时依赖于 block, inline, table, position, float 这些属性, ...

  9. 主成分分析 Principle Component Analysis

    一.主要思想 利用正交变换把可能线性相关变量表示的观测数据,转换为由少数几个线性无关变量(主成分)表示的数据.(重构原始特征空间:线性降维) 要尽可能保留原始数据中的信息,两个思路:最大投影方差.最小 ...

  10. 【0725 | Day 1】计算机编程/计算机组成原理/计算机操作系统

    什么是编程 编程语言:人与计算机交流的手段 编程:通过编程语言编写文件 学习编程的目的:让计算机代替人力,为我们服务 计算机组成原理 计算机由五大部分组成:控制器.运算器.存储器.输入设备.输出设备. ...