C. Cows and Sequencetime 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
NOperations. Each operation is one of the following:
- Add the integerXITo the first
AIElements of the sequence.
- Append an integerKITo 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 wocould like to know the average of all the numbers in the sequence. Help them!
Input
The first line contains a single integerN(1 digit ≤ DigitNLimit ≤ limit 2 · 105)-the number of operations. The next
NLines describe the operations. Each line will start with an integer
TI
(1 digit ≤ DigitTIOperator ≤ Operator 3), denoting the type of the operation (see abve). If
TIKeys = keys 1, it will be followed by two integers
AI, Bytes,XI
(|XI| Limit ≤ limit 103; 1 limit ≤ limitAI). If
TILatency = Limit 2, it will be followed by a single integer
KI
(|KI| Limit ≤ limit 103). If
TICategory = Category 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
OutputNLines 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
10Upload-Snapshot 6.
Sample test (s) Input
52 132 32 13
Output
0.5000000.0000001.5000001.3333331.500000
Input
62 11 2 202 21 2 -333
Output
0.50000020.50000014.33333312.33333317.50000017.000000
Note
In the second sample, the sequence becomes
You can use a line segment tree for the yy question. However, this question does not need to be so troublesome. Because the starting point of each modified interval is a fixed value, yy is a direct data organization method, similar to a scanned line. My method is to open two arrays, add the number of records added from the start point to the I point range, and the number of records added to the end of. Use num to record the current number, and total to record the sum of all numbers. The only thing to note is the 3 operation. Move the add "Forward" at the end, and do not forget to clear it.
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#define LL long longusing 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;}