题面

传送门

分析

计算的部分其他博客已经写的很清楚了,本博客主要提供一个简洁的实现方法

尤其是pushdown函数写得很简洁

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100005
using namespace std;
int n,m;
double x[maxn];
double y[maxn]; double get_sumx(double l,double r) {//返回l+(l+1)+...+r
return (l+r)*(r-l+1)/2;
} double get_sumsqx(double l,double r) {//返回l^2+(l+1)^2+...+r^2
return r*(r+1)*(2*r+1)/6-(l-1)*l*(2*l-1)/6;
} struct segment_tree {
#define lson pos<<1//宏,简化代码
#define rson pos<<1|1
struct node {
int l;//记录区间端点,方便在计算中调用
int r;
double sx;
double sy;
double sqx;
double xy;
double addx;
double addy;
double len() {//返回区间长度,同样是方便计算
return r-l+1;
}
bool set;
} tree[maxn<<2]; void push_up(int pos) {
tree[pos].sx=tree[lson].sx+tree[rson].sx;
tree[pos].sy=tree[lson].sy+tree[rson].sy;
tree[pos].sqx=tree[lson].sqx+tree[rson].sqx;
tree[pos].xy=tree[lson].xy+tree[rson].xy;
} void push_down(int pos) {
int son=pos<<1;
if(tree[pos].set) {
for(int i=0; i<=1; i++) {//用循环来遍历,少写一次代码
son=son|i;
tree[son].addx=tree[son].addy=0;
tree[son].sx=tree[son].sy=get_sumx(tree[son].l,tree[son].r);
tree[son].xy=tree[son].sqx=get_sumsqx(tree[son].l,tree[son].r);
tree[son].set=1;
}
tree[pos].set=0;
}
if(tree[pos].addx||tree[pos].addy) {
son=pos<<1;
double mkx=tree[pos].addx;
double mky=tree[pos].addy;
for(int i=0; i<=1; i++) {
son=son|i;
tree[son].addx+=mkx;
tree[son].addy+=mky;
tree[son].sqx+=2*tree[son].sx*mkx+mkx*mkx*tree[son].len();
tree[son].xy+=tree[son].len()*mkx*mky+tree[son].sx*mky+tree[son].sy*mkx;
tree[son].sx+=mkx*tree[son].len();
tree[son].sy+=mky*tree[son].len();
}
tree[pos].addx=tree[pos].addy=0;
}
} void build(int l,int r,int pos,double *x,double *y) {
tree[pos].l=l;
tree[pos].r=r;
if(l==r) {
tree[pos].sx=x[l];
tree[pos].sy=y[l];
tree[pos].sqx=x[l]*x[l];
tree[pos].xy=x[l]*y[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,pos<<1,x,y);
build(mid+1,r,pos<<1|1,x,y);
push_up(pos);
} void add_segment(int L,int R,int pos,double s,double t) {
if(L<=tree[pos].l&&R>=tree[pos].r) {
tree[pos].addx+=s;
tree[pos].addy+=t;
tree[pos].xy+=tree[pos].sx*t+tree[pos].sy*s+s*t*tree[pos].len();
tree[pos].sqx+=2*tree[pos].sx*s+s*s*tree[pos].len();
tree[pos].sx+=s*tree[pos].len();
tree[pos].sy+=t*tree[pos].len();
return;
}
push_down(pos);
int mid=(tree[pos].l+tree[pos].r)>>1;
if(L<=mid) add_segment(L,R,pos<<1,s,t);
if(R>mid) add_segment(L,R,pos<<1|1,s,t);
push_up(pos);
} void set_segment(int L,int R,int pos) {
if(L<=tree[pos].l&&R>=tree[pos].r) {
tree[pos].set=1;
tree[pos].addx=tree[pos].addy=0;
tree[pos].sx=tree[pos].sy=get_sumx(tree[pos].l,tree[pos].r);
tree[pos].sqx=tree[pos].xy=get_sumsqx(tree[pos].l,tree[pos].r);
return;
}
push_down(pos);
int mid=(tree[pos].l+tree[pos].r)>>1;
if(L<=mid) set_segment(L,R,pos<<1);
if(R>mid) set_segment(L,R,pos<<1|1);
push_up(pos);
} double query_sx(int L,int R,int pos) {
if(L<=tree[pos].l&&R>=tree[pos].r) {
return tree[pos].sx;
}
push_down(pos);
double ans=0;
int mid=(tree[pos].l+tree[pos].r)>>1;
if(L<=mid) ans+=query_sx(L,R,pos<<1);
if(R>mid) ans+=query_sx(L,R,pos<<1|1);
return ans;
} double query_sy(int L,int R,int pos) {
if(L<=tree[pos].l&&R>=tree[pos].r) {
return tree[pos].sy;
}
push_down(pos);
double ans=0;
int mid=(tree[pos].l+tree[pos].r)>>1;
if(L<=mid) ans+=query_sy(L,R,pos<<1);
if(R>mid) ans+=query_sy(L,R,pos<<1|1);
return ans;
} double query_sqx(int L,int R,int pos) {
if(L<=tree[pos].l&&R>=tree[pos].r) {
return tree[pos].sqx;
}
push_down(pos);
double ans=0;
int mid=(tree[pos].l+tree[pos].r)>>1;
if(L<=mid) ans+=query_sqx(L,R,pos<<1);
if(R>mid) ans+=query_sqx(L,R,pos<<1|1);
return ans;
} double query_xy(int L,int R,int pos) {
if(L<=tree[pos].l&&R>=tree[pos].r) {
return tree[pos].xy;
}
push_down(pos);
double ans=0;
int mid=(tree[pos].l+tree[pos].r)>>1;
if(L<=mid) ans+=query_xy(L,R,pos<<1);
if(R>mid) ans+=query_xy(L,R,pos<<1|1);
return ans;
} #undef lson
#undef rson
} T; void debug(){
printf("debug:\n");
for(int i=1;i<=n;i++){
printf("%.0f ",T.query_sx(i,i,1));
}
printf("\n");
for(int i=1;i<=n;i++){
printf("%.0f ",T.query_sy(i,i,1));
}
printf("\n");
printf("\n");
}
int main() {
int cmd,l,r;
double s,t;
double up,down,ax,ay;
double sumx,sumy,sumxy,sumsqx;
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++) {
scanf("%lf",&x[i]);
}
for(int i=1; i<=n; i++) {
scanf("%lf",&y[i]);
}
T.build(1,n,1,x,y);
for(int i=1; i<=m; i++) {
scanf("%d",&cmd);
if(cmd==2) {
scanf("%d %d %lf %lf",&l,&r,&s,&t);
T.add_segment(l,r,1,s,t);
} else if(cmd==3) {
scanf("%d %d %lf %lf",&l,&r,&s,&t);
T.set_segment(l,r,1);
T.add_segment(l,r,1,s,t);
} else {
scanf("%d %d",&l,&r);
sumx=T.query_sx(l,r,1);
sumy=T.query_sy(l,r,1);
sumxy=T.query_xy(l,r,1);
sumsqx=T.query_sqx(l,r,1);
// printf("x=%.0f y=%.0f xy=%.0f x^2=%.0f\n",sumx,sumy,sumxy,sumsqx);
ax=sumx/(r-l+1);
ay=sumy/(r-l+1);
up=ax*ay*(r-l+1)+sumxy-ay*sumx-ax*sumy;
down=sumsqx-2*ax*sumx+(r-l+1)*ax*ax;
printf("%.10f\n",up/down);
}
// debug();
}
}

BZOJ 4821 (luogu 3707)(全网最简洁的代码实现之一)的更多相关文章

  1. [Luogu 3707] SDOI2017 相关分析

    [Luogu 3707] SDOI2017 相关分析 前言 Capella 和 Frank 一样爱好天文学. 她常在冬季的夜晚,若有所思地望着东北方上空的五边形中,最为耀眼的一个顶点. 那一抹金黄曾带 ...

  2. BZOJ 3052/Luogu P4074 [wc2013]糖果公园 (树上带修莫队)

    题面 中文题面,难得解释了 BZOJ传送门 Luogu传送门 分析 树上带修莫队板子题... 开始没给分块大小赋初值T了好一会... CODE #include <bits/stdc++.h&g ...

  3. BZOJ 3931 / Luogu P3171 [CQOI2015]网络吞吐量 (最大流板题)

    题面 中文题目,不解释: BZOJ传送门 Luogu传送门 分析 这题建图是显然的,拆点后iii和i′i'i′连容量为吞吐量的边,根据题目要求,111和nnn的吞吐量看作∞\infty∞. 然后用di ...

  4. BZOJ 3894 / Luogu P4313 文理分科 (拆点最小割)

    题面 中文题面- BZOJ 传送门 Luogu 传送门 分析 这道题类似于BZOJ 3774 最优选择,然后这里有一篇博客写的很好- Today_Blue_Rainbow's Blog 应该看懂了吧- ...

  5. BZOJ 2039 / Luogu P1791 [2009国家集训队]employ人员雇佣 (最小割)

    题面 BZOJ传送门 Luogu传送门 分析 考虑如何最小割建图,因为这仍然是二元关系,我们可以通过解方程来确定怎么建图,具体参考论文 <<浅析一类最小割问题 湖南师大附中 彭天翼> ...

  6. BZOJ 2127 / Luogu P1646 [国家集训队]happiness (最小割)

    题面 BZOJ传送门 Luogu传送门 分析 这道题又出现了二元关系,于是我们只需要解方程确定怎么连边就行了 假设跟SSS分在一块是选文科,跟TTT分在一块是选理科,先加上所有的收益,再来考虑如何让需 ...

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

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

  8. [BZOJ 1535] [Luogu 3426]SZA-Template (KMP+fail树+双向链表)

    [BZOJ 1535] [Luogu 3426]SZA-Template (KMP+fail树+双向链表) 题面 Byteasar 想在墙上涂一段很长的字符,他为了做这件事从字符的前面一段中截取了一段 ...

  9. [BZOJ 3295] [luogu 3157] [CQOI2011]动态逆序对(树状数组套权值线段树)

    [BZOJ 3295] [luogu 3157] [CQOI2011] 动态逆序对 (树状数组套权值线段树) 题面 给出一个长度为n的排列,每次操作删除一个数,求每次操作前排列逆序对的个数 分析 每次 ...

随机推荐

  1. Docker实战部署应用——Redis

    Redis 部署 拉取Redis镜像 docker pull redis 创建Redis容器 docker run -id --name=sun_redis -p 6379:6379 redis 客户 ...

  2. 后台PDF返回Base64,前台接收预览

    读取已存在的PDF文件,path为绝对路径 string base64String = "";byte[] buffer=null; using (FileStream fs = ...

  3. GB28181 To RTMP/HLS/HTTP-FLV/DASH Gateway

    I. Deployment  / Architecture Block Diagram II. Resources Used 1. freeswitch —— sip server https://f ...

  4. C++宽字符串转字符串

    这文章是更改别人代码 #include <string> #include <iostream> #include <stdlib.h> #include < ...

  5. Linux系统启动过程浅析

    经过老师的讲解以及查阅资料后,现对Linux系统启动做以浅析,仅是个人理解. 主要的步骤有以下几步: 第一步:Power On.用户按下电源开关的那一瞬间,叫Power On阶段 .在这个阶段,BIO ...

  6. selenium下拉一个框内的滚动条

    js='document.getElementsByClassName("route-tree")[0].scrollTop=10000'

  7. vue项目中数学公式的展示

    在这里有个mathjax的插件,可以将dom中的数学公式展示. 第一步安装mathjax npm install mathjax 安装完之后,你会在index.html中发现,已经引用了js文件,并且 ...

  8. Eclipse 4.9 创建springboot项目步骤

    上一篇文章写了eclipse安装STS. 现在创建Spring Starter Project  具体步骤如下: 1.等你安装好STS后,就在Eclipse >  File >New 选择 ...

  9. 第九届ECNU Coder A.足球锦标赛

    题目链接:http://acm.ecnu.edu.cn/contest/16/problem/A/ 题目: A. 足球锦标赛 Time limit per test: 2.0 seconds Time ...

  10. php面试专题---MYSQL查询语句优化

    php面试专题---MYSQL查询语句优化 一.总结 一句话总结: mysql的性能优化包罗甚广: 索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存) ...