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 ...
随机推荐
- ASP.NET Core Web Api之JWT刷新Token(三)
前言 如题,本节我们进入JWT最后一节内容,JWT本质上就是从身份认证服务器获取访问令牌,继而对于用户后续可访问受保护资源,但是关键问题是:访问令牌的生命周期到底设置成多久呢?见过一些使用JWT的童鞋 ...
- codeforces 213div(2) 365 A.Good Number 365 B.The Fibonacci Segment
#include <stdio.h> #include <string.h> bool vis[11]; int n, k; bool judge(int x) { memse ...
- Python pip包管理器安装第三方库超时解决方案
一.国内镜像安装 使用方法:pip install --index 镜像网站 第三方库名 二.镜像网站 http://pypi.douban.com/simple/ 豆瓣 http://mirrors ...
- 关于JSP页面的静态包含和动态包含
JSP中有两种包含:静态包含:<%@include file="被包含页面"%> 和 动态包含:<jsp:include page="被包含页面&quo ...
- 调用链系列(1):解读UAVStack中的贪吃蛇
一.背景 对于分布式在线服务,一个请求需要经过多个系统中多个模块,可能多达上百台机器的协作才能完成单次请求.这种场景下单靠人力无法掌握整个请求中各个阶段的性能开销,更无法快速的定位系统中性能瓶颈.当发 ...
- django报错信息解决方法
You have 17 unapplied migration(s). Your project may not work properly until you apply the migration ...
- 使用 .NET CORE 创建 项目模板,模板项目,Template
场景:日常工作中,你可能会碰到需要新建一个全新的解决方案的情况(如公司新起了一个新项目,需要有全新配套的后台程序),如果公司内部基础框架较多.解决方案需要DDD模式等,那么从新起项目到各种依赖引用到能 ...
- Netbeans courier new 乱码问题
Netbeans 默认的字体 monospaced,显示英文的单引号及字体非常的不好看,在网上查了下资料可以变得很好看. 1.仍然保持默认字体 monospaced 2.在Netbeans 的安装目 ...
- 解决MobaXterm-SSH中文乱码问题
一般情况不用修改服务器字符集(linux或unix服务器字符集一般不会设置错误). 1.首先用命令查看当前系统的LANG是什么: >locale LANG=en_US LC_COLLATE=&q ...
- 在github上搭建个人博客并在线更新
换博客比更博还勤的我终于决定写一篇博客搭建教程了.. FAQ Q:\(hexo\)需要本地编译.\(jekyll\)虽然可以直接上传\(md\)..但是如果在github上直接编译也太难受了叭,毕竟不 ...