题目链接

利用期望的线性性质:

\(E(sum) = E(x_l) + E(x_{l+1})+ E(x_{l+2}) +.. E(x_r)\)

然后就考虑对于交换时两个区间元素的改动.

假设这两个区间的长度分别为\(L_1,L_2\),和为\(S_1,S_2\)

那么对于第一个区间元素的期望为.

\(E(x_i) = \dfrac{L_1 - 1}{L_1} * E(x_i) + \dfrac{1}{L_1}*\dfrac{L_2}{S_2}\)

对于第二个区间也是这样.

然后用线段树维护一下就可以了.

#include <iostream>
#include <cstdio>
#define lson now << 1
#define rson now << 1 | 1
const int maxN = 1e5 + 7; struct Node {
int l,r;
double sum,lazy,mul;
}tree[maxN << 2]; inline int read() {
int x = 0,f = 1;char c = getchar();
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;
} void init(int now,int l,int r) {
tree[now].l = l;tree[now].r = r;
tree[now].mul = 1;tree[now].lazy = 0;
return ;
} void updata(int now) {
tree[now].sum = tree[lson].sum + tree[rson].sum;
return;
} void build(int now,int l,int r) {
init(now,l,r);
if(l == r) {
tree[now].sum = read();
return;
}
int mid = (l + r) >> 1;
build(lson,l,mid);
build(rson,mid + 1,r);
updata(now);
return ;
} void work(int now,double lazy,double mul) {
tree[now].lazy = tree[now].lazy * mul + lazy;
tree[now].mul = tree[now].mul * mul;
tree[now].sum = tree[now].sum * mul + lazy * (tree[now].r - tree[now].l + 1);
return ;
} void pushdown(int now) {
work(lson,tree[now].lazy,tree[now].mul);
work(rson,tree[now].lazy,tree[now].mul);
tree[now].lazy = 0;tree[now].mul = 1;
return ;
} double query(int now,int L,int R) {
if(tree[now].l >= L && tree[now].r <= R) return tree[now].sum;
int mid = (tree[now].l + tree[now].r) >> 1;
double sum = 0;
if(tree[now].lazy || tree[now].mul != 1) pushdown(now);
if(mid >= L) sum += query(lson,L,R);
if(mid < R) sum += query(rson,L,R);
return sum;
} void modify(int now,int L,int R,double lazy,double mul) {
if(tree[now].l >= L && tree[now].r <= R) {
work(now,lazy,mul);
return ;
}
if(tree[now].lazy || tree[now].mul != 1) pushdown(now);
int mid = (tree[now].l + tree[now].r) >> 1;
if(mid >= L) modify(lson,L,R,lazy,mul);
if(mid < R) modify(rson,L,R,lazy,mul);
updata(now);
return ;
} int main() {
int n,m;
n = read();m = read();
build(1,1,n);
int opt,l1,r1,l2,r2;
while(m --) {
opt = read() - 1;l1 = read();r1 = read();
if(!opt) {
l2 = read();r2 = read();
int L_1,L_2;
double S_1,S_2;
L_1 = r1 - l1 + 1;L_2 = r2 - l2 + 1;
S_1 = query(1,l1,r1);S_2 = query(1,l2,r2);
modify(1,l1,r1,1.0 / L_1 * S_2 / L_2,1.0 * (L_1 - 1) / L_1);
modify(1,l2,r2,1.0 / L_2 * S_1 / L_1,1.0 * (L_2 - 1) / L_2);
}
else printf("%.7lf\n",query(1,l1,r1));
}
return 0;
}

CF895E Eyes Closed (期望)的更多相关文章

  1. [CF895E] Eyes Closed(线段树,期望)

    Desctiption 传送门:Portal 大致题意: 给你一个序列, 支持两种操作: 1 l1 r1 l2 y2 在\([l1, r1]\)随机选择一个数a, \([l2, r2]\) 内随机选择 ...

  2. [CF895E]Eyes Closed

    luogu description 一个序列\(a_i\),支持一下两种操作. \(1\ \ l_1\ \ r_1\ \ l_2\ \ r_2\): 随机交换区间\([l_1,r_1]\)和\([l_ ...

  3. CF895 E. Eyes Closed(线段树 期望)

    题意 Sol 今天考试的T3,,我本来留了一个小时去写.但是T2一刚就刚了两个小时 最后也没来的及写.. 然后考完 开始写,,25min就A了.. 感觉自己太高估自己的思维,太低估自己的码力了... ...

  4. codeforces895E. Eyes Closed

    题目链接 codeforces895E. Eyes Closed 题解 线段树维护期望和 写出修改两区间的相互影响 就是一个区间修改 emmm考试的代码过不去,这么松的spj都过不去Orz,每次和答案 ...

  5. Codeforces 895.E Eyes Closed

    E. Eyes Closed time limit per test 2.5 seconds memory limit per test 256 megabytes input standard in ...

  6. Codeforces 895E Eyes Closed(线段树)

    题目链接  Eyes Closed 题意  两个人玩一个游戏,现在有两种操作: 1.两个人格子挑选一个区间,保证两个的区间不相交.在这两个区间里面各选出一个数,交换这两个数. 2.挑选一个区间,求这个 ...

  7. 懒加载session 无法打开 no session or session was closed 解决办法(完美解决)

           首先说明一下,hibernate的延迟加载特性(lazy).所谓的延迟加载就是当真正需要查询数据时才执行数据加载操作.因为hibernate当中支持实体对象,外键会与实体对象关联起来.如 ...

  8. 4.Android 打包时出现的Android Export aborted because fatal error were founds [closed]

    Android 程序开发完成后,如果要发布到互联网上供别人使用,就需要将自己的程序打包成Android 安装包文件(Android Package,APK),其扩展名为.apk.使用run as 也能 ...

  9. 【BZOJ-3143】游走 高斯消元 + 概率期望

    3143: [Hnoi2013]游走 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2264  Solved: 987[Submit][Status] ...

随机推荐

  1. [Xcode 实际操作]八、网络与多线程-(16)使用网址会话对象URLSession下载图片并显示下载进度

    目录:[Swift]Xcode实际操作 本文将演示如何通过网址会话对象URLSession显示下载图片的进度. 网址会话对象URLSession具有在后台上传和下载.暂停和恢复网络操作.丰富的代理模式 ...

  2. 新建Podfile命令

    接下来,你需要建立一个主工程.建立成功以后,再次启动终端, 利用cd命令进入到工程文件夹内,此时需要创建一个特殊的文本文件,命令如下: 命令: touch Podfile 创建 命令: open -e ...

  3. ssh 下载文件以及上传文件到服务器

    https://blog.csdn.net/jackghq/article/details/64124062 scp john@192.168.1.100:~/Desktop/MHN_error_so ...

  4. Kera高层API

    目录 Keras != tf.keras Outline1 Metrics Step1.Build a meter Step2.Update data Step3.Get Average data C ...

  5. Linux之expect非交互式功能

    我在上一篇博文linux之SSH密钥认证 提过ssh之间的相互认证,但是每次使用ssh登录到其它服务器还是要输入密码的. expect是用于提供自动交互的工具,自动连接被管理的服务器,不需要手动输入密 ...

  6. JSON脱敏

    https://blog.csdn.net/yuan487639/article/details/79151344

  7. Eclipse mybatis中XML的自动提示

    1. 从mybatis-3.1.1.jar中解压出dtd文件 2. 3. 复制红线处内容 4. 将上步中内容粘贴到key中

  8. [題解](最小生成樹)luogu_P1265

    首先考虑最小生成树的模型,唯一不同的是第二种情形. 即“三个或三个以上的城市申请修建的公路成环” 考虑该情形,因为修路的申请是申请离它最近的城市,所以上述条件实质上为 “存在三个或三个以上的城市,他们 ...

  9. 如何使用LESS 深度定制Bootstrap

    一.LESS是什么? Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. 中文介绍:http://lesscss.cn/ 有 ...

  10. 多线程:『GCD』详尽总结

    本文用来介绍 iOS 多线程中 GCD 的相关知识以及使用方法.这大概是史上最详细.清晰的关于 GCD 的详细讲解+总结的文章了.通过本文,您将了解到:1. GCD 简介2. GCD 任务和队列3. ...