[Sdoi2017]相关分析 [线段树]
[Sdoi2017]相关分析
题意:沙茶线段树
md其实我考场上还剩一个多小时写了40分
其实当时写正解也可以吧1h也就写完了不过还要拍一下
正解代码比40分短2333
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
#define fir first
#define sec second
#define lc x<<1
#define rc x<<1|1
#define mid ((l+r)>>1)
#define lson lc, l, mid
#define rson rc, mid+1, r
const int N=1e5+5, INF=1e9, M=1e5+5;
inline int read() {
char c=getchar(); int x=0, f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
int n, Q, op, ql, qr;
double a[N], b[N], s, w;
namespace seg {
struct meow{
double a, b, c, a2, ta, tb, sa, sb;
meow(): sa(-1), sb(-1) {}
meow(double a, double b, double c, double a2): a(a), b(b), c(c), a2(a2), ta(0), tb(0), sa(-1), sb(-1){}
} t[N<<2];
meow operator + (const meow &x, const meow &y) { return meow(x.a + y.a, x.b + y.b, x.c + y.c, x.a2 + y.a2); }
inline double cal2(double n) {return n * (n+1) * (2*n + 1) / 6;}
inline void _set(int x, int l, int r, double s, double w) {
meow &now = t[x];
now.sa = s; now.sb = w;
now.ta = now.tb = 0; // clear
double len = r-l+1, val = (l+r) * len / 2;
now.a = len * s + val;
now.b = len * w + val;
double val2 = cal2(r) - cal2(l-1);
now.c = val2 + val * (s + w) + s * w * len;
now.a2 = val2 + val * (s + s) + s * s * len;
}
inline void _add(int x, int l, int r, double s, double w) {
meow &now = t[x];
now.ta += s; now.tb += w;
double len = r-l+1;
now.c += now.a * w + now.b * s + s * w * len;
now.a2 += now.a * (s + s) + s * s * len;
now.a += len * s;
now.b += len * w;
}
inline void pushdn(int x, int l, int r) {
if(t[x].sa != -1 || t[x].sb != -1) {
_set(lson, t[x].sa, t[x].sb);
_set(rson, t[x].sa, t[x].sb);
t[x].sa = t[x].sb = -1;
}
if(t[x].ta || t[x].tb) {
_add(lson, t[x].ta, t[x].tb);
_add(rson, t[x].ta, t[x].tb);
t[x].ta = t[x].tb = 0;
}
}
void build(int x, int l, int r) {
if(l == r) t[x] = meow(a[l], b[l], a[l] * b[l], a[l] * a[l]);
else {
build(lson); build(rson);
t[x] = t[lc] + t[rc];
}
}
void add(int x, int l, int r) {
if(ql<=l && r<=qr) _add(x, l, r, s, w);
else {
pushdn(x, l, r);
if(ql <= mid) add(lson);
if(mid < qr ) add(rson);
t[x] = t[lc] + t[rc];
}
}
void cha(int x, int l, int r) {
if(ql<=l && r<=qr) _set(x, l, r, s, w);
else {
pushdn(x, l, r);
if(ql <= mid) cha(lson);
if(mid < qr ) cha(rson);
t[x] = t[lc] + t[rc];
}
}
meow que(int x, int l, int r) {
if(ql<=l && r<=qr) return t[x];
else {
pushdn(x, l, r);
if(qr <= mid) return que(lson);
else if(mid < ql ) return que(rson);
else return que(lson) + que(rson);
}
}
void Quer() {
meow x = que(1, 1, n);
double len = qr-ql+1, ans1 = x.c - x.a * x.b / len, ans2 = x.a2 - x.a * x.a / len;
printf("%.10lf\n", ans1 / ans2);
}
}
int main() {
//freopen("in", "r", stdin);
freopen("relative.in", "r", stdin);
freopen("relative.out", "w", stdout);
n=read(); Q=read();
for(int i=1; i<=n; i++) a[i]=read();
for(int i=1; i<=n; i++) b[i]=read();
seg::build(1, 1, n);
for(int i=1; i<=Q; i++) {
op=read(); ql=read(); qr=read();
if(op == 1) seg::Quer();
if(op == 2) s=read(), w=read(), seg::add(1, 1, n);
if(op == 3) s=read(), w=read(), seg::cha(1, 1, n);
}
}
[Sdoi2017]相关分析 [线段树]的更多相关文章
- 【BZOJ4821】[Sdoi2017]相关分析 线段树
[BZOJ4821][Sdoi2017]相关分析 Description Frank对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等. ...
- BZOJ 4821 [Sdoi2017]相关分析 ——线段树
打开题面,看到许多$\sum$ woc,好神啊,SDOI好强啊 然后展开之后,woc,SDOI好弱啊,怎么T3出个线段树裸题啊. 最后写代码的时候,woc,SDOI怎么出个这么码农的题啊,怎么调啊. ...
- 洛谷P3707 [SDOI2017]相关分析(线段树)
题目描述 Frank对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等. Frank不仅喜欢观测,还喜欢分析观测到的数据.他经常分析两个 ...
- BZOJ 4821: [Sdoi2017]相关分析 线段树 + 卡精
考试的时候切掉了,然而卡精 + 有一个地方忘开 $long long$,完美挂掉 $50$pts. 把式子化简一下,然后直接拿线段树来维护即可. Code: // luogu-judger-enabl ...
- BZOJ.4821.[SDOI2017]相关分析(线段树)
BZOJ LOJ 洛谷 恶心的拆式子..然后就是要维护\(\sum x_i,\ \sum y_i,\ \sum x_iy_i,\ \sum x_i^2\). 操作三可以看成初始化一遍,然后同操作二. ...
- SDOI2017相关分析 线段树
题目 https://loj.ac/problem/2005 思路 \[ \sum_{L}^{R}{(x_i-x)^{2}} \] \[ \sum_{L}^{R}{(x_i^2-2*x_i*x+x^{ ...
- 【BZOJ4821】【SDOI2017】相关分析 [线段树]
相关分析 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Frank对天文学非常感兴趣,他经 ...
- luogu3707 相关分析 (线段树)
把式子展开以后会发现,可以用线段树维护$x,y,x*y,x^2$分别的区间和 然后操作有区间加和区间修改 这个pushdown的时候,如果改和加的标记同时存在,那一定是先改再加,要不然加的标记已经被清 ...
- LOJ #2005. 「SDOI2017」相关分析 线段树维护回归直线方程
题目描述 \(Frank\) 对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等. \(Frank\) 不仅喜欢观测,还喜欢分析观测到的 ...
随机推荐
- 原生JavaScript常用的DOM操作
之前项目一直都是用JQuery或者Vue来做的,确实好用,毕竟帮我们解决了很多浏览器兼容问题,但是后面发现大公司面试题都是要原生Javascript来做,然后我就一脸懵逼哈哈哈,毕竟大公司需要的框架或 ...
- js object 常用方法总结
Object.assign(target,source1,source2,...) 该方法主要用于对象的合并,将源对象source的所有可枚举属性合并到目标对象target上,此方法只拷贝源对象的自身 ...
- SMTP错误码建议解决方法
https://wenku.baidu.com/view/0af30e01e87101f69e3195b8.html SMTP 错误码 / 建议解决方法 错误总表 101 Cannot Open Co ...
- [ios 开发笔记]:一句话笔记
1.NSString转int int a=[@"123" intValue]; 同样适用于NSDictionary将NSNumber转为int 2.switch(stateme ...
- 【转】Maven pom.xml 配置详解
原文链接:https://yq.aliyun.com/articles/38271 pom.xml文件配置详解 --声明规范 <project xmlns="http://maven. ...
- Windows和Linux如何使用Java代码实现关闭进程
在用selenium做自动化测试时,由于各种不明原因,有时Chrome浏览器会出现假死的情况,也就是整个浏览器响应超时,本人脚本主要部署在Windows机器上,所以主要以Windows为主,浏览器为C ...
- 百度Apollo 尝试
从Git-Hub上下载了Apollo源码在Ubuntu上准备运行一下 完成了以下步骤: bash docker/scripts/install_docker.sh bash docker/script ...
- c语言统计一个文件中的单词,字符和行数
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- background是什么样式?
background是什么样式? 给标签添加背景图片 分为: background: url("图片路径"); #添加图片 background-position: xpx ...
- SQL 优化经验总结34条(转)
(1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最 ...