#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. 四.RabbitMQ之发布/订阅(Publish/Subscribe)

    一.基础知识点 在上述章节中,我们理解的RabbitMQ是基于如下这种模式运作的. 而事实上,这只是我们简单化了的模型的结果,真正的模型应该是这样的. P:Producer 生产者,生产消息,把它放进 ...

  2. android studio 使用adb命令传递文件到android设备

    一:文件传输 在android开发中,有时候需要将文件从pc端传递至android,或者将软件运行的日志,从android设备传递到pc进行分析,我们可以使用windows的cmd窗口,或者andro ...

  3. OC学习16——对象归档

    转载自  OC学习篇之---归档和解挡 OC中的归档就是将对象写入到一个文件中,Java中的ObjectInputStream和ObjectOutputStream来进行操作的.当然在操作的这些对象都 ...

  4. Java.util.Map排序输出

    在java的众多Map实现中,Map基本上是不能保证顺序的(LinkedHashMap可以保证插入顺序或者访问顺序,TreeMap默认按照key升序但可以自定义Comparator),在开发过程中当数 ...

  5. MySQL Group Replication 动态添加成员节点

    前提: MySQL GR 3节点(node1.node2.node3)部署成功,模式定为多主模式,单主模式也是一样的处理. 在线修改已有GR节点配置 分别登陆node1.node2.node3,执行以 ...

  6. 线上平滑升级nginx1.12

    .下载相关包,需要和之前用到的依赖包保持一致 wget http://nginx.org/download/nginx-1.12.2.tar.gz wget https://bitbucket.org ...

  7. SQL语句 我喜欢上海

    select * from [user] wherer name like '上海%'

  8. UWP 应用通知Notifications

    之前说UWP 使用OneDrive云存储2.x api(二)[全网首发],微识别实现了上传下载的功能,那么为了给用户更上一层楼的体验,那就是在上传下载完成之后,弹出一通知Notifications. ...

  9. Java自己动手写连接池一

    自己动手写连接池,废话不多说,直接上代码,读取配置文件 package com.kama.cn; import java.io.IOException;import java.io.InputStre ...

  10. Windows下使用OpenSSL生成自签证书

    下载OpenSSLhttp://slproweb.com/products/Win32OpenSSL.html 生成证书 生成crt证书CMD进入安装bin目录,执行命令:openssl req -x ...