Codeforces Round #174 (Div. 2) Cows and Sequence(线段树)

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

线段树成段更新

AC Code

  1 #include <iostream>
  2 #include <fstream>
  3 #include <string>
  4 #include <set>
  5 #include <map>
  6 #include <vector>
  7 #include <stack>
  8 #include <queue>
  9 #include <cmath>
 10 #include <cstdio>
 11 #include <cstring>
 12 #include <algorithm>
 13 #include <utility>
 14 using namespace std;
 15 #define ll long long
 16 #define cti const int
 17 #define ctll const long long
 18 #define dg(i) cout << '*' << i << endl;
 19 
 20 #define ls rt<<1
 21 #define rs rt<<1|1
 22 #define maxn 200002
 23 #define nl 2100000000  
 24 /*nl是“null”,当某一结点的sum为nl时表明此位置上没有数,这个位置只是预留给后面将要
 25 加入序列的数的.nl的值不能随便设置,它的值必须不可能在序列中出现,这里设为2100000000,
 26 这样就算200000次操作都是加1000也不会达到nl,因而nl不会出现*/
 27 int cnt;  //记录元素总数
 28 int n, t, a;
 29 ll k, x;
 30 struct Node
 31 {
 32     int l, r;
 33     ll inc, sum;  //inc是“increment”,记录区间的增量
 34 }tree[maxn<<2];
 35 
 36 void Build(int left, int right, int rt)
 37 {
 38     tree[rt].l = left;
 39     tree[rt].r = right;
 40     tree[rt].sum = nl;
 41     tree[rt].inc = 0;
 42     if(left == right)
 43     {
 44         if(left == 1) tree[rt].sum = 0;
 45         return ;
 46     }
 47     int mid = (left + right) >> 1;
 48     Build(left, mid, ls);
 49     Build(mid + 1, right, rs);
 50     tree[rt].sum = tree[ls].sum * (ll)(tree[ls].sum != nl) + tree[rs].sum * (ll)(tree[rs].sum != nl);
 51     return ;
 52 }
 53 
 54 void Update(int from, int to, ll d, int rt)
 55 {
 56     if(from <= tree[rt].l && tree[rt].r <= to)
 57     {
 58         if(t == 1)
 59         {
 60             tree[rt].inc += d;
 61             tree[rt].sum += ((ll)(tree[rt].r - tree[rt].l + 1) * d);
 62         }
 63         else if(t == 2)
 64         {
 65             tree[rt].sum = d;
 66         }
 67         else tree[rt].sum = nl;
 68         return ;
 69     }
 70     if(tree[rt].inc)
 71     {
 72         ll w = tree[rt].inc;  //创建w纯粹为了敲代码和阅读代码方便
 73         tree[ls].inc += w;
 74         tree[rs].inc += w;
 75         tree[ls].sum += ((ll)(tree[ls].r - tree[ls].l + 1) * w);
 76         tree[rs].sum += ((ll)(tree[rs].r - tree[rs].l + 1) * w);
 77         tree[rt].inc = 0;
 78     }
 79     int mid = (tree[rt].l + tree[rt].r) >> 1;
 80     if(from <= mid) Update(from, to, d, ls);
 81     if(to > mid) Update(from, to, d, rs);
 82     tree[rt].sum = tree[ls].sum * (ll)(tree[ls].sum != nl) + tree[rs].sum * (ll)(tree[rs].sum != nl);
 83     return ;
 84 }
 85 
 86 int main()
 87 {
 88     while(scanf("%d", &n) != EOF)
 89     {
 90         cnt = 1;
 91         Build(1, n + 1, 1);
 92         while(n--)
 93         {
 94             scanf("%d", &t);
 95             if(t == 1)
 96             {
 97                 scanf("%d %I64d", &a, &x);
 98                 Update(1, a, x, 1);
 99             }
100             else if(t == 2)
101             {
102                 cnt++;
103                 scanf("%I64d", &k);
104                 Update(cnt, cnt, k, 1);
105             }
106             else if(cnt > 1)
107             {
108                 Update(cnt, cnt, 0, 1);
109                 cnt--;
110             }
111             printf("%.6lf\n", 1.0 * tree[1].sum / cnt);
112         }
113     }
114     return 0;
115 }

 

posted on 2013-03-26 19:35  铁树银花  阅读(320)  评论(0编辑  收藏  举报

导航