CF 174(div2) C

本文介绍了一个涉及序列操作的问题,包括添加、删除元素及求平均值等操作,并提供了一种高效解决方案,利用数组记录操作以便快速更新序列状态。
C. Cows and Sequence
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following:

  1. Add the integer xi to the first ai elements of the sequence.
  2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1)
  3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence.

After each operation, the cows would like to know the average of all the numbers in the sequence. Help them!

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything.

It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence.

Output

Output n lines each containing the average of the numbers in the sequence after the corresponding operation.

The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.

Sample test(s)
Input
5
2 1
3
2 3
2 1
3
Output
0.500000
0.000000
1.500000
1.333333
1.500000
Input
6
2 1
1 2 20
2 2
1 2 -3
3
3
Output
0.500000
20.500000
14.333333
12.333333
17.500000
17.000000
Note

In the second sample, the sequence becomes

yy题,感觉用线段树也可以,不过这题不用这么麻烦,因为每次修改的区间的起点是定值,所以yy一种直接的数据组织方式,类似扫描线。我的方法是开两个数组,add记录从起点到第i个点范围内加了多少,a记录加入到尾部的那些数。用num记录目前有多少个数,total记录所有数的和。唯一要注意的是3操作,把末尾add“挪”向前一个,然后别忘了把它清零。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#define LL long long
using namespace std;
const int maxn = 200000  + 5;

LL add[maxn],a[maxn];

int main(){
    int n;
    while(cin >> n){
        LL total = 0;
        int num = 1;
        a[1] = 0;
        memset(add,0,sizeof(add));
        double ans;
        for(int i = 0;i < n;i++){
            LL op,x,y;
            cin >> op;
            if(op == 1){
                cin >> x >> y;
                total += x*y;
                add[x] += y;
            }
            else if(op == 2){
                cin >> x;
                total += x;
                num++;
                a[num] = x;
            }
            else{
                if(num == 1) continue;
                total = total - a[num] - add[num];
                add[num-1] += add[num];
                add[num] = 0;
                num--;
            }
            ans = (double)total/num;
            printf("%.6lf\n",ans);
        }
    }
    return 0;
}

### 如何设置 `div` 元素的左边框样式或调整其左侧布局 #### 方法一:设置边框样式 要为 `div` 元素添加左边框,可以使用 CSS 的 `border-left` 属性。此属性支持多个参数来定义边框的颜色、宽度和样式: ```css #d1 { border-left: 2px solid red; /* 左边框颜色为红色,宽度为2px,样式为实线 */ } ``` 如果需要进一步美化,还可以结合其他属性一起使用,比如圆角效果或者阴影[^1]。 --- #### 方法二:通过外边距(`margin-left`)调整位置 为了让 `div` 元素整体向右移动,从而改变它的左侧布局关系,可以为其指定一个正数作为左外边距值: ```css #d1 { margin-left: 50px; /* 向右偏移50像素 */ } ``` 这种方法不会影响内部结构,只是单纯改变了与其他兄弟节点之间的水平间距[^1]。 --- #### 方法三:利用浮动特性定位 当涉及到复杂的多栏排列时,“浮动”是一个非常有用的工具。例如下面的例子展示了两个并列显示的小方块是如何创建出来的: ```html <style> #container { overflow:hidden; } /* 清除子元素浮动带来的塌陷问题 */ .left-block { float:left; width:8em; height:4em; background:#f9c;} .right-block { float:right; width:8em; height:4em; background:#cf9;} </style> <div id="container"> <div class="left-block"></div> <div class="right-block"></div> </div> ``` 注意这里的 `.left-block` 使用了 `float:left`, 这使得它能够紧贴容器的最左侧边缘[^1]。 --- #### 方法四:绝对定位精确控制 另一种强大的手段就是运用相对/绝对定位组合策略来进行精确定位操作: ```css .parent-div{ position:relative; } .child-div{ position:absolute; top:20%;/*距离顶部百分比*/ left:30%;/*距离左边百分比*/ border-left:dashed black 3px;/*虚线型黑色边界线宽3px*/ padding-left:.5in;/*增加一些额外空间防止文字碰到线条*/ color:white;font-size:x-large; } ``` 这样就可以无视常规文档流的影响自由安排各个部分的位置了[^2]。 --- #### 方法五:清除浮动干扰正常渲染流程 有时候我们会发现设置了某些特殊样式的区块之后整个页面都变得混乱不堪起来,这很可能是因为前面提到了未处理好的浮动静态所引起的现象.解决办法之一就是在合适的地方加入clearfix hack : ```css .clearfix::after { content:""; display:block; clear:both; visibility:hidden; line-height:0; font-size:0; height:0; } ``` 然后给相应的父级标签加上这个类名即可恢复正常秩序[^3]. --- ### 总结 无论是简单的装饰作用还是复杂的空间规划考虑,掌握好基础CSS技巧总能帮助我们轻松应对各种场景下的需求.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值