相关分析

【问题描述】

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. 虚拟机设置NAT

    需要开启虚拟机网络相关服务, 安装虚拟网卡, 还有必须安装 VMware Tools VMware虚拟机下实现NAT方式上网1. 把你的虚拟网卡VMnet8设置为自动获得IP.自动获得DNS服务器,启 ...

  2. 中英文字符串截取函数msubstr

    Thinkphp内置了一个可以媲美smarty的模板引擎,给我们带来了很大的方便.调用函数也一样,可以和smarty一样调用自己需要的函数,而官方也内置了一些常用的函数供大家调用. 比如今天我们说的截 ...

  3. Java开发小游戏 用键盘控制精灵在游戏中上下左右跑动 窗体小游戏可打包下载,解压后双击start运行

    package com.swift; import java.awt.Point; import java.awt.event.KeyEvent; import com.rupeng.game.Gam ...

  4. iOS重绘机制drawRect

    iOS的绘图操作是在UIView类的drawRect方法中完成的,所以如果我们要想在一个UIView中绘图,需要写一个扩展UIView 的类,并重写drawRect方法,在这里进行绘图操作,程序会自动 ...

  5. 【原】基于matlab的蓝色车牌定位与识别---绪论

    本着对车牌比较感兴趣,自己在课余时间摸索关于车牌的定位与识别,现将自己所做的一些内容整理下,也方便和大家交流. 考虑到车牌的定位涉及到许多外界的因素,因此有必要对车牌照的获取条件进行一些限定: 一.大 ...

  6. 【转】MFC 程序入口和执行流程

    一 MFC程序执行过程剖析 1)我们知道在WIN32API程序当中,程序的入口为WinMain函数,在这个函数当中我们完成注册窗口类,创建窗口,进入消息循环,最后由操作系统根据发送到程序窗口的消息调用 ...

  7. 数据结构实用C语言基础

    大纲: 主要介绍了C语言中的指针,内存分配,两种传参方式,typedef的简单用法 关于C语言中的指针: 指针变量也称为指针(Pointer) 例如:int* p; 则p为一个指向int类型的指针. ...

  8. jenkins 全局工具配置

  9. Python基础(三)—— print()格式化输出变量

    先举一个简单的例子说明: name = 'Jack' answer = input('你好,%s '%(name) + '你认识 Sean 不, 输入 yes or no\n') print('Sea ...

  10. python-time模块--pickle模块

    目录 time 模块 为什么要有time模块,time模块有什么用? time模块的三种格式 时间戳(timestamp) 格式化时间(需要自己定义格式) 结构化时间(struct-time) 结构化 ...