样例过了,下载第一组数据第二个询问方差有问题。 求助,不知道错在哪里。
下面是第一组数据:
in
8 15
8.46 6.03 3.73 0.32 7.43 3.71 8.04 8.22
3 1 8
1 2 8 -2.7566713364794850E+0000
1 2 8 2.1308819339610636E+0000
1 1 6 1.5912831262685359E+0000
1 1 8 -2.7779214559122920E+0000
1 1 8 -6.5134523715823889E-0001
3 2 8
1 1 6 -8.5440817382186651E-0001
3 2 8
2 2 8
3 2 7
1 3 8 -1.8737916438840330E+0000
1 1 7 2.5193137815222144E+0000
1 3 8 1.2835426828823984E+0000
3 3 8
out
7.4145
5.2609
6.2101
1.8256
6.1810
5.8854
code
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
double a[N];
int n, m;
struct node {
int l, r;
double sum = 0, sqm = 0;
double lazy = 0;
}t[N<<2];
void pushup(int p) {
t[p].sum = t[p<<1].sum + t[p<<1|1].sum;
t[p].sqm = t[p<<1].sqm + t[p<<1|1].sqm;
}
void build(int p, int l, int r) {
t[p].l = l, t[p].r = r;
if (l == r) {
t[p].sum = a[l];
t[p].sqm = a[l] * a[l];
return;
}
int mid = l+r>>1;
build(p<<1, l, mid);
build(p<<1|1, mid+1, r);
pushup(p);
}
void change(int p) {
double k = t[p].lazy;
t[p].sqm += 2*k*t[p].sum + (t[p].r-t[p].l+1)*k*k;
t[p].sum += k * (t[p].r-t[p].l+1);
}
void pushdown(int p) {
if (t[p].lazy) {
t[p<<1].lazy += t[p].lazy;
t[p<<1|1].lazy += t[p].lazy;
change(p<<1);
change(p<<1|1);
t[p].lazy = 0;
}
}
void updata(int p, int L, int R, double k) {
cout << " " << p << " " << L << " " << R << " " << k << endl;
if (L <= t[p].l && t[p].r <= R) {
t[p].lazy += k;
change(p);
return;
}
pushdown(p);
int mid = t[p].l+t[p].r>>1;
if (L <= mid) updata(p<<1, L, R, k);
if (mid < R) updata(p<<1|1, L, R, k);
pushup(p);
}
pair<double, double> query(int p, int L, int R) {
if (L <= t[p].l && t[p].r <= R) {
return {t[p].sum, t[p].sqm};
}
pushdown(p);
int mid = t[p].l+t[p].r>>1;
double sum = 0, sqm = 0;
if (L <= mid) {
pair<double, double> q = query(p<<1, L, R);
sum += q.first;
sqm += q.second;
}
if (mid < R) {
pair<double, double> q = query(p<<1|1, L, R);
sum += q.first;
sqm += q.second;
}
return {sum, sqm};
}
int main() {
// ios::sync_with_stdio(0);
// cin.tie(0);
cin >> n >> m;
for (int i=1; i<=n; i++) cin >> a[i];
build(1, 1, n);
while (m--) {
int o, x, y;
double k;
cin >> o >> x >> y;
if (o == 1) {
cin >> k;
updata(1, x, y, k);
}
if (o == 2) {
printf("%.4lf\n", query(1, x, y).first/(y-x+1));
// cout << fixed << setprecision(4) << query(1, x, y).first/(y-x+1) << endl;
}
if (o == 3) {
pair<double, double> q = query(1, x, y);
double ave = q.first/(y-x+1);
printf("%.4lf\n", q.second/(y-x+1)-ave*ave);
// cout << fixed << setprecision(4) << q.second/(y-x+1)-ave*ave<< endl;
}
}
return 0;
}