题解看这里 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 #539 (Div. 1) 1109F. Sasha and Algorithm of Silence's Sounds LCT+线段树 (two pointers)

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

  2. Codeforces Round #539 (Div. 2) - D. 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 #539 (Div. 2) - C. 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 #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】

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

  5. Codeforces Round #539 (Div. 2) C. Sasha and a Bit of Relax(前缀异或和)

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

  6. Codeforces Round #539 (Div. 2) 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 #539 (Div. 1) E - Sasha and a Very Easy Test 线段树

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

  8. Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树

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

  9. Codeforces Round #539 (Div. 2)

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

随机推荐

  1. Python笔记003-字符串(1)

    1. 字符串基本特点 很多人初学编程时,总是担心自己数学不行,潜意识里认为数学好才能更好编程.但实际上,大多数程序员打交道最多的是 “ 字符串 ” 而不是 “ 数字 ”.因为,编程时用来解决现实问题的 ...

  2. nginx 设置开机启动

    设置nginx开机启动chkconfig --add /etc/init.d/nginx chkconfig nginx on

  3. SAS学习笔记31 SAS随机分组方法及实现

    随机分组方法包括: 简单随机化(simple randomization) 区组随机化(block randomization) 分层随机化(stratified randomization) 分层区 ...

  4. (三)第一个 Hibernate项目

    1.创建java工程,并导入hibernate所需要的jar包 2.通过IDE构建一个基础的Hibernate工程. 产生    hibernate.cfg.xml的框架总配置文件.        H ...

  5. (十二) web服务与javaweb结合(3)

    一.需求 上一章节虽然将webservice和web项目绑定在了一起,但是还是不能共同一个端口,本章讲解webservice和web项目绑定且共同端口. 二.案例 2.1 创建web工程,并引入依赖 ...

  6. Docker 安装mysql5.6

    1.首先进入命令行现在mysql5.6镜像 E:\>docker pull mysql:5.6 2.把mysql的配置文件放入到本地,供后期做修改用 文件分别为:mysql.cnf 和 mysq ...

  7. vue中子组件的created、mounted钩子中获取不到props中的值问题

    父子组件通信 这个官网很清楚,也很简单,父组件中使用v-bind绑定传送,子组件使用props接收即可 例如: 父组件中: <template> <div> <head- ...

  8. py datetime

    python datetime模块strptime/strptime format常见格式命令- [python]2011-12-23   版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本 ...

  9. JS中的原型对象与构造器

    在Javascript中:原型对象是属于构造函数的,不属于实例:实例只能共享原型对象中的属性和方法(当然也可以有自己的属性和方法,或者覆盖原型中同名的属性和方法):构造器constructor属于原型 ...

  10. RestControllerAdvice,ControllerAdvice

    1.切记@RestControllerAdvice 和 @ControllerAdvice 不能放在common里,会不生效,还会引起子项目的全局异常失败. 所以这2个还是放在各自的子项目里去处理.一 ...