相关分析 BZOJ 4821
相关分析
【问题描述】
【输入格式】
【输出格式】
对于每个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的更多相关文章
- (WA)BZOJ 4821: [Sdoi2017]相关分析
二次联通门 : BZOJ 4821: [Sdoi2017]相关分析 2017.8.23 Updata 妈妈!!这道题卡我!!!就是不然我过!!!!! #include <cstdio> # ...
- ●BZOJ 4821 [Sdoi2017]相关分析
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4821 题解: 线段树是真的恶心,(也许是我的方法麻烦了一些吧)首先那个式子可以做如下化简: ...
- BZOJ.4821.[SDOI2017]相关分析(线段树)
BZOJ LOJ 洛谷 恶心的拆式子..然后就是要维护\(\sum x_i,\ \sum y_i,\ \sum x_iy_i,\ \sum x_i^2\). 操作三可以看成初始化一遍,然后同操作二. ...
- bzoj 4821 [Sdoi2017]相关分析
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4821 题解 做法显然 就是维护一颗线段树 里面装4个东西 区间x的和 区间y的和 区间$x^ ...
- BZOJ 4821 [Sdoi2017]相关分析 ——线段树
打开题面,看到许多$\sum$ woc,好神啊,SDOI好强啊 然后展开之后,woc,SDOI好弱啊,怎么T3出个线段树裸题啊. 最后写代码的时候,woc,SDOI怎么出个这么码农的题啊,怎么调啊. ...
- BZOJ 4821: [Sdoi2017]相关分析 线段树 + 卡精
考试的时候切掉了,然而卡精 + 有一个地方忘开 $long long$,完美挂掉 $50$pts. 把式子化简一下,然后直接拿线段树来维护即可. Code: // luogu-judger-enabl ...
- BZOJ 4821 (luogu 3707)(全网最简洁的代码实现之一)
题面 传送门 分析 计算的部分其他博客已经写的很清楚了,本博客主要提供一个简洁的实现方法 尤其是pushdown函数写得很简洁 代码 #include<iostream> #include ...
- 4821: [Sdoi2017]相关分析
4821: [Sdoi2017]相关分析 链接 分析: 大力拆式子,化简,然后线段树.注意精度问题与爆longlong问题. 代码: #include<cstdio> #include&l ...
- BZOJ4817 SDOI2017 相关分析
4821: [Sdoi2017]相关分析 Time Limit: 10 Sec Memory Limit: 128 MBSec Special Judge Description Frank对天文 ...
随机推荐
- MHA
MHA 1. MHA简介 1.1 MHA工作原理总结为如下 1.2 MHA工具包介绍 2. 部署MHA 2.1 环境介绍 2.2 一主两从复制搭建 2.3 配置互信 2.4 下载MHA 2.5 安装M ...
- ipmitool的使用
https://www.ibm.com/developerworks/cn/linux/l-ipmi/index.html
- webpack4搭建Vue开发环境笔记~~持续更新
项目git地址 一.node知识 __dirname: 获取当前文件所在路径,等同于path.dirname(__filename) console.log(__dirname); // Prints ...
- IIS7.0/8.0的错误HTTP Error 500.19 - Internal Server Error ,错误代码为0x80070021
最近在部署项目的时候,总是出现了这个问题. 大概原因为IIS7.0的安全设定相比前版本有很大的变更.IIS7.0的安全设置文件在%windir%\system32\inetsrv \config\ap ...
- 【android】【android studio】修改emulator的本地化环境
Changing the emulator locale from the adb shell To change the locale in the emulator by using the ad ...
- 多线程之volatile关键字(五)
开始全文之前,先铺垫一下jvm基础知识以及线程栈: JVM栈是线程私有的,每个线程创建的同时都会创建JVM栈,JVM栈中存放的为当前线程中局部基本类型的变量(java中定义的八种基本类型:boolea ...
- goalng导出excel(csv格式)
最近项目中有个小需求,需要将查询结果导出到excel.之间前java比较容易,使用POI很容易就能实现,查了下golang的文档,发现golang下边并没有导出excel的包,但是却有一个encodi ...
- jubeeeeeat(网络流)
jubeeeeeat 总时间限制: 1000ms 内存限制: 256000kB 描述 众所周知,LZF很喜欢打一个叫Jubeat的游戏.这是个音乐游戏,游戏界面是4×4的方阵,会根据音乐节奏要求玩 ...
- poj 3107 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7885 Accepted: 2786 Descrip ...
- day18-socket 编程
1.Socket是网络上的使用的交互信息得方法,也叫套接字 用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. 通讯原理 Soc ...