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. hdu 2829 Lawrence(四边形不等式优化dp)

    T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...

  2. jforum(1)--环境搭建

    JForum 是采用Java开发的功能强大且稳定的论坛系统.它提供了抽象的接口.高效的论坛引擎以及易于使用的管理界面,同时具有完全的权限控制.多语言支持(包括中文).高性能.可自定义的用户接口.安全. ...

  3. NOIp2018提高组 双栈排序

    这真是道神奇的题目: 原题链接 首先我们要证明以下的性质: 若原序列为\(\{a_n\}\),\(a_i\)和\(a_j\)不能同时放入一个栈中,当且仅当\(i<j,a_i<a_j\),且 ...

  4. codeforces-1140 (div2)

    A.维护一个前缀最大值,不断跳即可 #include <map> #include <set> #include <ctime> #include <cmat ...

  5. Pandas系列(五)-分类数据处理

    内容目录 1. 创建对象 2. 常用操作 3. 内存使用量的陷阱 一.创建对象 1.基本概念:分类数据直白来说就是取值为有限的,或者说是固定数量的可能值.例如:性别.血型. 2.创建分类数据:这里以血 ...

  6. PLSQL Developer 远程连接Oracle数据库

    下载.安装.激活PLSQL Developer,本例使用 PLSQL Developer 13 (64 bit). 下载Oracle即时客户端 Instant Client,本例使用11gR2 链接: ...

  7. Coursera, Big Data 3, Integration and Processing (week 1/2/3)

    This is the 3rd course in big data specification courses. Data model reivew 1, data model 的特点: Struc ...

  8. Entity Framework查询

    Entity Framework是个好东西,虽然没有Hibernate功能强大,但使用更简便.今天整理一下常见SQL如何用EF来表达,Func形式和Linq形式都会列出来(本人更喜欢Func形式). ...

  9. 405 css样式的研究 list-style-type 属性研究

    CSS 列表的样式 list-style-type.list-style-position和list-style-image 属性 在CSS中,列表元素是一个块框,列表中的每个表项也是一个块框,只是在 ...

  10. C# - 常用接口

    常用接口 用于比较接口 IComparable<T> 接口内部定义了用于比较两个对象大小的CompareTo(T t)方法,>参数时返回1,=参数时返回0,<参数时返回-1.集 ...