#448 Div2 E

题意

给出一个数组,有两种类型操作:

  1. 选定不相交的两个区间,分别随机挑选一个数,交换位置。
  2. 查询区间和的期望。

分析

线段树区间更新区间求和。

既然是涉及到两个区间,那么对于第一个区间而言,它的和的期望由两部分组成:它剩下的数的和的期望,从第二个区间换过来的数的和的期望。第二个区间同理。

可以用两个数组分别(间接)维护这两部分的值(类似于线段树中常见的 lazy 数组)。

code

#include<bits/stdc++.h>
using namespace std;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
const int N = 4e5 + 10;
double a[N], b[N], c[N];
void pushDown(int l, int r, int rt) {
int m = l + r >> 1;
b[rt << 1] *= b[rt];
c[rt << 1] = c[rt << 1] * b[rt] + c[rt];
a[rt << 1] = c[rt] * (m - l + 1) + a[rt << 1] * b[rt];
b[rt << 1 | 1] *= b[rt];
c[rt << 1 | 1] = c[rt << 1 | 1] * b[rt] + c[rt];
a[rt << 1 | 1] = c[rt] * (r - m) + a[rt << 1 | 1] * b[rt];
b[rt] = 1;
c[rt] = 0;
}
void build(int l, int r, int rt) {
b[rt] = 1;
if(l == r) scanf("%lf", &a[rt]);
else {
int m = l + r >> 1;
build(lson);
build(rson);
a[rt] = a[rt << 1] + a[rt << 1 | 1];
}
}
void update(double bb, double cc, int L, int R, int l, int r, int rt) {
if(l >= L && r <= R) {
b[rt] *= bb;
c[rt] = c[rt] * bb + cc;
a[rt] = cc * (r - l + 1) + a[rt] * bb;
} else {
pushDown(l, r, rt);
int m = l + r >> 1;
if(L <= m) update(bb, cc, L, R, lson);
if(R > m) update(bb, cc, L, R, rson);
a[rt] = a[rt << 1] + a[rt << 1 | 1];
}
}
double query(int L, int R, int l, int r, int rt) {
if(l >= L && r <= R) return a[rt];
else {
pushDown(l, r, rt);
int m = l + r >> 1;
double res = 0;
if(L <= m) res += query(L, R, lson);
if(R > m) res += query(L, R, rson);
return res;
}
}
int main() {
int n, q;
cin >> n >> q;
build(1, n, 1);
while(q--) {
int idx, w[4];
cin >> idx;
for(int i = 0; i < 6 - 2 * idx; i++) scanf("%d", &w[i]);
if(idx == 1) {
double c1 = query(w[2], w[3], 1, n, 1) / ((double)w[1] - w[0] + 1), c2 = query(w[0], w[1], 1, n, 1) / ((double)w[3] - w[2] + 1);
update(((double)w[1] - w[0]) / ((double)w[1] - w[0] + 1), c1 / ((double)w[3] - w[2] + 1), w[0], w[1], 1, n, 1);
update(((double)w[3] - w[2]) / ((double)w[3] - w[2] + 1), c2 / ((double)w[1] - w[0] + 1), w[2], w[3], 1, n, 1);
} else {
printf("%.8f\n", query(w[0], w[1], 1, n, 1));
}
}
return 0;
}

Codeforces #448 Div2 E的更多相关文章

  1. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  2. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  3. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  4. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  5. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  6. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  7. CodeForces:#448 div2 B. XK Segments

    传送门:http://codeforces.com/contest/895/problem/B B. XK Segments time limit per test1 second memory li ...

  8. CodeForces:#448 div2 a Pizza Separation

    传送门:http://codeforces.com/contest/895/problem/A A. Pizza Separation time limit per test1 second memo ...

  9. CodeForces 448

    A:Rewards: 题目链接:http://codeforces.com/problemset/problem/448/A 题意:Bizon有a1个一等奖奖杯,a2个二等奖奖杯,a3个三等奖奖杯,b ...

随机推荐

  1. MPSOC之3——centos环境配置及petalinux安装及使用

    ubuntu虽然能正常安装,但是build时会出现闪退情况,闪退后一切归零,没啥错误提示,改用centos来安装petalinux. 0.环境 vmware pro 14,centos 7.3 pet ...

  2. IntelliJ IDEA 2017 注册方法

    本文使用破解方式注册. JetbrainsCrack-2.6.2.jar适用于ideaIU-2017.2.之前版本,若下载的版本较新破解文件可能无法使用,破解时一闪而退. 其中JetbrainsCra ...

  3. intellij idea的安装步骤---经典

    安装IntelliJ IDEA 一.安装JDK 1 下载最新的jdk,这里下的是jdk-8u66 2 将jdk安装到默认的路径C:\Program Files\Java目录下 二.安装IntelliJ ...

  4. git更新失败Probably the path to Git executable is not valid

    git更新代码失败 检查setting配置,发现路径配置错误 找对git的安装目录,修改路径后保存即可

  5. Spark Streaming VS Flink Streaming

    引自:https://www.slideshare.net/datamantra/introduction-to-flink-streaming

  6. adb devices找不到设备解决办法

    问题现象: 解决办法: 1.在设备管理器Android Device中找到设备硬件Id USB\VID对应值: 3.找到.android目录,找到adb_usb.ini文件,如果没有此文件则新建一个, ...

  7. JavaScript基础5——关于ECMAscript的函数

    ECMAScript的函数概述(一般定义到<head>标签之间) (1)定义函数,JavaScript一般有三种定义函数方法: *第一种是使用function语句定义函数(静态方法) fu ...

  8. 基于阿里云的MQTT远程控制

    好久没有写博客了,眼看自己的项目就要快做完了,先分享一下基于MQTT的远程控制,自己买了一个阿里的云端,然后在云端上安装了一个MQTT服务器,其实是一不小心买了两个,所以准备贡献出来一个供大家使用, ...

  9. [C#]使用Redis来存储键值对(Key-Value Pair)

    本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 开发工具:VS2017 语言:C# DotNet版本:.Net FrameWork 4.5及以 ...

  10. window下nginx的常用命令

    window nginx 启动 常用命令 2016-05-04 11:11 214人阅读 评论(0) 收藏 举报 分类: nginx(5) 版权声明:本文为博主原创文章,未经博主允许不得转载. 启动 ...