Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树
题解看这里 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 动态开点线段树的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #539 (Div. 1) E - Sasha and a Very Easy Test 线段树
如果mod是质数就好做了,但是做除法的时候对于合数mod可能没有逆元.所以就只有存一下mod的每个质因数(最多9个)的幂,和剩下一坨与mod互质的一部分.然后就能做了.有点恶心. CODE #incl ...
- 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 ...
- Codeforces Round #539 (Div. 2)
Codeforces Round #539 (Div. 2) A - Sasha and His Trip #include<bits/stdc++.h> #include<iost ...
随机推荐
- vscode配置phpxdebug
打debug还是很有必要的,以前嫌麻烦,现在觉得,通过debug可以看自己写的代码的执行的逻辑,更容易理清别人代码的逻辑. 步骤: 下载phpdebug插件 查看自己的php版本信息,下载对应的deb ...
- Python3 使用pygal 生成世界人口地图
最近在看<python从入门到实践>,其中有一个例子是使用pygal制作世界人口地图,觉得挺有意思的,这里就记录下来了, 其实代码不是很复杂,使用环境环境python3.废话不多说,直接上 ...
- CAS 5.x搭建常见问题系列(2).PKIX path building failed
错误原因 服务端的证书是不安全的,Cas的客户端在调用时因为安全提醒造成调用失败. CAS的客户端需要导入服务端的证书后,就正常了. 具体操作步骤如下: 1. 首先启动tomcat,看下之前搭建的ca ...
- (二)ActiveMQ之点对点消息实现
一.点对点消息实现概念 在点对点或队列模型下,一个生产者向一个特定的队列发布消息,一个消费者从该队列中读取消息.这里,生产者知道消费者的队列,并直接将消息发送到消费者的队列.这种模式被概括为:只有一个 ...
- Python中的一些常用模块1
OS模块,sys模块,time模块,random模块,序列化模块 os模块是与操作系统交互的一个接口 OS模块简单的来说是一个Python的系统编程操作模块,可以处理文件和目录这些我们日常手动需要做的 ...
- js之向div contenteditable光标位置添加字符
js之向div contenteditable光标位置添加字符 原理: 在HTML里面,光标是一个对象,光标对象是只有当你选中某个元素的时候才会出现的. 当我们去点击一个输入框的时候,实际上它会产生 ...
- S2-045、S2-046
前言 S2-045依然是一个Ognl表达式注入导致的RCE漏洞,且漏洞很严重.另外,还是建议读者阅读本篇文章前先看下系列文章的第一篇. 正文 依然是第一篇文章中讲过,StrutsPrepareFilt ...
- Struts2系列漏洞起始篇
前言 到目前位置struts2的漏洞编号已经到了S2-057,一直想系统的学习下Struts2的漏洞,但由于工作量较大,一直搁浅.最近由于工作需要,借此机会来填下坑.个人认为一个框架漏洞出来了仅仅看下 ...
- leetcode-55. Jump Game · Array
题面 这个题面挺简单的,不难理解.给定非负数组,每一个元素都可以看作是一个格子.其中每一个元素值都代表当前可跳跃的格子数,判断是否可以到达最后的格子. 样例 Input: [2,3,1,1,4] Ou ...
- goroutine的设计与实现
goroutine背后的系统知识 http://www.sizeofvoid.net/goroutine-under-the-hood/ 下周写完