题目

https://loj.ac/problem/2005

思路

\[\sum_{L}^{R}{(x_i-x)^{2}}
\]

\[\sum_{L}^{R}{(x_i^2-2*x_i*x+x^{2})}
\]

\[\sum_{L}^{R}{x_i^2}-2*x*\sum_{L}^{R}x_i+(r-l+1)x^{2}
\]

\[\sum_{L}^{R}x_{i}^2-2*\frac{1}{r-l+1}(\sum_{L}^{R}x_i)^2+\frac{1}{r-l+1}*(\sum_{L}^{R}x)^2
\]

\[\sum_{L}^{R}x_{i}*x_{i}-\frac{1}{r-l+1}\sum_{L}^{R}x_i*\sum_{L}^{R}x_i
\]

\[\sum_{L}^{R}(x_i-x)(y_i-y)
\]

\[\sum_{L}^{R}(x_i*y_i-x*y_i-y*x_i+x*y)
\]

\[\sum_{L}^{R}x_i*y_i-\sum_{L}^{R}x_i*y-\sum_{L}^{R}y_i*x+(r-l+1)*x*y
\]

\[\sum_{L}^{R}x_i*y_i-\frac{1}{r-l+1}\sum_{L}^{R}x_i\sum_{L}^{R}*y_i-\frac{1}{r-l+1}\sum_{L}^{R}*x_i\sum_{L}^{R}y_i+(r-l+1)*x*y
\]

\[\sum_{L}^{R}x_i*y_i-\frac{2}{r-l+1}\sum_{L}^{R}x_i\sum_{L}^{R}*y_i+\frac{1}{(r-l+1)}\sum_{L}^{R}x_i*\sum_{L}^{R}y_i
\]

\[\sum_{L}^{R}x_i*y_i-\frac{1}{r-l+1}\sum_{L}^{R}x_i\sum_{L}^{R}*y_i
\]

\[\frac{\sum_{L}^{R}x_i*y_i-\frac{1}{r-l+1}\sum_{L}^{R}x_i\sum_{L}^{R}*y_i}{\sum_{L}^{R}x_{i}*x_{i}-\frac{1}{r-l+1}\sum_{L}^{R}x_i\sum_{L}^{R}x_i}
\]

其实不用这么麻烦的、、

好了,剩下的去维护吧

好吧,我太菜了

要开long doule

代码

#include <bits/stdc++.h>
#define ll long double
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
const int N=2e5+7;
int read() {
int x=0,f=1;char s=getchar();
for (;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
for (;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
return x*f;
}
ll x[N],y[N];
struct node {
int l,r,siz;
ll tot[2],pingfang,chengji;
ll lazy,S,T;
}e[N<<2];
void pushup(int rt) {
e[rt].tot[0]=e[ls].tot[0]+e[rs].tot[0];
e[rt].tot[1]=e[ls].tot[1]+e[rs].tot[1];
e[rt].pingfang=e[ls].pingfang+e[rs].pingfang;
e[rt].chengji=e[ls].chengji+e[rs].chengji;
}
ll calc(int x) {return (ll)x*(x+1)/2;};
ll calc2(int x) {return (ll)x*(x+1)/2*(2*x+1)/3;};
void pushdown(int rt) {
if(e[rt].lazy) {
e[ls].tot[0]=e[ls].tot[1]=calc(e[ls].r)-calc(e[ls].l-1);
e[ls].pingfang=e[ls].chengji=calc2(e[ls].r)-calc2(e[ls].l-1);
e[ls].S=e[ls].T=0;
e[ls].lazy=1; e[rs].tot[0]=e[rs].tot[1]=calc(e[rs].r)-calc(e[rs].l-1);
e[rs].pingfang=e[rs].chengji=calc2(e[rs].r)-calc2(e[rs].l-1);
e[rs].S=e[rs].T=0;
e[rs].lazy=1; e[rt].lazy=0;
}
if(e[rt].S||e[rt].T) {
e[ls].chengji+=e[ls].tot[0]*e[rt].T+e[ls].tot[1]*e[rt].S+e[rt].S*e[rt].T*e[ls].siz;
e[ls].pingfang+=e[ls].tot[0]*2*e[rt].S+e[rt].S*e[rt].S*e[ls].siz;
e[ls].tot[0]+=e[ls].siz*e[rt].S;
e[ls].tot[1]+=e[ls].siz*e[rt].T;
e[ls].S+=e[rt].S;
e[ls].T+=e[rt].T; e[rs].chengji+=e[rs].tot[0]*e[rt].T+e[rs].tot[1]*e[rt].S+e[rt].S*e[rt].T*e[rs].siz;
e[rs].pingfang+=e[rs].tot[0]*2*e[rt].S+e[rt].S*e[rt].S*e[rs].siz;
e[rs].tot[0]+=e[rs].siz*e[rt].S;
e[rs].tot[1]+=e[rs].siz*e[rt].T;
e[rs].S+=e[rt].S;
e[rs].T+=e[rt].T; e[rt].S=e[rt].T=0;
}
}
void build(int l,int r,int rt) {
e[rt].l=l,e[rt].r=r,e[rt].siz=r-l+1;
if(l==r) {
e[rt].tot[0]=x[l];
e[rt].tot[1]=y[l];
e[rt].pingfang=x[l]*x[l];
e[rt].chengji=x[l]*y[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,ls);
build(mid+1,r,rs);
pushup(rt);
}
void modify_1(int L,int R,ll S,ll T,int rt) {
if(L<=e[rt].l&&e[rt].r<=R) {
e[rt].chengji+=e[rt].tot[0]*T+e[rt].tot[1]*S+S*T*e[rt].siz;
e[rt].pingfang+=e[rt].tot[0]*2*S+S*S*e[rt].siz;
e[rt].tot[0]+=e[rt].siz*S;
e[rt].tot[1]+=e[rt].siz*T;
e[rt].S+=S;
e[rt].T+=T;
return;
}
pushdown(rt);
int mid=(e[rt].l+e[rt].r)>>1;
if(L<=mid) modify_1(L,R,S,T,ls);
if(R>mid) modify_1(L,R,S,T,rs);
pushup(rt);
}
void modify_2(int L,int R,int rt) {
if(L<=e[rt].l&&e[rt].r<=R) {
e[rt].tot[0]=e[rt].tot[1]=calc(e[rt].r)-calc(e[rt].l-1);
e[rt].pingfang=e[rt].chengji=calc2(e[rt].r)-calc2(e[rt].l-1);
e[rt].S=e[rt].T=0;
e[rt].lazy=1;
return;
}
pushdown(rt);
int mid=(e[rt].l+e[rt].r)>>1;
if(L<=mid) modify_2(L,R,ls);
if(R>mid) modify_2(L,R,rs);
pushup(rt);
}
ll query(int L,int R,int opt,int rt) {
if(L<=e[rt].l&&e[rt].r<=R) {
if(opt==0) return e[rt].tot[0];
if(opt==1) return e[rt].tot[1];
if(opt==2) return e[rt].pingfang;
if(opt==3) return e[rt].chengji;
}
pushdown(rt);
int mid=(e[rt].l+e[rt].r)>>1;
ll ans=0;
if(L<=mid) ans+=query(L,R,opt,ls);
if(R>mid) ans+=query(L,R,opt,rs);
pushup(rt);
return ans;
}
int main() {
int n=read(),m=read();
for(int i=1;i<=n;++i) x[i]=read();
for(int i=1;i<=n;++i) y[i]=read();
build(1,n,1);
while(m--) {
int opt=read(),L=read(),R=read();
if(opt==1) {
ll tot_x=query(L,R,0,1);
ll tot_y=query(L,R,1,1);
ll tot_x_x=query(L,R,2,1);
ll tot_x_y=query(L,R,3,1);
long double a=tot_x_y-tot_x*tot_y/(R-L+1);
long double b=tot_x_x-tot_x*tot_x/(R-L+1);
printf("%.10Lf\n",(long double)a/b);
} else if(opt==2) {
ll S=read(),T=read();
modify_1(L,R,(ll)S,(ll)T,1);
} else {
ll S=read(),T=read();
modify_2(L,R,1);
modify_1(L,R,(ll)S,(ll)T,1);
}
}
return 0;
}

SDOI2017相关分析 线段树的更多相关文章

  1. [Sdoi2017]相关分析 [线段树]

    [Sdoi2017]相关分析 题意:沙茶线段树 md其实我考场上还剩一个多小时写了40分 其实当时写正解也可以吧1h也就写完了不过还要拍一下 正解代码比40分短2333 #include <io ...

  2. 【BZOJ4821】[Sdoi2017]相关分析 线段树

    [BZOJ4821][Sdoi2017]相关分析 Description Frank对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等. ...

  3. BZOJ 4821 [Sdoi2017]相关分析 ——线段树

    打开题面,看到许多$\sum$ woc,好神啊,SDOI好强啊 然后展开之后,woc,SDOI好弱啊,怎么T3出个线段树裸题啊. 最后写代码的时候,woc,SDOI怎么出个这么码农的题啊,怎么调啊. ...

  4. 洛谷P3707 [SDOI2017]相关分析(线段树)

    题目描述 Frank对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等. Frank不仅喜欢观测,还喜欢分析观测到的数据.他经常分析两个 ...

  5. BZOJ 4821: [Sdoi2017]相关分析 线段树 + 卡精

    考试的时候切掉了,然而卡精 + 有一个地方忘开 $long long$,完美挂掉 $50$pts. 把式子化简一下,然后直接拿线段树来维护即可. Code: // luogu-judger-enabl ...

  6. BZOJ.4821.[SDOI2017]相关分析(线段树)

    BZOJ LOJ 洛谷 恶心的拆式子..然后就是要维护\(\sum x_i,\ \sum y_i,\ \sum x_iy_i,\ \sum x_i^2\). 操作三可以看成初始化一遍,然后同操作二. ...

  7. 【BZOJ4821】【SDOI2017】相关分析 [线段树]

    相关分析 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Frank对天文学非常感兴趣,他经 ...

  8. luogu3707 相关分析 (线段树)

    把式子展开以后会发现,可以用线段树维护$x,y,x*y,x^2$分别的区间和 然后操作有区间加和区间修改 这个pushdown的时候,如果改和加的标记同时存在,那一定是先改再加,要不然加的标记已经被清 ...

  9. LOJ #2005. 「SDOI2017」相关分析 线段树维护回归直线方程

    题目描述 \(Frank\) 对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等. \(Frank\) 不仅喜欢观测,还喜欢分析观测到的 ...

随机推荐

  1. Rpgmakermv(15) PH任务插件

    插件介绍 一个用来简单显示任务阶段的任务书 使用方法 插件安装 下载js文件放置到游戏目录/plugins目录下.打开插件管理器,选择PH_QuestBook.js并开启. 插件参数 Show in ...

  2. Vue系列之 => 结合ajax完成列表增删查

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Cipher

    Description Bob and Alice started to use a brand-new encoding scheme. Surprisingly it is not a Publi ...

  4. 【Redis学习之七】Redis持久化

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 redis-2.8.18 什么是持久化? 将数据从掉电易失的 ...

  5. 决策树算法——ID3

    决策树算法是一种有监督的分类学习算法.利用经验数据建立最优分类树,再用分类树预测未知数据. 例子:利用学生上课与作业状态预测考试成绩. 上述例子包含两个可以观测的属性:上课是否认真,作业是否认真,并以 ...

  6. Linux基础命令---arp

    arp arp指令用来管理系统的arp缓冲区,可以显示.删除.添加静态mac地址.ARP以各种方式操纵内核的ARP缓存.主要选项是清除地址映射项并手动设置.为了调试目的,ARP程序还允许对ARP缓存进 ...

  7. 转:Image与byte之间互转

    #region<Image 与 byte之间互转> /// <summary> /// 将一个byte转换成一个Bitmap对象 /// </summary> // ...

  8. android本地数据库,微信数据库WCDB for Android 使用实例

    android本地数据库,微信数据库WCDB for Android 使用实例 Home · Tencent/wcdb Wikihttps://github.com/Tencent/wcdb/wiki ...

  9. sqlyog连接Linux上的mysql报错误号码2013,错误号码1130的解决办法

    sqlyog连接Linux上的mysql报错误号码2013,错误号码1130的解决办法 1.报错误号码2013,可能是端口号不是默认的3306,需要改成对应的,检查命令是: [root@host et ...

  10. AtCoder Beginner Contest 066 B - ss

    题目链接:http://abc066.contest.atcoder.jp/tasks/abc066_b Time limit : 2sec / Memory limit : 256MB Score ...