BZOJ 4821 (luogu 3707)(全网最简洁的代码实现之一)
题面
分析
计算的部分其他博客已经写的很清楚了,本博客主要提供一个简洁的实现方法
尤其是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)(全网最简洁的代码实现之一)的更多相关文章
- [Luogu 3707] SDOI2017 相关分析
[Luogu 3707] SDOI2017 相关分析 前言 Capella 和 Frank 一样爱好天文学. 她常在冬季的夜晚,若有所思地望着东北方上空的五边形中,最为耀眼的一个顶点. 那一抹金黄曾带 ...
- BZOJ 3052/Luogu P4074 [wc2013]糖果公园 (树上带修莫队)
题面 中文题面,难得解释了 BZOJ传送门 Luogu传送门 分析 树上带修莫队板子题... 开始没给分块大小赋初值T了好一会... CODE #include <bits/stdc++.h&g ...
- BZOJ 3931 / Luogu P3171 [CQOI2015]网络吞吐量 (最大流板题)
题面 中文题目,不解释: BZOJ传送门 Luogu传送门 分析 这题建图是显然的,拆点后iii和i′i'i′连容量为吞吐量的边,根据题目要求,111和nnn的吞吐量看作∞\infty∞. 然后用di ...
- BZOJ 3894 / Luogu P4313 文理分科 (拆点最小割)
题面 中文题面- BZOJ 传送门 Luogu 传送门 分析 这道题类似于BZOJ 3774 最优选择,然后这里有一篇博客写的很好- Today_Blue_Rainbow's Blog 应该看懂了吧- ...
- BZOJ 2039 / Luogu P1791 [2009国家集训队]employ人员雇佣 (最小割)
题面 BZOJ传送门 Luogu传送门 分析 考虑如何最小割建图,因为这仍然是二元关系,我们可以通过解方程来确定怎么建图,具体参考论文 <<浅析一类最小割问题 湖南师大附中 彭天翼> ...
- BZOJ 2127 / Luogu P1646 [国家集训队]happiness (最小割)
题面 BZOJ传送门 Luogu传送门 分析 这道题又出现了二元关系,于是我们只需要解方程确定怎么连边就行了 假设跟SSS分在一块是选文科,跟TTT分在一块是选理科,先加上所有的收益,再来考虑如何让需 ...
- (WA)BZOJ 4821: [Sdoi2017]相关分析
二次联通门 : BZOJ 4821: [Sdoi2017]相关分析 2017.8.23 Updata 妈妈!!这道题卡我!!!就是不然我过!!!!! #include <cstdio> # ...
- [BZOJ 1535] [Luogu 3426]SZA-Template (KMP+fail树+双向链表)
[BZOJ 1535] [Luogu 3426]SZA-Template (KMP+fail树+双向链表) 题面 Byteasar 想在墙上涂一段很长的字符,他为了做这件事从字符的前面一段中截取了一段 ...
- [BZOJ 3295] [luogu 3157] [CQOI2011]动态逆序对(树状数组套权值线段树)
[BZOJ 3295] [luogu 3157] [CQOI2011] 动态逆序对 (树状数组套权值线段树) 题面 给出一个长度为n的排列,每次操作删除一个数,求每次操作前排列逆序对的个数 分析 每次 ...
随机推荐
- glibc2.22
- javaweb各种框架组合案例(九):springboot+tk.mybatis+通用service
一.项目结构 二.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- 奇虎360的开源OpenResty Windows版本
https://github.com/LomoX-Offical/nginx-openresty-windows
- 洛谷P2606 [ZJOI2010]排列计数 组合数学+DP
题意:称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic的,答案可能很大, ...
- java类实现序列化的方法?collection框架中实现什么样的接口
- Message相关函数对比
SendMessage PostMessage 过程类型 同步过程:等待其他程序处理消息完了之后才返回,继续执行 异步过程:只把消息放入队列,不管其他程序是否处理都返回,然后继续执行 返回值 表 ...
- 微服务+DDD代码结构例子
这是一个基本的微服务+DDD演示例子: 基于 Spring Boot 1.5.6 , Spring Cloud Edgware.SR4 Version 微服务 + DDD,个人觉得应该是首先是从微服务 ...
- 对webpack的初步研究6
Plugins 插件是webpack 的支柱.webpack本身构建在您在webpack配置中使用的相同插件系统上! 它们也是这样做的目的别的,一个装载机无法做到的. Anatomy webpack ...
- tensorflow图像处理函数(1)
1.tensorflow中对jpeg格式图像的编码/解码函数: import matplotlib.pyplot as plt import tensorflow as tf image_raw_da ...
- 状压DP常用操作
1. 判断一个数字x二进制下第i位是不是等于1. 方法:if ( ( ( 1 << ( i - 1 ) ) & x ) > 0) 将1左移i-1位,相当于制造了一个只有第i位 ...