p1471方差

一个裸的很的推式子、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#include<algorithm>

#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define ls i<<1
#define rs i<<1|1

using namespace std;

const int maxn=100100;

int n,m;
double a[maxn];

struct tree{
int l,r;
int chg;
double add,ssum,nsum;
}t[maxn*4];

struct st{

inline void pushdown(int i)
{
if(t[i].chg)
{
t[ls].add+=t[i].add;
t[rs].add+=t[i].add;
t[ls].ssum+=t[i].add*t[i].add*(t[ls].r-t[ls].l+1)+2*t[i].add*t[ls].nsum;
t[rs].ssum+=t[i].add*t[i].add*(t[rs].r-t[rs].l+1)+2*t[i].add*t[rs].nsum;
t[ls].nsum+=t[i].add*(t[ls].r-t[ls].l+1);
t[rs].nsum+=t[i].add*(t[rs].r-t[rs].l+1);
t[i].add=0;
t[i].chg=0;
t[ls].chg=1;
t[rs].chg=1;
}
}

inline void pushup(int i)
{
t[i].ssum=t[ls].ssum+t[rs].ssum;
t[i].nsum=t[ls].nsum+t[rs].nsum;
}

void build_tree(int i,int l,int r)
{
t[i].l=l;
t[i].chg=0;
t[i].r=r;
t[i].add=0;
t[i].nsum=0;
t[i].ssum=0;
if(l==r)
{
t[i].nsum=a[l];
t[i].ssum=a[l]*a[l];
return ;
}
int mid=(l+r)>>1;
build_tree(lson);
build_tree(rson);
pushup(i);
}

void change_tree(int i,int l,int r,int x,int y,double target)
{
if(l>=x&&r<=y)
{
t[i].ssum+=target*target*(r-l+1)+2*t[i].nsum*target;
t[i].nsum+=target*(r-l+1);
t[i].add+=target;
t[i].chg=1;
return ;
}
pushdown(i);
int mid=(l+r)>>1;
if(x<=mid) change_tree(lson,x,y,target);
if(y>mid) change_tree(rson,x,y,target);
pushup(i);
}

double ask_nsum_tree(int i,int l,int r,int x,int y)
{
if(l>=x&&r<=y) return t[i].nsum;
pushdown(i);
int mid=(l+r)/2;
double ans=0.0;
if(x<=mid) ans+=ask_nsum_tree(lson,x,y);
if(y>mid) ans+=ask_nsum_tree(rson,x,y);
return ans;
}

double ask_ssum_tree(int i,int l,int r,int x,int y)
{
if(l>=x&&r<=y) return t[i].ssum;
pushdown(i);
int mid=(l+r)>>1;
double ans=0.0;
if(x<=mid) ans+=ask_ssum_tree(lson,x,y);
if(y>mid) ans+=ask_ssum_tree(rson,x,y);
return ans;
}

}st;

int main()
{
ios::sync_with_stdio(false);
register int i,j;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
scanf("%lf",&a[i]);
st.build_tree(1,1,n);
int opt,l,r;
double t;
for(i=1;i<=m;i++)
{
scanf("%d",&opt);
if(opt==1)
{
scanf("%d %d %lf",&l,&r,&t);
st.change_tree(1,1,n,l,r,t);
}
if(opt==2)
{
scanf("%d %d",&l,&r);
printf("%.4lf\n",st.ask_nsum_tree(1,1,n,l,r)/(r-l+1));
}
if(opt==3)
{
scanf("%d %d",&l,&r);
double t1=st.ask_ssum_tree(1,1,n,l,r);
double t2=st.ask_nsum_tree(1,1,n,l,r);
double t3=2*t2*t2/(r-l+1);
double t4=t2*t2/(r-l+1);
printf("%.4lf\n",(t1-t3+t4 )/(r-l+1));
}
}
return 0;
}
Author: MMMsc0.618
Link: http://yoursite.com/2020/05/08/p1471方差/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付宝

Comment