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 ...
随机推荐
- [转帖]详解oracle数据库唯一主键SYS_GUID()
详解oracle数据库唯一主键SYS_GUID() https://www.toutiao.com/i6728736163407856139/ 其实 需要注意 这里满不能截取 因为截取了 就不一定唯一 ...
- 注册中心Eureka 说明
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.SpringCloud将它集成在其子项 ...
- recover函数捕获异常
package main import ( //"fmt" "time" ) func test () { var m map[string]int m[&qu ...
- Fabric分支/版本切换问题
(以下示例是从 release-1.4 切换到 release-1.3) 首先将 $GOAPTH/src/github.com/hyperledger/ 下1.4版本的fabric-samples给删 ...
- django静态文件配置和使用
一.首先需要了解的知识点是: 1.出于对效率和安全的考虑,django管理静态文件的功能仅限于在开发阶段的debug模式下使用,且需要在配置文件的INSTALLED_APPS中加入django.con ...
- (五)sturts2+spring整合
一.Spring与Struts的整合 1.1:加入Spring的jar包.1.2:加入Struts的jar包.1.3:加入Struts与Spring的整合jar//struts2-spring-plu ...
- .NET Core 发布部署问题
运行环境 操作系统 开发工具 frameworks .Net Core SDK 版本 托管运行 本地 ...
- Android蓝牙遥控器APP关键代码 guihub项目
package com.car.demo; import java.io.IOException; import java.io.OutputStream; import java.util.UUID ...
- 冒泡(bubblesort)、选择排序、插入排序、快速排序
冒泡排序(bubblesort) 特点:通过换位置的方式,一直向上冒泡 package main import "fmt" func bubbleSortAsc(arrayA [] ...
- $.serializeArray()获取不到input的value值bug问题
今天修改form表单,发现有好几个input值保存不上,上网搜索了一下是$.serializeArray()获取不到disabled的值.如果想要让input元素变为不可用,可以把input设为rea ...