[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\) 不仅喜欢观测,还喜欢分析观测到的 ...
随机推荐
- Myeclipse xml标签代码提示,引入schema
以SpringMVC为例 先引入命名空间 需要配置 xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schema ...
- 【android studio】 gradle配置成本地离线zip包
http://blog.csdn.net/jingleye/article/details/52689260 关键词:distributionUrl修改成本地路径,android stud ...
- windows 命令直接搜索局域网计算机的ip
以前都不知道还可以这样.....孤陋寡闻了... cmd 中 输入 net view ,搜索局域网或域中的计算机名. 找到要查询ip地址的计算机名后右键 标记,接着ping 一下,要用 -4 这个参数 ...
- JXLS 2.4.0系列教程(三)——嵌套循环是怎么做到的
注:本文代码在第一篇文章基础上修改而成,请务必先阅读第一篇文章. http://www.cnblogs.com/foxlee1024/p/7616987.html 本文也不会过多的讲解模板中遍历表达式 ...
- Dev中GridControl的GridView 基本样式设置
基本样式图: 代码如下: /// <summary> /// gridView样式 /// </summary> /// <param name="gdv&qu ...
- nth-child()选择器小结
之前也用到过nth-child,只是当时理解的不够透彻.今天回过头去看这个知识点时,发现了一个易错点. 浏览器支持情况: 所有主流浏览器都支持nth-child()选择器,除了IE8及更早的版本. 下 ...
- Spring MVC集成Swagger
什么是Swagger? 大部分 Web 应用程序都支持 RESTful API,但不同于 SOAP API——REST API 依赖于 HTTP 方法,缺少与 Web 服务描述语言(Web Servi ...
- GitHub上传文件或项目的教程
既然是往GitHub上传文件,那GitHub账号必须得有,这时候就会有同学问:妖怪吧,我没有GitHub账号怎么办? 别急别急,打开GitHub网站https://github.com/,然后注册就O ...
- Map,List,POJO深拷贝(序列化实现)方法与注意事项
转载请注明出处,谢谢! 方法1: /*jdk >= 1.5*/ @SuppressWarnings("unchecked") public static <T> ...
- github中删除项目