Codeforces 431E Chemistry Experiment 线段树 + 二分
维护一个权值线段树,然后二分答案。
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end() using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
vector<LL> oo;
int getPos(double x) {
return upper_bound(ALL(oo), x) - oo.begin();
}
int cnt[N << ]; LL sum[N << ];
void update(int p, int op, int l, int r, int rt) {
if(l == r) {
cnt[rt] += op;
sum[rt] += op * oo[p - ];
return;
}
int mid = (l + r) >> ;
if(p <= mid) update(p, op, lson);
else update(p, op, rson);
cnt[rt] = cnt[rt << ] + cnt[rt << | ];
sum[rt] = sum[rt << ] + sum[rt << | ];
} PLI query(int L, int R, int l, int r, int rt) {
if(L > R) return mk(, );
if(R < l || r < L) return mk(, );
if(L <= l && r <= R) return mk(sum[rt], cnt[rt]);
int mid = (l + r) >> ;
PLI tmpL = query(L, R, lson);
PLI tmpR = query(L, R, rson);
return mk(tmpL.fi + tmpR.fi, tmpL.se + tmpR.se);
} bool check(double tar, LL v) {
PLI ret = query(, getPos(tar), , SZ(oo), );
return ret.se * tar - ret.fi > v;
} int n, q;
LL qus[N][], h[N]; int main() {
scanf("%d%d", &n, &q);
for(int i = ; i <= n; i++) {
scanf("%d", &h[i]);
oo.push_back(h[i]);
}
for(int i = ; i <= q; i++) {
scanf("%d", &qus[i][]);
if(qus[i][] == ) {
scanf("%lld%lld", &qus[i][], &qus[i][]);
oo.push_back(qus[i][]);
} else {
scanf("%lld", &qus[i][]);
}
}
sort(ALL(oo));
oo.erase(unique(ALL(oo)), oo.end());
for(int i = ; i <= n; i++) update(getPos(h[i]), , , SZ(oo), );
for(int i = ; i <= q; i++) {
if(qus[i][] == ) {
int p = qus[i][], x = qus[i][];
update(getPos(h[p]), -, , SZ(oo), );
h[p] = x;
update(getPos(h[p]), , , SZ(oo), );
} else {
LL v = qus[i][];
double low = , high = 1e9 + v + ;
for(int o = ; o < ; o++) {
double mid = (low + high) / ;
if(check(mid, v)) high = mid;
else low = mid;
}
printf("%.15f\n", (low + high) / );
}
}
return ;
} /*
*/
Codeforces 431E Chemistry Experiment 线段树 + 二分的更多相关文章
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Codeforces Gym100543B 计算几何 凸包 线段树 二分/三分 卡常
原文链接https://www.cnblogs.com/zhouzhendong/p/CF-Gym100543B.html 题目传送门 - CF-Gym100543B 题意 给定一个折线图,对于每一条 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)
题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n n个数,然后求有多少个区间[l,r] 满足 a[l]+a[r]=max([l, ...
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- hdu4614 线段树+二分 插花
Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...
- 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块
!!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...
- luogu4422 [COCI2017-2018#1] Deda[线段树二分]
讨论帖:线段树二分的题..我还考场切过..白学 这题我一年前的模拟赛考场还切过,现在就不会了..好菜啊. 显然直接线段树拆成$\log n$个区间,然后每个区间在进行线段树二分即可. UPD:复杂度分 ...
随机推荐
- BZOJ4817[Sdoi2017]树点涂色——LCT+线段树
题目描述 Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色.Bob可能会进 ...
- css常用单位
css常用单位 本文来简单介绍下css的常用单位. 绝对长度单位 绝对长度单位代表一个物理测量. 像素px(pixels) 在web上,像素px是典型的度量单位,很多其他长度单位直接映射成像素.最终, ...
- centos7下使用LVM给系统硬盘扩容超详细
简单介绍: LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的 ...
- 深入理解JVM(3)——类加载机制
1.类加载时机 类的整个生命周期包括了:加载( Loading ).验证( Verification ).准备( Preparation ).解析( Resolution ).初始化( Initial ...
- Mock8 moco框架如何返回一个cookie信息
还是用之前的startupWithCookies.json这个文件,直接往里面添加上面的一个代码: [ { "description":"这是一个会返回cookies信息 ...
- expansion pattern ‘Frame&’ contains no argument packs
camera/CameraImpl.h::: error: expansion pattern ‘Frame&’ contains no argument packs void read_fr ...
- 我眼中的支持向量机(SVM)
看吴恩达支持向量机的学习视频,看了好几遍,才有一点的理解,梳理一下相关知识. (1)优化目标: 支持向量机也是属于监督学习算法,先从优化目标开始. 优化目标是从Logistics regressi ...
- 转:Mac操作技巧 | "键盘侠"必备快捷键
看到一篇网友整理的比较好的“Mac操作技巧 | "键盘侠"必备快捷键”,转载过来分享给大家!希望能有帮助. 更多专题,可关注小编[磨人的小妖精],查看我的文章,也可上[风云社区 S ...
- ZOJ Monthly, January 2018
A 易知最优的方法是一次只拿一颗,石头数谁多谁赢,一样多后手赢 #include <map> #include <set> #include <ctime> #in ...
- openstack项目【day23】:云计算介绍(一)
本节内容 为何选择云计算/云计算之前遇到的问题 什么是云计算 云服务模式 云应用形式 传统应用与云感知应用 一:为何选择云计算/云计算之前遇到的问题 一.有效解决硬件单点故障问题 单点故障是指某个硬件 ...