BZOJ 4821: [Sdoi2017]相关分析 线段树 + 卡精
考试的时候切掉了,然而卡精 + 有一个地方忘开 $long long$,完美挂掉 $50$pts.
把式子化简一下,然后直接拿线段树来维护即可.
Code:
// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include <cstdio>
#include <algorithm>
#define N 130304
#define ll double
#define ldb long double
#define setIO(s) freopen(s".in","r",stdin) , freopen(s".out","w",stdout)
using namespace std;
namespace IO
{
char *p1, *p2, buf[100000]; #define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ ) int rd() {
int x = 0, f = 1;
char c = nc();
while (c < 48) {
if (c == '-')
f = -1;
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x * f;
}
};
int n,Q;
ll X[N], Y[N];
ll sqr[N],sum[N];
struct Node
{
ll x,y,xy,add_t, add_s, set_s, set_t,sqr;
int set_tag;
}t[N<<2];
inline void pushup(int l,int r,int now)
{
int mid=(l+r)>>1;
if(mid>=l)
{
t[now].x=t[now<<1].x;
t[now].y=t[now<<1].y;
t[now].xy=t[now<<1].xy;
t[now].sqr=t[now<<1].sqr;
}
if(r>mid)
{
t[now].x+=t[now<<1|1].x;
t[now].y+=t[now<<1|1].y;
t[now].xy+=t[now<<1|1].xy;
t[now].sqr+=t[now<<1|1].sqr;
}
}
inline void mark1(int l,int r,int now,ll S,ll T)
{
t[now].add_s+=S, t[now].add_t+=T;
t[now].xy+=(ll)T*t[now].x+(ll)S*t[now].y+(ll)(r-l+1)*S*T;
t[now].sqr+=(ll)2*S*t[now].x+(ll)S*S*(r-l+1);
t[now].x+=(ll)(r-l+1)*S, t[now].y+=(ll)(r-l+1)*T;
}
inline void mark2(int l,int r,int now,ll S,ll T)
{
t[now].set_tag=1, t[now].set_s=S, t[now].set_t=T;
t[now].add_s=t[now].add_t=0;
t[now].x=sum[r]-sum[l-1]+(ll)S*(r-l+1);
t[now].y=sum[r]-sum[l-1]+(ll)T*(r-l+1);
t[now].xy=sqr[r]-sqr[l-1]+(ll)(T+S)*(sum[r]-sum[l-1])+(ll)(r-l+1)*S*T;
t[now].sqr=sqr[r]-sqr[l-1]+(ll)S*S*(r-l+1)+(ll)2*S*(sum[r]-sum[l-1]);
}
inline void pushdown(int l,int r,int now)
{
if(t[now].set_tag)
{
int mid=(l+r)>>1;
if(l<=mid) mark2(l,mid,now<<1,t[now].set_s,t[now].set_t);
if(r>mid) mark2(mid+1,r,now<<1|1,t[now].set_s,t[now].set_t);
t[now].set_s=t[now].set_t=t[now].set_tag=0;
}
if(t[now].add_s || t[now].add_t)
{
int mid=(l+r)>>1;
if(l<=mid) mark1(l,mid,now<<1,t[now].add_s,t[now].add_t);
if(r>mid) mark1(mid+1,r,now<<1|1,t[now].add_s,t[now].add_t);
t[now].add_t=t[now].add_s=0;
}
}
void build(int l,int r,int now)
{
if(l==r)
{
t[now].x=X[l];
t[now].y=Y[l];
t[now].xy=(ll)X[l]*Y[l];
t[now].sqr=(ll)X[l]*X[l];
return;
}
int mid=(l+r)>>1;
if(mid>=l) build(l,mid,now<<1);
if(r>mid) build(mid+1,r,now<<1|1);
pushup(l,r,now);
}
// x+=S, y+=T
void addv(int l,int r,int now,int L,int R,double S,double T)
{
if(l>=L&&r<=R)
{
mark1(l,r,now,S,T);
return;
}
pushdown(l,r,now);
int mid=(l+r)>>1;
if(L<=mid) addv(l,mid,now<<1,L,R,S,T);
if(R>mid) addv(mid+1,r,now<<1|1,L,R,S,T);
pushup(l,r,now);
}
void setv(int l,int r,int now,int L,int R,double S,double T)
{
if(l>=L&&r<=R)
{
mark2(l,r,now,S,T);
return;
}
pushdown(l,r,now);
int mid=(l+r)>>1;
if(L<=mid) setv(l,mid,now<<1,L,R,S,T);
if(R>mid) setv(mid+1,r,now<<1|1,L,R,S,T);
pushup(l,r,now);
}
ll queryx(int l,int r,int now,int L,int R)
{
if(l>=L&&r<=R) return t[now].x;
pushdown(l,r,now);
ll re=0;
int mid=(l+r)>>1;
if(L<=mid) re+=queryx(l,mid,now<<1,L,R);
if(R>mid) re+=queryx(mid+1,r,now<<1|1,L,R);
return re;
}
ll queryy(int l,int r,int now,int L,int R)
{
if(l>=L&&r<=R) return t[now].y;
pushdown(l,r,now);
ll re=0;
int mid=(l+r)>>1;
if(L<=mid) re+=queryy(l,mid,now<<1,L,R);
if(R>mid) re+=queryy(mid+1,r,now<<1|1,L,R);
return re;
}
ll queryxy(int l,int r,int now,int L,int R)
{
if(l>=L&&r<=R) return t[now].xy;
pushdown(l,r,now);
ll re=0;
int mid=(l+r)>>1;
if(L<=mid) re+=queryxy(l,mid,now<<1,L,R);
if(R>mid) re+=queryxy(mid+1,r,now<<1|1,L,R);
return re;
}
ll queryxx(int l,int r,int now,int L,int R)
{
if(l>=L&&r<=R) return t[now].sqr;
pushdown(l,r,now);
ll re=0;
int mid=(l+r)>>1;
if(L<=mid) re+=queryxx(l,mid,now<<1,L,R);
if(R>mid) re+=queryxx(mid+1,r,now<<1|1,L,R);
return re;
}
int main()
{
int i,j,cas;
// setIO("de");
n=IO::rd(),Q=IO::rd();
for(i=1;i<=n;++i) X[i]=IO::rd();
for(i=1;i<=n;++i) Y[i]=IO::rd();
for(i=1;i<=n;++i) sum[i]=sum[i-1]+i, sqr[i]=sqr[i-1]+(ll)i*i;
build(1,n,1);
for(cas=1;cas<=Q;++cas)
{
int opt,l,r,s,t;
opt=IO::rd(),l=IO::rd(),r=IO::rd();
if(opt==1)
{
long double up, down;
long double ybar=queryy(1,n,1,l,r)/(double)(r-l+1);
long double xbar=queryx(1,n,1,l,r)/(double)(r-l+1);
up=(ldb)queryxy(1,n,1,l,r)-ybar*(ldb)queryx(1,n,1,l,r)-xbar*(ldb)queryy(1,n,1,l,r)+(ldb)(r-l+1)*xbar*ybar;
down=(ldb)queryxx(1,n,1,l,r)+xbar*xbar*(ldb)(r-l+1)-2.00*xbar*(ldb)queryx(1,n,1,l,r);
long double answer=up/down;
printf("%.10lf\n",(double)answer);
}
if(opt==2)
{
s=IO::rd(),t=IO::rd();
addv(1,n,1,l,r,s,t);
}
if(opt==3)
{
s=IO::rd(),t=IO::rd();
setv(1,n,1,l,r,s,t);
}
}
return 0;
}
/*
*/
BZOJ 4821: [Sdoi2017]相关分析 线段树 + 卡精的更多相关文章
- BZOJ 4821 [Sdoi2017]相关分析 ——线段树
打开题面,看到许多$\sum$ woc,好神啊,SDOI好强啊 然后展开之后,woc,SDOI好弱啊,怎么T3出个线段树裸题啊. 最后写代码的时候,woc,SDOI怎么出个这么码农的题啊,怎么调啊. ...
- BZOJ.4821.[SDOI2017]相关分析(线段树)
BZOJ LOJ 洛谷 恶心的拆式子..然后就是要维护\(\sum x_i,\ \sum y_i,\ \sum x_iy_i,\ \sum x_i^2\). 操作三可以看成初始化一遍,然后同操作二. ...
- (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 题解: 线段树是真的恶心,(也许是我的方法麻烦了一些吧)首先那个式子可以做如下化简: ...
- [Sdoi2017]相关分析 [线段树]
[Sdoi2017]相关分析 题意:沙茶线段树 md其实我考场上还剩一个多小时写了40分 其实当时写正解也可以吧1h也就写完了不过还要拍一下 正解代码比40分短2333 #include <io ...
- 【BZOJ4821】[Sdoi2017]相关分析 线段树
[BZOJ4821][Sdoi2017]相关分析 Description Frank对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等. ...
- bzoj 4821 [Sdoi2017]相关分析
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4821 题解 做法显然 就是维护一颗线段树 里面装4个东西 区间x的和 区间y的和 区间$x^ ...
- 洛谷P3707 [SDOI2017]相关分析(线段树)
题目描述 Frank对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等. Frank不仅喜欢观测,还喜欢分析观测到的数据.他经常分析两个 ...
- SDOI2017相关分析 线段树
题目 https://loj.ac/problem/2005 思路 \[ \sum_{L}^{R}{(x_i-x)^{2}} \] \[ \sum_{L}^{R}{(x_i^2-2*x_i*x+x^{ ...
随机推荐
- 【Qt开发】QTextEdit 外观属性设置
一.给QTextEdit添加背景图片,有下面两种方法: QTextEdit* iEdit = new QTextEdit(); 1:使用样式表: iEdit->setStyleSheet(&q ...
- SpringBoot初步介绍及安装
SpringBoot的四个核心: 自动配置: 起步依赖: 命令行界面: Actuator: SpringBoot的web起步依赖:org.springframework.boot:spring-boo ...
- webdriver中判断元素是否存在的方法
selenium.webdriver中没有内置的判断元素是否存在的方法,所以定义一个方法,如果找到该元素则返回True,否则返回False: from selenium import webdrive ...
- 【转帖】联芸Maxio展示国产PCIe SSD主控:速度可达3.5GB/s
联芸Maxio展示国产PCIe SSD主控:速度可达3.5GB/s https://www.cnbeta.com/articles/tech/855223.htm 国产主控 紫光做国产颗粒 国产器件对 ...
- 如何将Numpy加速700倍?用 CuPy 呀
如何将Numpy加速700倍?用 CuPy 呀 作为 Python 语言的一个扩展程序库,Numpy 支持大量的维度数组与矩阵运算,为 Python 社区带来了很多帮助.借助于 Numpy,数据科学家 ...
- 高效编程之 concurrent.future
背景 我们知道 Python 中有多线程threading 和多进程multiprocessing 实现并发, 但是这两个东西开销很大,一是开启线程/进程的开销,二是主程序和子程序之间的通信需要 序列 ...
- uoj #242【UR #16】破坏蛋糕
uoj 考虑把那最后一条直线拎出来,并且旋转到和\(y\)轴平行(其他直线同时一起旋转),然后它和其他直线相交形成\(n+1\)个区间,现在要知道这些区间是否处在一个面积有限的区域 可以发现一段在有限 ...
- linux复习4:文件和目录
7一.linux文件 1.linux文件的扩展名:文件扩展名是文件名最后一个点之后的部分,下面列出了其中一部分 (1)压缩文件和归档文件 压缩和归档的文件扩展名及其含义如下. .bz2:使用bzip2 ...
- 使用Vim打开十六进制的文件
So Easy 这里使用打开 Hello.class 文件为例 首先使用 vim -b Hello.class 打开文件,然后在 Vim 的命令模式下输入 :%!xxd 回车即可看见文件内容. 效果: ...
- Delphi 对象观察器