题目链接

利用期望的线性性质:

\(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. 第二篇 HTML5打包发布IOS APP之苹果开发者账号申请流程

    打包技术转移到了公众号

  2. (十一)SpringBoot导出excel文件

    一:添加POI依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-oox ...

  3. assembly x86(nasm)画三角形等图形的实现(升级版)

    https://www.cnblogs.com/lanclot-/p/10962702.html接上一篇 本来就有放弃的想法,可是有不愿退而求次, 然后大神室友写了一个集海伦公式计算三角形面积, 三点 ...

  4. 如何利用python制作微信好友头像照片墙?

    这个不难,主要用到itchat和pillow这2个库,其中itchat用于获取微信好友头像照片,pillow用于拼接头像生成一个照片墙,下面我简单介绍一下实现过程,代码量不多,也很好理解,实验环境wi ...

  5. js实现打印正三角

    代码: <html> <head> <title>function</title> </head> <body> <scr ...

  6. iOS 更改状态栏、导航栏颜色的几种方法

    ios上状态栏 就是指的最上面的20像素高的部分状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时间等部分:背景部分:就是显示黑色或者图片的背景部分: (一)设置sta ...

  7. Zynq7000开发系列-4(新:Xilinx交叉编译环境搭建)

    一.前言  本来上一篇文章已经讲了Xilinx交叉编译环境的搭建,但是我在后续的使用中发现:使用2011年版本的交叉编译链编译OpenCV 3.1.0时出现错误: 网络搜索一番,查明是交叉编译链的问题 ...

  8. TopCoder9915(期望dp)

    1.还是逆向. 2.状态是还剩红i黑j张时的期望,这样从0,0往R,B推.注意因为是逆着的,所以到了某一步发现期望为负时直接f[i][j]归零,意义是这之后(在递推中算是这之前)的都不摸了,到这就停( ...

  9. O(nlogn)求逆序数对的个数

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  10. freertos之资源管理学习

    OS下在对硬件外设资源操作.多任务的共享变量.任务和中断的共享变量操作时需要考虑资源的完整性和安全性. FREERTOS提供了临界区.调度器上锁.互斥量.优先级自动继承.创建守护任务的方法来改变最小优 ...