Codeforces #448 Div2 E
#448 Div2 E
题意
给出一个数组,有两种类型操作:
- 选定不相交的两个区间,分别随机挑选一个数,交换位置。
- 查询区间和的期望。
分析
线段树区间更新区间求和。
既然是涉及到两个区间,那么对于第一个区间而言,它的和的期望由两部分组成:它剩下的数的和的期望,从第二个区间换过来的数的和的期望。第二个区间同理。
可以用两个数组分别(间接)维护这两部分的值(类似于线段树中常见的 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的更多相关文章
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- CodeForces:#448 div2 B. XK Segments
传送门:http://codeforces.com/contest/895/problem/B B. XK Segments time limit per test1 second memory li ...
- CodeForces:#448 div2 a Pizza Separation
传送门:http://codeforces.com/contest/895/problem/A A. Pizza Separation time limit per test1 second memo ...
- CodeForces 448
A:Rewards: 题目链接:http://codeforces.com/problemset/problem/448/A 题意:Bizon有a1个一等奖奖杯,a2个二等奖奖杯,a3个三等奖奖杯,b ...
随机推荐
- 数据的随机抽取 及 jQuery补充效果(菜单、移动)
一.数据的随机抽取 都见过那种考试题从很多题中随机抽取几道的试卷吧,现在就要做这样的一个例子:从数据库中随机抽取几条数据出来显示(例如:一百中随机挑选50条) 随机挑选是要有提交数据的,所以肯定是要有 ...
- mybatis的#{}占位符和${}拼接符的区别
#{}占位符:占位 如果传入的是基本类型,那么#{}中的变量名称可以随意写 如果传入的参数是pojo类型,那么#{}中的变量名称必须是pojo中的属性.属性.属性- ${}拼接符:字符串原样拼接 如果 ...
- Android快速实现上传项目到Github
本文为skylinelin原创,转载请注明出处! 一.简介 现在在网上浏览关于Git的文章,基本上都是使用命令行(Git Bash),命令行效率是很高的,但是有一定的复杂性,现在我们看如何用AS来讲项 ...
- JS如何实现导航栏的智能浮动
<script language="javascript"> function smartFloat(obj) { var obj = docu ...
- 视觉SLAM
SLAM:Simultaneous Localization And Mapping.中文:同时定位与地图重建. 它是指搭载特定传感器的主体,在没有实验先验信息的情况下,于运动过程中建立环境的模型,同 ...
- 如何在yarn上运行Hello World(二)
在之前的一篇文章我们介绍了如何编写在yarn集群提交运行应用的AM的yarnClient端,现在我们来继续介绍如何编写在yarn集群控制应用app运行的核心模块 ApplicationMaster ...
- 对于group by 和 order by 并用 的分析
今天朋友问我一个sql查询. 需求是 找到idapi最近那条数据,说明idapi 是重复的,于是就简单的写了 SELECT * FROM `ag_alarm_history` group by ` ...
- 4、树莓派的中文:安装ftp,安装gcc,安装qt,编译qt程序,运行qt界面程序
本博文仅作本人操作过程的记录,留作备忘.自强不息 QQ1222698 1.安装FTP:sudo apt-get install vsftpd 2.配置FTP,修改,/etc/vsftpd.conf # ...
- ConcurrentHashMap 从Java7 到 Java8的改变
一.关于分段锁 集合框架很大程度减少了java程序员的重复劳动,然而,在Java多线程环境中,以线程安全的方式使用集合类是一个首先考虑的问题. 越来越多的程序员了解到了ConcurrentHashMa ...
- vue基础学习(三)
03-01 vue的生存周期-钩子函数 <style> [v-cloak]{display:none;} </style> <div id="box" ...