线段树单点更新+区间更新
http://www.codeforces.com/contest/283/problem/A
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MM = 222222; #define debug puts("wrong") typedef __int64 int64; #define lson rt<<1 #define rson rt<<1|1 int64 N,id; int64 col[(MM<<2)+5]; int64 sum[(MM<<2)+5]; void push_up(int64 rt) { sum[rt]=sum[lson]+sum[rson]; } void push_down(int64 l,int64 r,int64 rt) { int64 mid=(l+r)>>1; if(col[rt]!=0) { col[lson]+=col[rt]; col[rson]+=col[rt]; sum[lson]+=(mid-l+1)*col[rt]; sum[rson]+=(r-mid)*col[rt]; col[rt]=0; } } void build(int64 l,int64 r,int64 rt) { sum[rt] = 0; if(l==r) return; int64 mid=(l+r)>>1; build(l,mid,lson); build(mid+1,r,rson); } void del(int64 p,int64 l,int64 r,int64 rt) { if(l==r) { sum[rt] = 0; return; } push_down(l,r,rt); int64 mid=(l+r)>>1; if(p<=mid) del(p,l,mid,lson); else del(p,mid+1,r,rson); push_up(rt); } void insert(int64 val,int64 p,int64 l,int64 r,int64 rt) { if(l==r) { sum[rt] = val; return; } push_down(l,r,rt); int64 mid=(l+r)>>1; if(p<=mid) insert(val,p,l,mid,lson); else insert(val,p,mid+1,r,rson); push_up(rt); } void update(int64 L,int64 R,int64 val,int64 l,int64 r,int64 rt) { if(L<=l && r<=R) { col[rt]+=val; sum[rt]+=(r-l+1)*val; return; } push_down(l,r,rt); int64 mid=(l+r)>>1; if(L<=mid) update(L,R,val,l,mid,lson); if(R>mid) update(L,R,val,mid+1,r,rson); push_up(rt); } void tran(int p,int l,int r,int rt) { if(l==r) { printf("%d ",sum[rt]); return; } int mid=(l+r)>>1; if(p<=mid) tran(p,l,mid,lson); else tran(p,mid+1,r,rson); } void solve() { int64 i,j,k,op,a,x; id=1; build(1,MM-1,1); memset(col,0,sizeof(col)); for(i=0;i<N;i++) { scanf("%I64d",&op); if(op==1) { scanf("%I64d%I64d",&a,&x); update(1,a,x,1,MM-1,1); printf("%.6lf\n",(double)(sum[1]*1.0)/(1.0*id)); } else if(op==2) { scanf("%I64d",&x); id++; insert(x,id,1,MM-1,1); printf("%.6lf\n",(double)(sum[1]*1.0)/(1.0*id)); } else { del(id,1,MM-1,1); id--; printf("%.6lf\n",(double)(sum[1]*1.0)/(1.0*id)); } } } int main() { while(scanf("%I64d",&N)!=EOF) solve(); return 0; }