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 线段树 + 二分的更多相关文章

  1. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  2. Codeforces Gym100543B 计算几何 凸包 线段树 二分/三分 卡常

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF-Gym100543B.html 题目传送门 - CF-Gym100543B 题意 给定一个折线图,对于每一条 ...

  3. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  4. 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, ...

  5. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

  6. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  7. hdu4614 线段树+二分 插花

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  8. 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块

    !!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...

  9. luogu4422 [COCI2017-2018#1] Deda[线段树二分]

    讨论帖:线段树二分的题..我还考场切过..白学 这题我一年前的模拟赛考场还切过,现在就不会了..好菜啊. 显然直接线段树拆成$\log n$个区间,然后每个区间在进行线段树二分即可. UPD:复杂度分 ...

随机推荐

  1. MongoDB和pymongo的CURD

    一.mongodb 1.介绍 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之 ...

  2. python代码格式

    1,函数名:不要大写,都用小写,单词之间用下划线分隔 2,注释:注释的时候#后面要带空格 3,方法与方法之间空格两行 4,使用方法时,里面的参数用  逗号和空格  隔开 5,使用方法时,里面的参数“= ...

  3. FFT什么的

    目录 多项式 多项式加法 多项式乘法 多项式的表示 系数表达 点值表达 系数形式表示的多项式的快速乘法 DFT&FFT&IDFT 单位复数根 DFT FFT IDFT 多项式乘法 蝶形 ...

  4. python学习日记(内置函数)

    目前所有内置函数 http://www.runoob.com/python3/python3-built-in-functions.html *菜鸟教程* 内置函数分类 作用域相关globals(). ...

  5. BZOJ2870 最长道路

    题意:给定树,有点权.求一条路径使得最小点权 * 总点数最大.只需输出这个最大值.5w. 解:树上路径问题,点分治. 考虑合并两个子树的时候,答案的形式是val1 * (d1 + d2),当1是新插入 ...

  6. POJ 1015 Jury Compromise(双塔dp)

    Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33737   Accepted: 9109 ...

  7. python中 yield的用法和生成器generator的说明

    详情: https://www.cnblogs.com/python-life/articles/4549996.html

  8. OpenStack虚拟机冷迁移与热迁移

    一.虚拟机迁移分析 openstacvk虚拟机迁移分为冷迁移和热迁移两种方式. 1.1冷迁移: 冷迁移(cold migration),也叫静态迁移.关闭电源的虚拟机进行迁移.通过冷迁移,可以选择将关 ...

  9. JQuery对象关系图

    上图转自:http://www.cnblogs.com/haogj/archive/2010/04/19/1715762.html 自定义函数示例: $.fn.jAccordionunfold = f ...

  10. [物理学与PDEs]第5章习题2 Jacobian 的物质导数

    验证 (3. 6) 式, 即证明 $$\bex \cfrac{\rd J}{\rd t}=J\Div_y {\bf v}. \eex$$ 证明: $$\beex \bea \cfrac{\rd J}{ ...