4821: [Sdoi2017]相关分析

链接

分析:

  大力拆式子,化简,然后线段树。注意精度问题与爆longlong问题。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#define Root 1, n, 1
#define lc rt << 1
#define rc rt << 1 | 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
typedef double DB; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
DB sx[N << ], sy[N << ], s[N << ], s2[N << ], tag[N << ], taga[N << ], tagb[N << ], X[N], Y[N];
DB sum[N], sum2[N];
DB Sx, Sy, S, S2;
int n; inline void pushup(int rt) {
sx[rt] = sx[lc] + sx[rc];
sy[rt] = sy[lc] + sy[rc];
s2[rt] = s2[lc] + s2[rc];
s[rt] = s[lc] + s[rc];
}
inline void col(int rt,int l,int r) {
sy[rt] = sx[rt] = sum[r] - sum[l - ];
s2[rt] = s[rt] = sum2[r] - sum2[l - ];
tag[rt] = ; taga[rt] = tagb[rt] = ;
}
inline void add(int rt,DB len,DB a,DB b) {
s[rt] += sx[rt] * b + sy[rt] * a + a * b * len;
s2[rt] += a * a * len + * a * sx[rt];
sx[rt] += a * len;
sy[rt] += b * len;
taga[rt] += a, tagb[rt] += b;
}
inline void pushdown(int rt,int l,int r) {
int mid = (l + r) >> ;
if (tag[rt]) {
col(lc, l, mid); col(rc, mid + , r);
tag[rt] = ;
}
if (taga[rt] || tagb[rt]) { // !!!
add(lc, mid - l + , taga[rt], tagb[rt]);
add(rc, r - mid, taga[rt], tagb[rt]);
taga[rt] = tagb[rt] = ;
}
}
void build(int l,int r,int rt) {
if (l == r) {
sx[rt] = X[l], sy[rt] = Y[l], s[rt] = X[l] * Y[l], s2[rt] = X[l] * X[l]; return ;
}
int mid = (l + r) >> ;
build(lson), build(rson);
pushup(rt);
}
void update(int l,int r,int rt,int L,int R,DB a,DB b) {
if (L <= l && r <= R) {
add(rt, r - l + , a, b); return ;
}
int mid = (l + r) >> ;
pushdown(rt, l, r);
if (L <= mid) update(lson, L, R, a, b);
if (R > mid) update(rson, L, R, a, b);
pushup(rt);
}
void Change(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) {
col(rt, l, r); return ;
}
int mid = (l + r) >> ;
pushdown(rt, l, r);
if (L <= mid) Change(lson, L, R);
if (R > mid) Change(rson, L, R);
pushup(rt);
}
void query(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) {
Sx += sx[rt], Sy += sy[rt], S += s[rt], S2 += s2[rt]; return ;
}
int mid = (l + r) >> ;
pushdown(rt, l, r);
if (L <= mid) query(lson, L, R);
if (R > mid) query(rson, L, R);
}
void Ask() {
int l = read(), r = read();
Sx = Sy = S = S2 = ;
query(Root, l, r);
double x = 1.0 * Sx / (r - l + ), y = 1.0 * Sy / (r - l + );
// double u = S - Sy * x - Sx * y + x * y * (r - l + 1);
// double d = S2 + x * x * (r - l + 1) - 2 * Sx * x;
double u = S - y * Sx, d = S2 - x * Sx;
printf("%.10lf\n", (double)(u / d));
}
void work1() {
int l = read(), r = read();
DB a = (DB)read(), b = (DB)read();
update(Root, l, r, a, b);
}
void work2() {
int l = read(), r = read();
DB a = (DB)read(), b = (DB)read();
Change(Root, l, r);
update(Root, l, r, a, b);
}
int main() {
n = read();int m = read();
for (int i = ; i <= n; ++i) {
sum[i] = sum[i - ] + 1.0 * i;
sum2[i] = sum2[i - ] + 1.0 * i * i; // 此处报int !!!
}
for (int i = ; i <= n; ++i) X[i] = (DB)read();
for (int i = ; i <= n; ++i) Y[i] = (DB)read();
build(Root);
while (m --) {
int opt = read();
if (opt == ) Ask();
else if (opt == ) work1();
else work2();
}
return ;
}

4821: [Sdoi2017]相关分析的更多相关文章

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

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

  2. ●BZOJ 4821 [Sdoi2017]相关分析

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4821 题解: 线段树是真的恶心,(也许是我的方法麻烦了一些吧)首先那个式子可以做如下化简: ...

  3. bzoj 4821 [Sdoi2017]相关分析

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4821 题解 做法显然 就是维护一颗线段树 里面装4个东西 区间x的和 区间y的和 区间$x^ ...

  4. BZOJ.4821.[SDOI2017]相关分析(线段树)

    BZOJ LOJ 洛谷 恶心的拆式子..然后就是要维护\(\sum x_i,\ \sum y_i,\ \sum x_iy_i,\ \sum x_i^2\). 操作三可以看成初始化一遍,然后同操作二. ...

  5. BZOJ 4821 [Sdoi2017]相关分析 ——线段树

    打开题面,看到许多$\sum$ woc,好神啊,SDOI好强啊 然后展开之后,woc,SDOI好弱啊,怎么T3出个线段树裸题啊. 最后写代码的时候,woc,SDOI怎么出个这么码农的题啊,怎么调啊. ...

  6. BZOJ 4821: [Sdoi2017]相关分析 线段树 + 卡精

    考试的时候切掉了,然而卡精 + 有一个地方忘开 $long long$,完美挂掉 $50$pts. 把式子化简一下,然后直接拿线段树来维护即可. Code: // luogu-judger-enabl ...

  7. BZOJ4817 SDOI2017 相关分析

    4821: [Sdoi2017]相关分析 Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special Judge Description Frank对天文 ...

  8. [Sdoi2017]相关分析 [线段树]

    [Sdoi2017]相关分析 题意:沙茶线段树 md其实我考场上还剩一个多小时写了40分 其实当时写正解也可以吧1h也就写完了不过还要拍一下 正解代码比40分短2333 #include <io ...

  9. [题目] Luogu P3707 [SDOI2017]相关分析

    参考资料:[Luogu 3707] SDOI2017 相关分析 P3707 [SDOI2017]相关分析 TFRAC FRAC DFRAC \(\tfrac{\sum}{1}\) \(\frac{\s ...

随机推荐

  1. Mybatis工作原理(含部分源码)

    MyBatis的初始化 1.读取配置文件,形成InputStream String resource = "mybatis.xml"; // 加载mybatis的配置文件(它也加载 ...

  2. 设计带有placeHolder的TextView

    设计带有placeHolder的TextView 效果: 源码: PlaceholderTextView.h 与 PlaceholderTextView.m // // PlaceholderText ...

  3. September 14th 2017 Week 37th Thursday

    Don't let the past steal your present. 别让过去悄悄偷走了我们的当下. We take what we can get and make the best of ...

  4. Python Frame

    http://farmdev.com/src/secrets/framehack/index.html sys._getframe([depth]) Return a frame object fro ...

  5. 021.14 IO流 管道流

    用的频率不高特点:读取管道和写入管道对接,需要是用多线程技术,单线程容易死锁 使用connect方法连接两个流,实现边读编写,和node.js的管道流差不多 //##主函数位置 public stat ...

  6. Eclipse validation

    window->preferences->validation 可以取消部分文件的验证,取消build时验证,改为手动验证,提高效率.

  7. JDK1.6在LINUX下的安装配置[转]

    JDK1.6在LINUX下的安装是如何进行的呢,让我们开始我们的演示: Ubuntu Linux下jdk的安装与配置 1.JDK1.6安装准备 从sun公司网站www.sun.com下载linux版本 ...

  8. Python全栈开发:list 、tuple以及dict的总结

    总结: 列表:增:append(),inset(),extend() 删:pop(),remove(),clear(),del 改:a.通过指定元素和切片重新赋值.b.可以使用repelace替换列表 ...

  9. 20145203盖泽双java实验三 敏捷开发与XP实践

    java实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> ...

  10. React 入门学习笔记2

    摘自阮一峰:React入门实例教程,转载请注明出处. 一.获取真实的DOM节点 组件并不是真实的 DOM 节点,而是存在于内存之中的一种数据结构,叫做虚拟 DOM (virtual DOM).只有当它 ...