题解看这里 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. Time & Space Complexity

    Quick Sort: Time complexity: best case O(n*lgn), worst case O(n^2) Space complexity: Best case O(lgn ...

  2. 数据结构——java实现栈

    栈 定义: 栈是一种先进后出的数据结构,我们把允许插入和删除的一端称为栈顶,另一端称为栈底,不含任何元素的栈称为空栈 栈的java代码实现: 基于数组: import org.junit.jupite ...

  3. win10系统ping另一台电脑上虚拟机的IP

    刚刚因为虚拟机与主机没法互相ping通的事情,奋战到将近凌晨一点.现在把这个过程总结一下,以方便后加入该行业的广大IT精英. VMWare提供了三种工作模式:bridged(桥接模式).NAT(网络地 ...

  4. Django-redis配置cache和session

    CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", &q ...

  5. 利用贝叶斯算法实现手写体识别(Python)

    在开始介绍之前,先了解贝叶斯理论知识 https://www.cnblogs.com/zhoulujun/p/8893393.html 简单来说就是:贝叶斯分类是一类分类算法的总称,这类算法均以贝叶斯 ...

  6. js循环及for-in , for-of的区别

    循环 字符串遍历:可通过for-of遍历字符串 for-in:遍历对象自身可继承可枚举属性 Object.keys():返回对象自身可枚举属性的键组成的数组 Object.getOwnProperty ...

  7. 一步步用ABAP Development Tools连接SAP云平台上的ABAP编程环境

    使用ABAP Development Tools的项目创建向导: New->ABAP Cloud Project: Service Instance Connection,选择SAP Cloud ...

  8. Eclipse安装windowsbuilder

    详见:https://www.cnblogs.com/plusplus/p/9864708.html https://www.cnblogs.com/lsy-blogs/p/7717036.html ...

  9. 转: 解决idea工具下tomcat中文乱码问题

    在运行/调试 配置对话框的Startup/Connection面板中, 勾选Pass environment variables. 并添加一个environment variable, Name填 J ...

  10. MySQL数据库的二进制安装、源码编译和基础入门操作

    一.MySQL安装 (1)安装方式: 1 .程序包yum安装 优点:安装快,简单 缺点:定死了各个文件的地方,需要修改里边的相关配置文件,很麻烦 2 .二进制格式的程序包:展开至特定路径,并经过简单配 ...