CodeForces 283A Cows and Sequence 树状数组

明天就要去GDKOI了好虚怎么办QAQ。

给你一个序列,一开始只有一个0
接下来有n个操作,每个操作是下面3种操作描述的一种

  • 前ai个数全部加上xi
  • 在序列末尾加上ki这个数
  • 序列末尾删去一个数(所以序列的大小会减一)。只有序列里至少有2个数才执行这个操作 。

做完每一步,你需要输出这个序列的和的平均数。

Input

第一行n
接下来n行,每行第一个数为ti(1<=ti<=3),为相应的操作编号
如果 Ti=1 ,则跟着 ai,xi(|xi|<=103;1<=ai)
如果 Ti=2 ,则跟着 ki(|ki|<=103)
如果 Ti=3 ,后面没有任何数。

Output

输出n行,每行为每一次操作后序列每个数字的平均值,保留6位小数。

Sample

Input1

5
2 1
3
2 3
2 1
3

Output1

0.500000 
0.000000 
1.500000 
1.333333 
1.500000

Input2

6 
2 1 
1 2 20 
2 2 
1 2 -3 
3 
3 

Output2

0.500000 
20.500000 
14.333333 
12.333333 
17.500000 
17.000000 

Sample Explanation

Sample 2

{0}{0,1}{20,21}{20,21,2}{17,18,2}{17,18}{17}

Hint

40%数据: n5000
100%数据: 1n2105 ,保证操作合法性。

Solution

然而我只会暴力地打树状数组了。

记得学长曾经讲过不过忘记了QAQ。

大家一定要以我为鉴QAQ,做过的题一定要搞懂QAQ。
以上是考试时候的心情。。
然而。。我的做法和当初学长讲的做法竟然一样。。好开森。

首先,一眼题可看出是一个区间修改区间查询的线段树。
但是一道题就这么完了?学长肯定留下了什么诡计坑大家的(实际上数据水并没有坑lazy线段树)
发现,对于每个操作,我们都只需要输出整个数列的和。
因此不妨考虑直接维护这个和ans和数列长度l。
为了维护这个和,我们需要一种方法快速得到数列的最后一项。实际上还是得快速求得整个数列各项确切值(一直删)。实际上就是单点查询。
而对于数列我们有区间修改的操作。
符合条件的数据结构就是树状数组。

  • 对于操作1,对树状数组区间修改 [1,a]+x 即可,同时维护ans+=a*x。
  • 对于操作2,对树状数组单点修改 c[l]+k 即可,同时维护ans+=k。
  • 对于操作3,如果操作合法,对树状数组单点查询 l 上的值,并单调修改c[l]k即可。
#include <cstdio>
#define FOR(i,j,k) for(i=j;i<=k;i++)
int c[200001], n;
void add(int i, int x) {
    for (; i <= n; i += i & -i) c[i] += x;
}
int sum(int i) {
    int s = 0;
    for (; i; i -= i & -i) s += c[i];
    return s;
}
int main() {
    int i, t, l = 1, a, x, k;
    double ans = 0;
    scanf("%d", &n);
    FOR(i,1,n) {
        scanf("%d", &t);
        if (t == 1) {
            scanf("%d%d", &a, &x);
            add(1, x), add(a + 1, -x);
            ans += a * x;
        } else if (t == 2) {
            scanf("%d", &k); ans += k; ++l;
            add(l, k); add(l + 1, -k);
        } else if (l >= 2) {
            k = sum(l); ans -= k;
            add(l, -k); add(l + 1, k);
            --l;
        }
        printf("%.6lf\n", ans / l);
    }
    return 0;
}

QAQ QAQ,操作3看错成数据保证合法只有在n>=2的情况下才会有操作3,于是全WA,看错题好坑自己啊。。

A. Cows and Sequence

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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:

Add the integer xi to the first ai elements of the sequence.
Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1)
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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值