#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. java泛型学习(一)

    泛型也叫做参数化类型,顾名思义的去理解,就是把类型作为一个参数.类比方法的传参,我们举个例子. class A{ public void getX(int x){ System.out.println ...

  2. Solr4.10与tomcat整合并安装中文分词器

    1.solr Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器.Solr提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展,并对索引. ...

  3. JavaScript类型比较

    JavaScript的类型 原始类型: number string boolean null undefined 对象类型: Object function Array Date ... 隐式转换 + ...

  4. mongodb 复制集

    mongodb 复制集 复制集简介 Mongodb复制集由一组Mongod实例(进程)组成,包含一个Primary节点和多个Secondary节点,Mongodb Driver(客户端)的所有数据都写 ...

  5. iOS 通知的变化ios9-10,新功能展示

    二.新功能展示 1  使用 /iOS通知新功能玩法 2.  全面   iOS10里的通知与推送详情 一.变化 四.Notification(通知) 自从Notification被引入之后,苹果就不断的 ...

  6. Mac 终端—不同文件不同颜色显示,提示文字

    原文地址 修改Mac终端(Terminal)里不同类型文件的显示颜色 修改Mac终端(Terminal)的提示文字 Mac终端显示/隐藏文件命令 1. 修改Mac终端(Terminal)里不同类型文件 ...

  7. java.util.ConcurrentHashMap (JDK 1.8)

    1.1 java.util.ConcurrentHashMap继承结构 ConcurrentHashMap和HashMap的实现有很大的相似性,建议先看HashMap源码,再来理解Concurrent ...

  8. 每周.NET前沿技术文章摘要(2017-05-17)

    汇总国外.NET社区相关文章,覆盖.NET ,ASP.NET等内容: .NET .NET Framework 4.7正式发布 链接: http://www.infoq.com/cn/news/2017 ...

  9. ArcGIS 网络分析[8.4] 资料4 聚合——创建及打开网络数据集的类实现

    这篇是对前三篇的总结,因为网络数据集涉及的"点"太多了,我只能挑重点来设置,大家明白框架后可以自行寻求帮助文档添加功能. 我以C#类的形式给出,这个类包含很多种方法,因为本人的C# ...

  10. nova创建虚拟机源码分析系列之八 compute创建虚机

    /conductor/api.py _build_instance()  /conductor/rpcapi.py  _build_instance() 1 构造一些数据类型2 修改一些api版本信息 ...