[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\) 不仅喜欢观测,还喜欢分析观测到的 ...
随机推荐
- android动画源码合集、动态主题框架、社交app源码等
Android精选源码 仿MIUI果冻视图-BouncingJellyView 一个快速易用的动态主题框架 android动画效果集合源码 android使用Kotlin开发的Dribbb ...
- 解决 重启nginx: [alert] kill(189, 1) failed (3: No such process)
解决 nginx: [alert] kill(189, 1) failed (3: No such process) [root@localhost/]# nginx -s reloadnginx: ...
- [国嵌攻略][054][NandFlash驱动设计_写]
Nand Flash支持按页写和随机写两种方式,在下面实现的是按页写.闪存在写数据时,只能写入1,不能写入0,所以写函数必须和擦除函数一起使用,并且擦除函数是按块擦除. /************** ...
- 久未更 ~ 四之 —— Vsftpd出现 Failed to start Vsftpd ftp daemon错误
> > > > > 久未更 系列一:Failed to start Vsftpd ftp daemon错误 配置 vsftpd.conf文件后 重启ftp服务 出现 Fa ...
- java序列化反序列化深入探究
When---什么时候需要序列化和反序列化: 简单的写一个hello world程序,用不到序列化和反序列化.写一个排序算法也用不到序列化和反序列化.但是当你想要将一个对象进行持久化写入文件,或者你想 ...
- JavaScript常见封装方法
1.最简单的,使用变量,然后用匿名函数包裹,不封装 2.对象字面量简单封装(不完整的模块模式,因为无法达到变量.方法私有效果.不过确实有分离和组织代码的能力,也就算一种简略的模块模式的实现方式) va ...
- Android之MaterialDesign应用技术
PS:纵观现在大大小小软件的界面都变的比较漂亮,还有一些系统了,比如小米的MIUI,华为的EMUI等,虽然底层都是安卓,但他们的界面多多少少都会不同,谷歌对这个UI也是非常重视的,MaterialDe ...
- Java 得到泛型中得到T.class
Class <T> entityClass = (Class <T>) ((ParameterizedType) getClass().getGenericSuperclass ...
- Spring的RestTemplata使用
spring-web的RestTemplata是对java底层http的封装,使用RestTemplata用户可以不再关注底层的连接建立,并且RestTemplata不仅支持Rest规范,还可以定义返 ...
- 爬取西刺网代理ip,并把其存放mysql数据库
需求: 获取西刺网代理ip信息,包括ip地址.端口号.ip类型 西刺网:http://www.xicidaili.com/nn/ 那,如何解决这个问题? 分析页面结构和url设计得知: 数据都在本页面 ...