Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树

时间:2022-02-13 14:02:27

题解看这里 liouzhou_101的博客园

更简洁的代码看这里:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define X first
#define Y second
inline void read(int &x) {
int flag = 1; char ch;
while(!isdigit(ch=getchar()))if(ch=='-')flag=-flag;
for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
x*=flag;
}
struct node {
node *ls, *rs;
bool cov; //是否被全覆盖
int val; //被覆盖的值
LL sum, lsum;
}*root;
inline node* Newnode() {
node *re = new node;
re->ls = re->rs = 0;
re->sum = re->lsum = re->val = re->cov = 0;
return re;
}
inline void upd(node *&p) { //update
if(!p->ls) p->ls = Newnode();
if(!p->rs) p->rs = Newnode();
p->sum = p->ls->sum + p->rs->sum;
p->lsum = min(p->ls->lsum, p->ls->sum + p->rs->lsum);
}
inline void cover(node *&p, int l, int r, int val) { //覆盖
if(!p) p = Newnode();
p->cov = 1, p->val = val;
p->sum = 1ll * (r-l+1) * val;
p->lsum = val >= 0 ? 0 : p->sum;
}
inline void pd(node *&p, int l, int r, int mid) { //pushdown
if(p->cov) {
cover(p->ls, l, mid, p->val);
cover(p->rs, mid+1, r, p->val);
p->val = p->cov = 0;
}
}
void Modify(node *&p, int l, int r, int x, int y, int val) {
//printf("(%d, %d)\n", l, r);
if(!p) p = Newnode();
if(x == l && y == r) {
cover(p, l, r, val);
return;
}
int mid = (l + r) >> 1;
pd(p, l, r, mid);
if(y <= mid) Modify(p->ls, l, mid, x, y, val);
else if(x > mid) Modify(p->rs, mid+1, r, x, y, val);
else Modify(p->ls, l, mid, x, mid, val), Modify(p->rs, mid+1, r, mid+1, y, val);
upd(p);
}
#define pdl pair<double, long long>
pdl query(node *&p, int l, int r, int x, int y, LL V) {
if(!p) p = Newnode();
if(l == x && r == y) {
if(V + p->lsum > 0) return pdl(-1, p->sum);
if(l == r || p->cov) return pdl(l-1.0*V/(p->sum/(r-l+1)), p->sum); //p->sum < 0
}
int mid = (l + r) >> 1;
pd(p, l, r, mid);
if(y <= mid) return query(p->ls, l, mid, x, y, V);
else if(x > mid) return query(p->rs, mid+1, r, x, y, V);
else {
auto l_ans = query(p->ls, l, mid, x, mid, V);
if(l_ans.X > 0) return l_ans;
else {
auto r_ans = query(p->rs, mid+1, r, mid+1, y, V + l_ans.Y);
return pdl(r_ans.X, l_ans.Y + r_ans.Y);
}
}
}
map<int, int>H;
int main () {
int L = 1, R = 1000000000, Q;
read(Q); H[L-1] = H[R+1] = 0;
root = Newnode();
int op, l, r, v;
while(Q--) {
read(op);
if(op == 1) {
read(l), read(v); H[l] = v;
auto it = H.find(l), nxt = it;
++nxt;
Modify(root, L, R, l, min(nxt->X-1, R), v);
}
else if(op == 2) {
read(l);
auto it = H.find(l), pre = it, nxt = it;
--pre, ++nxt;
Modify(root, L, R, l, min(nxt->X-1, R), pre->Y);
H.erase(l);
}
else {
read(l), read(r), read(v);
if(v == 0) printf("%d\n", l);
else {
auto it = H.lower_bound(l);
l = it->X;
if(l >= r) puts("-1");
else {
auto ans = query(root, L, R, l, r-1, v);
if(ans.X < 0) puts("-1");
else printf("%.8f\n", ans.X);
}
}
}
}
}

Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树的更多相关文章

  1. Codeforces Round &num;539 &lpar;Div&period; 1&rpar; 1109F&period; Sasha and Algorithm of Silence's Sounds LCT&plus;线段树 &lpar;two pointers&rpar;

    题解请看 Felix-Lee的CSDN博客 写的很好,不过最后不用判断最小值是不是1,因为[i,i]只有一个点,一定满足条件,最小值一定是1. CODE 写完就A,刺激. #include <b ...

  2. Codeforces Round &num;539 &lpar;Div&period; 2&rpar; - D&period; Sasha and One More Name(思维)

    Problem   Codeforces Round #539 (Div. 2) - D. Sasha and One More Name Time Limit: 1000 mSec Problem ...

  3. Codeforces Round &num;539 &lpar;Div&period; 2&rpar; - C&period; Sasha and a Bit of Relax(思维题)

    Problem   Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax Time Limit: 2000 mSec Problem ...

  4. Codeforces Round &num;535 &lpar;Div&period; 3&rpar; E2&period; Array and Segments &lpar;Hard version&rpar; 【区间更新 线段树】

    传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...

  5. Codeforces Round &num;539 &lpar;Div&period; 2&rpar; C&period; Sasha and a Bit of Relax&lpar;前缀异或和&rpar;

    转载自:https://blog.csdn.net/Charles_Zaqdt/article/details/87522917 题目链接:https://codeforces.com/contest ...

  6. Codeforces Round &num;539 &lpar;Div&period; 2&rpar; C Sasha and a Bit of Relax

    题中意思显而易见,即求满足al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar且l到r的区间长为偶数的这样的数对(l,r)的个数. 若al⊕al+1⊕…⊕amid=amid+1⊕amid ...

  7. Codeforces Round &num;539 &lpar;Div&period; 1&rpar; E - Sasha and a Very Easy Test 线段树

    如果mod是质数就好做了,但是做除法的时候对于合数mod可能没有逆元.所以就只有存一下mod的每个质因数(最多9个)的幂,和剩下一坨与mod互质的一部分.然后就能做了.有点恶心. CODE #incl ...

  8. Codeforces Round &num;169 &lpar;Div&period; 2&rpar; E&period; Little Girl and Problem on Trees dfs序&plus;线段树

    E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...

  9. Codeforces Round &num;539 &lpar;Div&period; 2&rpar;

    Codeforces Round #539 (Div. 2) A - Sasha and His Trip #include<bits/stdc++.h> #include<iost ...

随机推荐

  1. MySQL Performance-Schema&lpar;三&rpar; 实践篇

    前一篇文章我们分析了Performance-Schema中每个表的用途,以及主要字段的含义,比较侧重于理论的介绍.这篇文章我主要从DBA的角度出发,详细介绍如何通过Performance-Schema ...

  2. HTML中使用javascript解除禁止input输入框代码:

    转:表单中Readonly和Disabled的区别 参考资料: disabled和readonly区别: 参考博文1地址:http://blog.csdn.net/symgdwyh/article/d ...

  3. u-boot-2010&period;09移植(A)

    第一阶段 1.开发环境 系统:centOS6.5           linux版本:2.6.32         交叉编译器:buildroot-2012.08 以上工具已经准备好,具体安装步骤不再 ...

  4. html5 语义

    页面示意图

  5. FEE Development Essentials

    FEE Development Essentials JS Basic function call() and apply() func1.bind(thisObj,arg1...argn) Cust ...

  6. Linux FTP服务器搭建与使用

    一.vsftpd说明 LINUX下实现FTP服务的软件很多,最常见的有vsftpd,Wu-ftpd和Proftp等.Red Hat Enterprise Linux中默认安装的是vsftpd. 访问F ...

  7. Ajax原理与封装详解

    Ajax大家每天都在用,jquery库对Ajax的封装也很完善.很好用,下面我们看一下他的内部原理,并手动封装一个自己的Ajax库. 更多有关ajax封装及数据处理,请参看上海尚学堂<Ajax中 ...

  8. 异常小结:上一张图搞清楚Java的异常机制

    下面是Java异常类的组织结构,红色区域的异常类表示是程序需要显示捕捉或者抛出的. Throwable Throwable是Java异常的*类,所有的异常都继承于这个类. Error,Excepti ...

  9. PHP读写Excel

    PHP读写Excel PHP读写Excel可以通过第三方库phpexcel比较优雅地完成,由于PHP对于字符串处理的优势,读写PHP非常方便. 库导入 这里使用composer包管理工具,以下是配置信 ...

  10. 洛谷AC破百题纪念!