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 volatile关键字作用及使用场景
1. volatile关键字的作用:保证了变量的可见性(visibility).被volatile关键字修饰的变量,如果值发生了变更,其他线程立马可见,避免出现脏读的现象.如以下代码片段,isShut ...
- 前端登录jq图形验证码
<!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8" ...
- [转载]MongoDB管理基础
1. 启动和停止MongoDB: 执行mongod命令启动MongoDB服务器.mongod有很多可配置的选项,我们通过mongod --help可以查看所有选项,这里仅介绍一些主要选项: - ...
- android ——Toolbar
Toolbar是我看material design内容的第一个 官方文档:https://developer.android.com/reference/android/support/v7/widg ...
- Java 复制PDF文档的2种方法
本文将介绍通过Java程序来复制PDF页面,包括: 跨文档复制,即从文档1复制到文档2 在同一文档内复制,即从页面A复制到页面B 使用工具:Free Spire.PDF for Java (免费版) ...
- JS闪电打字特效
HTML <div class="page page-thunder-to-text"> <input id="input" type=&qu ...
- css3实现loading效果--当页面加载过程中显示Loading的进度条,全部加载完成之后进度条消失
一个页面等图片资源全部加载完成,会需要很长时间,用户体验会很差,所以我们需要loading来掩盖这个漫长的过程! emmm,定时器?写个定时器还要清除,万一造成内存泄露?定时器之间还会互相影响,呼呼呼 ...
- MongoDB Day 1
创建数据库 db.createCollection("user"); 插入字段 //----insert------- db.user.insert({uid:1, user_co ...
- ASP.NET Core MVC 之区域(Area)
区域(Area)是一个 ASP.NET MVC 功能,用于将相关功能组织为一个单独的命名空间(用于路由)和文件结构(用于视图).使用区域通过向控制器和操作添加 一个路由参数(area)来创建用于路由目 ...
- docker安装到基本使用
记录docker概念,安装及入门日常使用 Docker安装(Linux / Debian) 查看官方文档,在Debian上安装Docker,其他平台在这里查阅,以下均在root用户下操作,省去sudo ...