codeforces895E. Eyes Closed
题目链接
题解
线段树维护期望和
写出修改两区间的相互影响
就是一个区间修改
emmm考试的代码过不去,这么松的spj都过不去Orz,每次和答案差0.001*ans左右...
可能是我每次直接对区间暴力统计右区间的影响,然后直接打个修改区间tag....
求解
代码
std:
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <complex>
#define inf 0x3f3f3f3f
#define eps 1e-10
#define lc k << 1
#define rc k << 1 | 1
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
ll p;
double tmp[200005];
struct node{
double dat, tag1, tag2;
int l, r;
};
struct seg{
node d[800005];
void pushup(int k){
d[k].dat = d[lc].dat + d[rc].dat;
}
void build(int k, int l, int r){
d[k].l = l; d[k].r = r; d[k].tag1 = 0; d[k].tag2 = 1;
if(l == r){
d[k].dat = tmp[l];
return;
}
int mid = (l + r) >> 1;
build(lc, l, mid);
build(rc, mid + 1, r);
pushup(k);
}
void add(int k, double x){
double len = d[k].r - d[k].l + 1;
d[k].dat = (d[k].dat + x * len);
d[k].tag1 = (d[k].tag1 + x);
}
void mul(int k, double x){
d[k].dat = d[k].dat * x;
d[k].tag1 = d[k].tag1 * x;
d[k].tag2 = d[k].tag2 * x;
}
void pushdown(int k){
if(fabs(d[k].tag2 - 1) > eps){
mul(lc, d[k].tag2);
mul(rc, d[k].tag2);
d[k].tag2 = 1;
}
if(fabs(d[k].tag1) > eps){
add(lc, d[k].tag1);
add(rc, d[k].tag1);
d[k].tag1 = 0;
}
}
void add(int k, int l, int r, double x){
if(l <= d[k].l && d[k].r <= r){
add(k, x); return;
}
pushdown(k);
int mid = (d[k].l + d[k].r) >> 1;
if(l <= mid) add(lc, l, r, x);
if(r > mid) add(rc, l, r, x);
pushup(k);
}
void mul(int k, int l, int r, double x){
if(l <= d[k].l && d[k].r <= r){
mul(k, x); return;
}
pushdown(k);
int mid = (d[k].l + d[k].r) >> 1;
if(l <= mid) mul(lc, l, r, x);
if(r > mid) mul(rc, l, r, x);
pushup(k);
}
double query(int k, int l, int r){
if(l <= d[k].l && d[k].r <= r){
return d[k].dat;
}
pushdown(k); double sum = 0;
int mid = (d[k].l + d[k].r) >> 1;
if(l <= mid) sum = (sum + query(lc, l, r));
if(r > mid) sum = (sum + query(rc, l, r));
return sum;
}
}Seg;
int n, m;
int main(){
//freopen("random.in", "r", stdin);freopen("random.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++) scanf("%lf", &tmp[i]);
Seg.build(1, 1, n);
while(m--){
int opt, l1, r1, l2, r2; ll x;
scanf("%d", &opt);
if(opt == 1){
scanf("%d%d%d%d", &l1, &r1, &l2, &r2);
double d1 = Seg.query(1, l1, r1);
double d2 = Seg.query(1, l2, r2);
Seg.mul(1, l1, r1, double(r1 - l1) / double(r1 - l1 + 1));
Seg.mul(1, l2, r2, double(r2 - l2) / double(r2 - l2 + 1));
Seg.add(1, l1, r1, d2 / double(r1 - l1 + 1) / double(r2 - l2 + 1));
Seg.add(1, l2, r2, d1 / double(r2 - l2 + 1) / double(r1 - l1 + 1));
}
if(opt == 2){
scanf("%d%d", &l1, &r1);
printf("%.8lf\n", Seg.query(1, l1, r1));
}
}
return 0;
}
mycode
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
inline int read() {
int x = 0 ,f = 1;
char c = getchar();
while(c > '9' || c < '0') c = getchar();
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
int n,m;
#define double long double
const int maxn = 2000007;
int a[maxn];
double t[maxn << 1] ;
double tag[maxn << 1];
int tagl[maxn << 1],tagr[maxn << 1];
void update(int x) {
t[x] = t[x << 1] + t[x << 1 | 1];
}
double merge(double c1,double c2,int l,int r,int t1,int t2) {
double k1 = 1.0 * r - l + 1, k2 = 1.0 * t2;
return c1 * (k1 - 1) / k1 + c2 /t2 ;
}
void pushdown(int x,int l,int r) {
int mid = l + r >> 1;
t[x << 1] = merge(t[x << 1] , tag[x] , l , mid , tagl[x] , tagr[x]);
t[x << 1 | 1] = merge(t[x << 1 | 1] , tag[x] , mid + 1,r , tagl[x] , tagr[x]);
tag[x << 1] = tag[x << 1 | 1] = tag[x];
tagl[x << 1] = tagl[x << 1 | 1] = tagl[x];
tagr[x << 1] = tagr[x << 1 | 1] = tagr[x];
tagl[x] = tagr[x] = 0; tag[x] = 0.0;
}
void build(int x,int l,int r) {
if(l == r) { t[x] = a[l]; return; }
int mid = l + r >> 1;
build(x << 1,l,mid); build(x << 1 | 1,mid + 1,r);
update(x);
}
double Query(int x,int l,int r,int L,int R) {
if(tag[x] != 0.0 && tagl[x] && tagr[x]) pushdown(x,l,r);
if(l >= L && r <= R) return t[x];
double ret = 0;
int mid = l + r >> 1;
if(L <= mid) ret += Query(x << 1,l,mid,L,R);
if(R > mid ) ret += Query(x << 1 | 1,mid + 1,r,L,R);
return ret;
//update(x);
}
void modify(int x,int l,int r,int L,int R,double CC,int t1,int t2) {
if(tag[x] != 0.0&& tagr[x] && tagl[x]) pushdown(x,l,r);
if(l >= L && r <= R) {
t[x] = merge(t[x],CC,l,r,t1,t2);
if(l != r) {
tag[x] = CC;
tagl[x] = t1;
tagr[x] = t2;
}
return ;
}
int mid = l + r >> 1;
if(L <= mid) modify(x << 1,l,mid,L,R,CC, t1,t2);
if(R > mid ) modify(x << 1 | 1,mid + 1,r,L,R, CC,t1,t2);
update(x);
}
int main() {
//freopen("random.in","r",stdin); freopen("random.out","w",stdout);
n = read(),m = read();
for(int i = 1;i <= n;++ i) a[i] = read();
build(1,1,n);
for(int type,l,r,l1,r1,i = 1;i <= m;++ i) {
type = read();
if(type == 1) {
l = read(),r = read();
l1 = read();r1 = read();
double q1 = Query(1,1,n,l,r);
double q2 = Query(1,1,n,l1,r1);
modify(1,1,n,l,r,q2 , r - l + 1 , r1 - l1 + 1);
modify(1,1,n,l1,r1,q1 , r1 - l1 + 1 , r - l + 1);
}
else {
l = read(),r = read();
//printf("%llf\n",Query(1,1,n,l,r));
std::cout << Query(1,1,n,l,r) << std::endl;
}
}
return 0;
}
/*
4 4
1 1 2 2
1 2 2 3 3
2 1 2
1 1 2 3 4
2 1 2
*/
codeforces895E. Eyes Closed的更多相关文章
- 懒加载session 无法打开 no session or session was closed 解决办法(完美解决)
首先说明一下,hibernate的延迟加载特性(lazy).所谓的延迟加载就是当真正需要查询数据时才执行数据加载操作.因为hibernate当中支持实体对象,外键会与实体对象关联起来.如 ...
- 4.Android 打包时出现的Android Export aborted because fatal error were founds [closed]
Android 程序开发完成后,如果要发布到互联网上供别人使用,就需要将自己的程序打包成Android 安装包文件(Android Package,APK),其扩展名为.apk.使用run as 也能 ...
- Resource leak: 'context' is never closed
from: http://stackoverflow.com/questions/14184059/spring-applicationcontext-resource-leak-context-is ...
- 开放封闭原则(Open Closed Principle)
在面向对象的设计中有很多流行的思想,比如说 "所有的成员变量都应该设置为私有(Private)","要避免使用全局变量(Global Variables)",& ...
- ORA-12537: TNS:connection closed
http://www.vitalsofttech.com/ora-12537-tnsconnection-closed/ Question: When trying to establish a sq ...
- EX:The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
EX:The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secu ...
- SQLExecption:Operation not allowed after ResultSet closed解决办法
原网址:http://blog.csdn.net/sku0923/article/details/1722370 一个stmt多个rs进行操作引起的ResultSet已经关闭错误 一个stmt多个rs ...
- .Net关闭数据库连接时判断ConnectionState为Open还是Closed?
两种写法: if (conn.State == System.Data.ConnectionState.Open) { conn.Close(); ...
- Unable to find element on closed window (WARNING: The server did not provide any stacktrace information)
当你的selenium WebDriver 启动IE11报这个错时:Unable to find element on closed window (WARNING: The server did n ...
随机推荐
- c++动态库封装及调用(3、windows下动态库调用)
1.DLL的隐式调用 隐式链接采用静态加载的方式,比较简单,需要.h..lib..dll三件套.新建“控制台应用程序”或“空项目”.配置如下: 项目->属性->配置属性->VC++ ...
- 【API】API函数创建用户,添加到管理组
1 学习目标 使用API添加用户可以绕过某些杀毒软件的限制. 2 编程思路 2.1 代码原理 使用NetUserAdd这个API添加普通权限的用户,NetLocalGroupAddMembers这个A ...
- shell script中read的用法
1.read基本读取 #!/bin/bash #testing the read command echo -n "Enter you name:" #echo -n 让用户直接在 ...
- MariaDB:登陆报错:mysqladmin: connect to server at 'localhost' failed
见图: 解决办法: /etc/init.d/mysqld stop mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended ...
- centos7 部署 open-falcon 0.2.1
=============================================== 2019/4/28_第1次修改 ccb_warlock 更新 ...
- vue系列之webstrom的设置
1.安装vue插件,方法 Setting->Plugins,点击Plugins,在右边输入vue,找到相应插件,然后安装 2.创建vue模板 注意红圈里面的 3.设置vue文件支持的样式 注意: ...
- MySQL 由 5.7 升级为 8.0 之后,Laravel 的配置改动
开发机上升级了 MySQL 8.0, 原有的 Laravel 5.5 项目就启动失败了. 报错信息是: [2018-05-30 11:17:37] local.ERROR: SQLSTATE[4200 ...
- Codeforces 407B Long Path(好题 DP+思维)
题目链接:http://codeforces.com/problemset/problem/407/B 题目大意:一共n+1个房间,一个人从1走到n+1,每次经过房间都会留下一个标记,每个房间有两扇门 ...
- 我靠,上班eclipse看糗事百科
package test; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; ...
- JavaScrit全面总结
前端技术的发展变化太快了,各种各样的框架.再变也离不开js.所以,在此把js的高级语法总结一遍. js解析和执行包括:全局和函数 一:全局预处理 <script type="te ...