4821: [Sdoi2017]相关分析

来源:互联网 发布:贵州大数据平台 编辑:程序博客网 时间:2024/06/05 07:05

4821: [Sdoi2017]相关分析

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 191 Solved: 29
[Submit][Status][Discuss]
Description

Frank对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度、颜色等等,进而估算出
星星的距离,半径等等。Frank不仅喜欢观测,还喜欢分析观测到的数据。他经常分析两个参数之间(比如亮度和
半径)是否存在某种关系。现在Frank要分析参数X与Y之间的关系。他有n组观测数据,第i组观测数据记录了x_i和
y_i。他需要一下几种操作1 L,R:用直线拟合第L组到底R组观测数据。用xx表示这些观测数据中x的平均数,用yy
表示这些观测数据中y的平均数,即
xx=Σx_i/(R-L+1)(L<=i<=R)
yy=Σy_i/(R-L+1)(L<=i<=R)
如果直线方程是y=ax+b,那么a应当这样计算:
a=(Σ(x_i-xx)(y_i-yy))/(Σ(x_i-xx)(x_i-xx)) (L<=i<=R)
你需要帮助Frank计算a。
2 L,R,S,T:
Frank发现测量数据第L组到底R组数据有误差,对每个i满足L <= i <= R,x_i需要加上S,y_i需要加上T。
3 L,R,S,T:
Frank发现第L组到第R组数据需要修改,对于每个i满足L <= i <= R,x_i需要修改为(S+i),y_i需要修改为(T+i)。
Input

第一行两个数n,m,表示观测数据组数和操作次数。
接下来一行n个数,第i个数是x_i。
接下来一行n个数,第i个数是y_i。
接下来m行,表示操作,格式见题目描述。
1<=n,m<=10^5,0<=|S|,|T|,|x_i|,|y_i|<=10^5
保证1操作不会出现分母为0的情况。
Output

对于每个1操作,输出一行,表示直线斜率a。
选手输出与标准输出的绝对误差不超过10^-5即为正确。
Sample Input

3 5

1 2 3

1 2 3

1 1 3

2 2 3 -3 2

1 1 2

3 1 2 2 1

1 1 3
Sample Output

1.0000000000

-1.5000000000

-0.6153846154
HINT

请不要提交,尚无SPJ

Source

鸣谢infinityedge上传

[Submit][Status][Discuss]

要了下数据发现极限数据的确挺极限的。。。
貌似是要用另外一个公式来维护不然就溢出了。。。
a^=Ri=L(xix¯)(yiy¯)Ri=L(xix¯)2=(Ri=Lxiyi)nx¯y¯(Ri=Lx2i)nx¯2
于是我们只要维护区间内xyxy的和就行了
推推公式大力线段树一波就好
没有SPJ…
存储要用long double,不然溢出了…
中间计算还是要用long double,不然还是溢出…

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<vector>#include<queue>#include<set>#include<map>#include<stack>#include<bitset>#include<ext/pb_ds/priority_queue.hpp>#define max(a,b) ((a) > (b) ? (a) : (b))using namespace std;typedef long double DB;typedef long long LL;const int T = 4;const LL INF = 1E16;const int maxn = 1E5 + 10;int n,m;LL a[maxn],b[maxn],s0[maxn],s1[maxn];DB sx[maxn*T],sy[maxn*T],sxy[maxn*T],sm[maxn*T],ax[maxn*T],cx[maxn*T],ay[maxn*T],cy[maxn*T];DB SX,SY,SXY,SM,X,Y;inline void maintain(int o){    sx[o] = sx[o<<1] + sx[o<<1|1];    sy[o] = sy[o<<1] + sy[o<<1|1];    sm[o] = sm[o<<1] + sm[o<<1|1];    sxy[o] = sxy[o<<1] + sxy[o<<1|1];}inline void Build(int o,int l,int r){    ax[o] = cx[o] = INF;    if (l == r)    {        sx[o] = a[l]; sy[o] = b[l];        sxy[o] = 1LL * a[l] * b[l];        sm[o] = 1LL * a[l] * a[l]; return;    }    int mid = l + r >> 1;    Build(o<<1,l,mid); Build(o<<1|1,mid+1,r);    maintain(o);}inline void push_add(int o,LL AX,LL AY){    if (ax[o] == INF && cx[o] == INF) ax[o] = AX,ay[o] = AY;    else if (ax[o] == INF) cx[o] += AX,cy[o] += AY;    else ax[o] += AX,ay[o] += AY;}inline void push_cov(int o,LL CX,LL CY){    ax[o] = INF;    cx[o] = CX; cy[o] = CY;}inline void pushdown(int o,int l,int r){    if (ax[o] == INF && cx[o] == INF) return;    LL len = r - l + 1;    int lc = (o << 1),rc = (o << 1 | 1),mid = l + r >> 1;    if (ax[o] != INF)    {        sm[o] += ax[o] * ax[o] * len + 2LL * ax[o] * sx[o];        sxy[o] += ax[o] * ay[o] * len + ay[o] * sx[o] + ax[o] * sy[o];        sx[o] += len * ax[o]; sy[o] += len * ay[o];        if (l < r) push_add(lc,ax[o],ay[o]),push_add(rc,ax[o],ay[o]);    }    else    {        sm[o] = cx[o] * cx[o] * len + 2LL * cx[o] * s0[len - 1] + s1[len - 1];        sxy[o] = cx[o] * cy[o] * len + (cx[o] + cy[o]) * s0[len - 1] + s1[len - 1];        sx[o] = (cx[o] + cx[o] + len - 1LL) * len / 2LL;        sy[o] = (cy[o] + cy[o] + len - 1LL) * len / 2LL; LL D = mid - l + 1LL;        if (l < r) push_cov(lc,cx[o],cy[o]),push_cov(rc,cx[o] + D,cy[o] + D);    }    ax[o] = cx[o] = INF;}inline void Query(int o,int l,int r,int ql,int qr){    pushdown(o,l,r);    if (ql <= l && r <= qr)    {        SXY += sxy[o]; SM += sm[o];        SX += sx[o]; SY += sy[o]; return;    }    int mid = l + r >> 1;    if (ql <= mid) Query(o<<1,l,mid,ql,qr);    if (qr > mid) Query(o<<1|1,mid+1,r,ql,qr);}inline void Modify(int o,int l,int r,int ql,int qr,LL AX,LL AY){    if (ql <= l && r <= qr)    {        if (ax[o] == INF && cx[o] == INF) ax[o] = AX,ay[o] = AY;        else if (ax[o] == INF) cx[o] += AX,cy[o] += AY;        else ax[o] += AX,ay[o] += AY;        pushdown(o,l,r); return;    }    int mid = l + r >> 1; pushdown(o,l,r);    if (ql <= mid) Modify(o<<1,l,mid,ql,qr,AX,AY); else pushdown(o<<1,l,mid);    if (qr > mid) Modify(o<<1|1,mid+1,r,ql,qr,AX,AY); else pushdown(o<<1|1,mid+1,r);    maintain(o);}inline void Cover(int o,int l,int r,int ql,int qr,LL CX,LL CY){    if (ql <= l && r <= qr)    {        ax[o] = INF; cx[o] = CX;        cy[o] = CY; pushdown(o,l,r); return;    }    int mid = l + r >> 1; pushdown(o,l,r);    if (ql <= mid && mid < qr)    {        Cover(o<<1,l,mid,ql,qr,CX,CY);        LL D = mid - max(l,ql) + 1;        Cover(o<<1|1,mid+1,r,ql,qr,CX + D,CY + D);    }    else    {        if (ql <= mid) Cover(o<<1,l,mid,ql,qr,CX,CY); else pushdown(o<<1,l,mid);        if (qr > mid) Cover(o<<1|1,mid+1,r,ql,qr,CX,CY); else pushdown(o<<1|1,mid+1,r);      }    maintain(o);}inline int getint(){    char ch = getchar(); int ret = 0,a = 1;    while (ch < '0' || '9' < ch)    {        if (ch == '-') a = -1;        ch = getchar();    }    while ('0' <= ch && ch <= '9')        ret = ret * 10 + ch - '0',ch = getchar();    return ret * a;}int main(){    #ifdef DMC        freopen("DMC.txt","r",stdin);        freopen("1.out","w",stdout);    #endif    n = getint(); m = getint();    for (int i = 1; i <= n; i++) a[i] = getint();    for (int i = 1; i <= n; i++) b[i] = getint();    for (int i = 1; i < maxn; i++)    {        s0[i] = 1LL * i + s0[i - 1];        s1[i] = 1LL * i * i + s1[i - 1];    }    Build(1,1,n);    while (m--)    {        int typ,l,r; LL s,t;        typ = getint(); l = getint(); r = getint();        if (typ == 1)        {            SX = SY = SXY = SM = 0;            Query(1,1,n,l,r); X = SX; Y = SY;            X /= (DB)(r - l + 1); Y /= (DB)(r - l + 1);            DB A = SXY,B = SM;            A -= (DB)(r - l + 1) * X * Y;            B -= (DB)(r - l + 1) * X * X;            printf("%.10lf\n",(double)(A / B));         }        else if (typ == 2)        {            s = getint(); t = getint();            Modify(1,1,n,l,r,s,t);        }        else if (typ == 3)        {            s = getint(); t = getint();            Cover(1,1,n,l,r,s + l,t + l);        }    }    return 0;}
0 0