#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. 【转】Win10下 python3和python2同时安装并解决pip共存问题

    1.下载python3和python2 进入python官网,链接https://www.python.org/ 选择Downloads--->Windows,点击进入就可以看到寻找想要的pyt ...

  2. Springboot-添加对jsp支持

    1,在项目的配置文件加入以下依赖 <dependency> <groupId>javax.servlet</groupId> <artifactId>j ...

  3. javascript中的异步 macrotask 和 microtask 简介

    javascript中的异步 macrotask 和 microtask 简介 什么是macrotask?什么是microtask?在理解什么是macrotask?什么是microtask之前,我们先 ...

  4. 《TCP-IP详解卷3:TCP 事务协议、HTTP、NNTP和UNIX域协议》【PDF】下载

    TCP-IP详解卷3:TCP 事务协议.HTTP.NNTP和UNIX域协议>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062539 ...

  5. scala写算法-用小根堆解决topK

    topK问题是指从大量数据中获取最大(或最小)的k个数,比如从全校学生中寻找成绩最高的500名学生等等. 本问题可采用小根堆解决.思路是先把源数据中的前k个数放入堆中,然后构建堆,使其保持堆序(可以简 ...

  6. 绝世emacs配置for Ubuntu

    反正过不了几天就要退役了,把emacs配置放出来造福(祸害)大众? 浓浓的OIER风格,除了方便打代码就没别的用处(F8并不这样认为?),只可惜windows下的弄丢了,只有Ubuntu下的. F1不 ...

  7. bzoj 2752: [HAOI2012]高速公路(road)

    Description Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这条高速公路上设立了许多收费站.Y901高速公路是一条由N-1段路以及N个收 ...

  8. 关于vue 框架与后台框架的混合使用的尝试------转载

    这几天我在研究前台框架和后台框架融合的问题,进行了一些尝试; 我前台选择的是 vue,当然也可以选择 react 等其他 mvvm 框架,不过 vue 对于我来说是最熟悉的; 后台话,我选择的是 ph ...

  9. Servlet与Jsp的结合使用实现信息管理系统一

    PS:1:先介绍一下什么是Servlet? Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地 ...

  10. du 命令详解

    du : show disk usage  作用:统计目录或文件所占用磁盘空间的大小. 语法:du 参数 选项 参数: -a 为每个制定文件显示磁盘使用情况, 或者为目录中每个文件显示各自磁盘使用情况 ...