[Luogu] 相关分析

不想调了

#include <bits/stdc++.h>

const int N = 1e5 + 10;

#define LL long long
#define gc getchar()

int fjs;
struct Node {
    LL xy, x, y, x_2, lenst, lens, lent, lentt;
    LL S_2, T_2/*用于 2 操作*/, S_1, T_1/*用于 3 操作*/;
    bool flag1/*用于 2 操作*/, flag2/*用于 3 操作*/; 
    int size;
    int l, r;
};
Node T[N << 2];
int impx[N], impy[N];

Node operator + (const Node &a, const Node &b) {
    Node ret;
    ret.x = a.x + b.x;
    ret.y = a.y + b.y;
    ret.xy = a.xy + b.xy;
    ret.x_2 = a.x_2 + b.x_2;
    ret.size = a.size + b.size;
    return ret;
}

int n, Ty, Len;

inline LL read() {
    LL x = 0, f = 1; char c = gc;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = gc;}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
    return x * f;
}

#define lson jd << 1
#define rson jd << 1 | 1

void Build_tree(int l, int r, int jd) {
    T[jd].size = (r - l + 1); T[jd].l = l; T[jd].r = r;
    if(l == r) {
        int x_ = impx[l], y_ = impy[l];
        T[jd].x = x_;
        T[jd].y = y_;
        T[jd].xy = x_ * y_;
        T[jd].x_2 = x_ * x_;
        return ;
    }
    int mid = (l + r) >> 1;
    Build_tree(l, mid, lson);
    Build_tree(mid + 1, r, rson);
    T[jd] = T[lson] + T[rson];
}

void Clear_1(int jd) {
    T[jd].flag1 = T[jd].lenst = T[jd].lens = T[jd].lent = T[jd].lentt = 0;
    T[jd].S_1 = T[jd].T_1 = 0;
}

void Clear_2(int jd) {
    T[jd].flag2 = 0;
    T[jd].S_2 = T[jd].T_2 = 0;
}

void Down_1(int jd) {
    T[lson].flag1 = T[rson].flag1 = ++ fjs;
    T[lson].S_1 += T[jd].S_1;
    T[rson].S_1 += T[jd].S_1;
    T[lson].T_1 += T[jd].T_1;
    T[rson].T_1 += T[jd].T_1;
    T[lson].lenst += T[jd].lenst;
    T[rson].lenst += T[jd].lenst;
    T[lson].lens += T[jd].lens;
    T[rson].lens += T[jd].lens;
    T[lson].lent += T[jd].lent;
    T[rson].lent += T[jd].lent;
    T[lson].lentt += T[jd].lentt;
    T[rson].lentt += T[jd].lentt;
    int s = T[jd].S_1, t = T[jd].T_1;
    T[lson].xy += (t * T[lson].x + s * T[lson].y + T[jd].lenst);
    T[lson].x_2 += (2 * t * T[lson].x + T[jd].lentt);
    T[lson].x += T[jd].lens;
    T[lson].y += T[jd].lent;
    T[rson].xy += (t * T[rson].x + s * T[rson].y + T[jd].lenst);
    T[rson].x_2 += (2 * t * T[rson].x + T[jd].lentt);
    T[rson].x += T[jd].lens;
    T[rson].y += T[jd].lent;
    Clear_1(jd);
}

void Work_1(int, int, int, int, int, int, int);

void Down_2(int jd) {
    T[lson].flag2 = T[rson].flag2 = ++ fjs;
    T[lson].S_2 = T[rson].S_2 = T[jd].S_2;
    T[lson].T_2 = T[rson].T_2 = T[jd].T_2;
    int l, r;
    l = T[lson].l, r = T[lson].r;
    T[lson].xy = T[lson].x_2 = (r * (r +  1) * (2 * r + 1) / 6) - (l * (l - 1) * (2 * l - 1)) / 6;
    T[lson].x = T[lson].y = (l + r) / 2 * (r - l + 1);
    Work_1(1, n, 1, l, r, T[jd].S_2, T[jd].T_2);
    l = T[rson].l, r = T[rson].r;
    T[rson].xy = T[rson].x_2 = (r * (r + 1) * (2 * r + 1) / 6) - (l * (l - 1) * (2 * l - 1)) / 6;
    T[rson].x = T[rson].y = (l + r) / 2 * (r - l + 1);
    Work_1(1, n, 1, l, r, T[jd].S_2, T[jd].T_2);
    Clear_2(jd);
}

void Work_1(int l, int r, int jd, int x, int y, int s, int t) {
    if(x <=  l && r <= y) {
        T[jd].flag1 = ++ fjs;
        T[jd].S_1 = s;
        T[jd].T_1 = s;
        T[jd].xy += (t * T[jd].x + s * T[jd].y + Len * s * t);
        T[jd].x_2 += (2 * t * T[jd].x + Len * t * t);
        T[jd].x += Len * s;
        T[jd].y += Len * t;
        T[jd].lenst += Len * s * t;
        T[jd].lens += Len * s;
        T[jd].lent += Len * t;
        T[jd].lentt += Len * t * t;
        return ;
    }
    if(T[jd].flag2 > T[jd].flag1) Down_1(jd), Down_2(jd);
    else Down_2(jd), Down_1(jd);
    int mid = (l + r) >> 1;
    if(x <= mid) Work_1(l, mid, lson, x, y, s, t);
    if(y > mid)  Work_1(mid + 1, r, rson, x, y, s, t);
    T[jd] = T[lson] + T[rson];
}

void Work_2(int l, int r, int jd, int x, int y, int s, int t) {
    if(x <= l && r <= y) {
        T[jd].flag2 = ++ fjs;
        T[jd].S_2 = s;
        T[jd].T_2 = t;
        T[jd].xy = T[jd].x_2 = (r * (r + 1) * (2 * r + 1)) / 6 - (l * (l - 1) * (2 * l - 1)) / 6;
        T[jd].x = T[jd].y = (l + r) / 2 * (r - l + 1);
        Work_1(1, n, 1, l, r, s, t);
        return ;
    }
    if(T[jd].flag2 > T[jd].flag1) Down_1(jd), Down_2(jd);
    else Down_2(jd), Down_1(jd);
    int mid = (l + r) >> 1;
    if(x <= mid) Work_2(l, mid, lson, x, y, s, t);
    if(y > mid)  Work_2(mid + 1, r, rson, x, y, s, t);
    T[jd] = T[lson] + T[rson]; 
}

int Sec_A_xy(int l, int r, int jd, int x, int y) {
    if(x <= l && r <= y) return T[jd].xy;
    if(T[jd].flag2 > T[jd].flag1) Down_1(jd), Down_2(jd);
    else Down_2(jd), Down_1(jd);
    int mid = (l + r) >> 1;
    if(y <= mid) return Sec_A_xy(l, mid, lson, x, y);
    else if(x > mid) return Sec_A_xy(mid + 1, r, rson, x, y);
    else return Sec_A_xy(l, mid, lson, x, y) + Sec_A_xy(mid + 1, r, rson, x, y); 
}

int Sec_A_x(int l, int r, int jd, int x, int y) {
    if(x <= l && r <= y) return T[jd].x;
    if(T[jd].flag2 > T[jd].flag1) Down_1(jd), Down_2(jd);
    else Down_2(jd), Down_1(jd);
    int mid = (l + r) >> 1;
    if(y <= mid) return Sec_A_x(l, mid, lson, x, y);
    else if(x > mid) return Sec_A_x(mid + 1, r, rson, x, y);
    else return Sec_A_x(l, mid, lson, x, y) + Sec_A_x(mid + 1, r, rson, x, y); 
}

int Sec_A_y(int l, int r, int jd, int x, int y) {
    if(x <= l && r <= y) return T[jd].y;
    if(T[jd].flag2 > T[jd].flag1) Down_1(jd), Down_2(jd);
    else Down_2(jd), Down_1(jd);
    int mid = (l + r) >> 1;
    if(y <= mid) return Sec_A_y(l, mid, lson, x, y);
    else if(x > mid) return Sec_A_y(mid + 1, r, rson, x, y);
    else return Sec_A_y(l, mid, lson, x, y) + Sec_A_y(mid + 1, r, rson, x, y); 
}

int Sec_A_x_2(int l, int r, int jd, int x, int y) {
    if(x <= l && r <= y) return T[jd].x_2;
    if(T[jd].flag2 > T[jd].flag1) Down_1(jd), Down_2(jd);
    else Down_2(jd), Down_1(jd);
    int mid = (l + r) >> 1;
    if(y <= mid) return Sec_A_x_2(l, mid, lson, x, y);
    else if(x > mid) return Sec_A_x_2(mid + 1, r, rson, x, y);
    else return Sec_A_x_2(l, mid, lson, x, y) + Sec_A_x_2(mid + 1, r, rson, x, y); 
}

int main() {
    freopen("gg.in", "r", stdin);
    n = read(); Ty = read();
    for(int i = 1; i <= n; i ++) impx[i] = read();
    for(int i = 1; i <= n; i ++) impy[i] = read();
    Build_tree(1, n, 1);
    for(; Ty; Ty --) {
        int opt = read(), l = read(), r = read(); Len = (r - l + 1);
        if(opt == 1) {
            double a = Sec_A_xy(1, n, 1, l, r) * 1.0;
            double b = Sec_A_x(1, n, 1, l, r) * 1.0;
            double c = Sec_A_y(1, n, 1, l, r) * 1.0;
            double d = Sec_A_x_2(1, n, 1, l, r) * 1.0;
            double Ans = (a - b * c / (double)Len) / (double)(1.0 * d - 1.0 * b * b / (double)Len);
            printf("%.10lf\n", Ans);
            continue ;
        }
        int s = read(), t = read();
        if(opt == 2) Work_1(1, n, 1, l, r, s, t);
        else if(opt == 3) Work_2(1, n, 1, l, r, s, t);
    }
    return 0;
}

 

posted @ 2018-05-07 10:55  xayata  阅读(152)  评论(0编辑  收藏  举报