相关分析

【问题描述】

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)。

【输入格式】

第一行两个数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的情况。

【输出格式】

对于每个1操作,输出一行,表示直线斜率a。

选手输出与标准输出的绝对误差不超过10^-5即为正确。

【样例输入】

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

【样例输出】

1.0000000000
-1.5000000000
-0.6153846154


题解:

对于线性回归方程我们把它拆分,x平方和,x权值和,y权值和,xy权值和,x平方和

第二个第三个操作用初中学的完全平方公式拆一拆就好了

记得爆 long long MMP

 #include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
inline void Scan(int &x)
{
char c;
bool o = false;
while(!isdigit(c = getchar()))
if(c == '-')
o = true;
x = c - '';
while(isdigit(c = getchar()))
x = x * + c - '';
if(o) x = -x;
}
const int maxn = 1e5 + ;
const int maxs = maxn << ;
struct ele
{
long long x, y;
double xy, sx;
inline void print()
{
printf("%lf %lf %lf %lf\n", (double) x, (double) y, (double) xy, (double) sx);
}
inline void empty()
{
x = y = xy = sx = ;
}
};
struct tag
{
long long s, t;
inline bool exist()
{
return s || t;
}
inline void empty()
{
s = t = ;
}
};
tag mark[maxs], sign[maxs];
ele ans, add;
ele tr[maxs];
int n, m;
int x[maxn], y[maxn];
double sum[maxn];
inline ele operator + (ele a, ele b)
{
return (ele) {a.x + b.x, a.y + b.y, a.xy + b.xy, a.sx + b.sx};
}
inline tag operator + (tag a, tag b)
{
return (tag) {a.s + b.s, a.t + b.t};
}
void Build(int k, int l, int r)
{
if(l == r)
{
tr[k] = (ele) {x[l], y[l], (double) x[l] * y[l], (double) x[l] * x[l]};
return;
}
int mi = l + r >> ;
int lc = k << , rc = k << | ;
Build(lc, l, mi), Build(rc, mi + , r);
tr[k] = tr[lc] + tr[rc];
}
inline void Add(int k, int n, long long s, long long t)
{
long long x, y;
double xy, sx;
x = n * s;
y = n * t;
xy = (double) s * tr[k].y + (double) t * tr[k].x + (double) n * s * t;
sx = (double) n * s * s + * s * (double) tr[k].x;
add = (ele) {x, y, xy, sx};
tr[k] = tr[k] + add;
mark[k] = mark[k] + (tag) {s, t};
}
inline long long Sum(long long l, long long r, int n)
{
return (l + r) * n / ;
}
inline void Change(int k, double s, double t, int l, int r)
{
int n = r - l + ;
double x, y, xy, sx;
x = Sum(s + l, s + r, n);
y = Sum(t + l, t + r, n);
xy = (double) n * s * t + (double) (s + t) * Sum(l, r, n) + sum[r] - sum[l - ];
sx = (double) n * s * s + sum[r] - sum[l - ] + (double) * s * Sum(l, r, n);
tr[k] = (ele) {x, y, xy, sx};
sign[k] = (tag) {s, t};
mark[k].empty();
}
inline void Down(int k, int l, int r)
{
int lc = k << , rc = k << | ;
int mi = l + r >> ;
double s, t;
if(sign[k].exist())
{
s = sign[k].s, t = sign[k].t;
Change(lc, s, t, l, mi), Change(rc, s, t, mi + , r);
sign[k].empty();
}
if(mark[k].exist())
{
s = mark[k].s, t = mark[k].t;
Add(lc, mi - l + , s, t), Add(rc, r - mi, s, t);
mark[k].empty();
}
}
void Query(int k, int l, int r, int x, int y)
{
if(x <= l && r <= y)
{
ans = ans + tr[k];
return;
}
Down(k, l, r);
int mi = l + r >> ;
if(x <= mi) Query(k << , l, mi, x, y);
if(y > mi) Query(k << | , mi + , r, x, y);
}
void Insert(int k, int l, int r, int x, int y, int s, int t)
{
if(x <= l && r <= y)
{
Add(k, r - l + , s, t);
return;
}
Down(k, l, r);
int mi = l + r >> ;
int lc = k << , rc = k << | ;
if(x <= mi) Insert(lc, l, mi, x, y, s, t);
if(y > mi) Insert(rc, mi + , r, x, y, s, t);
tr[k] = tr[lc] + tr[rc];
}
void Modify(int k, int l, int r, int x, int y, int s, int t)
{
if(x <= l && r <= y)
{
Change(k, s, t, l, r);
return;
}
Down(k, l, r);
int mi = l + r >> ;
int lc = k << , rc = k << | ;
if(x <= mi) Modify(lc, l, mi, x, y, s, t);
if(y > mi) Modify(rc, mi + , r, x, y, s, t);
tr[k] = tr[lc] + tr[rc];
}
int main()
{
Scan(n), Scan(m);
for(int i = ; i <= n; ++i) Scan(x[i]);
for(int i = ; i <= n; ++i) Scan(y[i]);
for(int i = ; i <= n; ++i) sum[i] = sum[i - ] + (double) i * i;
Build(, , n);
int o, x, y, s, t;
int len;
long long meanx;
long double up, down, meany;
while(m--)
{
Scan(o), Scan(x), Scan(y);
switch(o)
{
case :
{
ans.empty();
Query(, , n, x, y);
len = y - x + ;
meanx = ans.x;
meany = (double) ans.y / len;
up = ans.xy - meanx * meany;
down = ans.sx - (double) meanx * meanx / len;
double answer = up / down;
printf("%.10lf\n", answer);
break;
}
case :
{
Scan(s), Scan(t);
Insert(, , n, x, y, s, t);
break;
}
case :
{
Scan(s), Scan(t);
Modify(, , n, x, y, s, t);
break;
}
}
}
}

相关分析 BZOJ 4821的更多相关文章

  1. (WA)BZOJ 4821: [Sdoi2017]相关分析

    二次联通门 : BZOJ 4821: [Sdoi2017]相关分析 2017.8.23 Updata 妈妈!!这道题卡我!!!就是不然我过!!!!! #include <cstdio> # ...

  2. ●BZOJ 4821 [Sdoi2017]相关分析

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4821 题解: 线段树是真的恶心,(也许是我的方法麻烦了一些吧)首先那个式子可以做如下化简: ...

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

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

  4. bzoj 4821 [Sdoi2017]相关分析

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4821 题解 做法显然 就是维护一颗线段树 里面装4个东西 区间x的和 区间y的和 区间$x^ ...

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

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

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

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

  7. BZOJ 4821 (luogu 3707)(全网最简洁的代码实现之一)

    题面 传送门 分析 计算的部分其他博客已经写的很清楚了,本博客主要提供一个简洁的实现方法 尤其是pushdown函数写得很简洁 代码 #include<iostream> #include ...

  8. 4821: [Sdoi2017]相关分析

    4821: [Sdoi2017]相关分析 链接 分析: 大力拆式子,化简,然后线段树.注意精度问题与爆longlong问题. 代码: #include<cstdio> #include&l ...

  9. BZOJ4817 SDOI2017 相关分析

    4821: [Sdoi2017]相关分析 Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special Judge Description Frank对天文 ...

随机推荐

  1. Linux学习日记:第一天

    一,登录Linux Login:test Password:123456 Last Login:Wed Dec 3 22:40:02 on tty1 test@ubuntu: startx    进入 ...

  2. js中替换字符串

    function formatStr(str){ str=str.replace(/\r\n/ig,"<br/>"); return str; } 要注意两点: 要使用 ...

  3. NSOperation、NSOperationQueue

    NSOperation.NSOperationQueue NSOperation 和 NSOperationQueue 配合使用也能实现多线程. NSOperation 继承于 NSObject,是一 ...

  4. CNCF 有哪些具体的项目内容?

    前言:CNCF(Cloud Native Computing Foundation)于 2015 年 7 月成立,隶属于 Linux 基金会,初衷围绕“云原生”服务云计算,致力于维护和集成开源技术,支 ...

  5. python入门:1-99所有数的和的等式

    #!/usr/bin/env python # -*- coding:utf-8 -*- #1-99所有数的和的等式 #start(开始,译音:思达二测)sum(合计,译音:桑木)temp(临时雇员, ...

  6. Java使用Apache的HttpClient组件发送https请求

    如果我们直接通过普通的方式对https的链接发送请求,会报一个如下的错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Va ...

  7. paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之onehot coding styles(index-parameter style with registered outputs)

    case语句中,对于state/next 矢量仅仅做了1-bit比较. parameter 值不是表示FSM的状态编码,而是表示state/next变量的索引.

  8. TB平台搭建之一

    最近在搭建公司的testbench,主要有一下总结: 1.TB主要有两部分:部分一,软件部分主要用C写的,她的作用是写硬件的驱动(其实就是让核的外围设备可以正常工作或工作到特定的环境上)甚至有可能写整 ...

  9. python-数据类型总结 (面试常问)

    目录 数字类型总结 拷贝 浅拷贝 深拷贝 数字类型总结 一个值 多个值 整型/浮点型/字符串 列表/字典/元祖/集合 有序 无序 字符串/列表/元祖 字典/集合 可变 不可变 列表/字典/集合 整型/ ...

  10. python3 简单服务器监控,自动发送邮件

    import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimpo ...