Codeforces 1109C 线段树
题意及思路:https://www.cnblogs.com/TinyWong/p/10400682.html
代码:
#include <bits/stdc++.h>
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
#define LL long long
#define db long double
#define INF 1e15;
using namespace std;
const int maxn = 200010;
LL t[maxn * 2];
LL add[maxn * 2];
map<LL, int> mp;
int m;
db ans, v;
struct OP{
LL id, x, y, z;
};
OP op[maxn];
set<int> s;
set<int>::iterator it, it1, it2;
struct SegmentTree {
LL sum, lsum, Set;
int l, r;
bool flag;
};
SegmentTree tr[maxn * 4];
void pushup(int o) {
tr[o].sum = tr[ls(o)].sum + tr[rs(o)].sum;
tr[o].lsum = min(tr[ls(o)].lsum, tr[ls(o)].sum + tr[rs(o)].lsum);
}
void maintain(int o, LL val) {
tr[o].sum = val * (t[tr[o].r + 1] - t[tr[o].l]);
tr[o].lsum = min(0ll, tr[o].sum);
tr[o].Set = val;
tr[o].flag = 1;
}
void pushdown(int o) {
if(tr[o].flag) {
maintain(ls(o), tr[o].Set);
maintain(rs(o), tr[o].Set);
tr[o].flag = 0;
}
}
void build(int o, int l, int r) {
tr[o].l = l, tr[o].r = r;
if(l == r) {
tr[o].flag = 0;
tr[o].sum = 0;
tr[o].lsum = 0;
return;
}
int mid = (l + r) >> 1;
build(ls(o), l, mid);
build(rs(o), mid + 1, r);
pushup(o);
}
void update(int o, int l, int r, int ql, int qr, LL val) {
if(l >= ql && r <= qr) {
maintain(o, val);
return;
}
pushdown(o);
int mid = (l + r) >> 1;
if(ql <= mid) update(ls(o), l, mid, ql, qr, val);
if(qr > mid) update(rs(o), mid + 1, r, ql, qr, val);
pushup(o);
}
void query(int o, int l, int r, int ql, int qr) {
if(ql > qr) return;
if(l == r) {
if(tr[o].lsum + v > 0) {
v += tr[o].sum;
return;
}
db tmp = v / (-((db)tr[o].Set));
if(tmp <= 0 || (tmp + t[l] > t[qr + 1])) {
v += tr[o].sum;
return;
}
ans = tmp + (db)t[l];
return;
}
pushdown(o);
int mid = (l + r) >> 1;
if(l >= ql && r <= qr) {
if(tr[o].lsum + v > 0) {
v += tr[o].sum;
return;
}
if(v + tr[ls(o)].lsum <= 0) {
query(ls(o), l, mid, ql, qr);
if(ans != -1) return;
}
v += tr[ls(o)].sum;
if(v + tr[rs(o)].lsum <= 0) {
query(rs(o), mid + 1, r, ql, qr);
if(ans != -1) return;
}
v += tr[rs(o)].sum;
return;
}
if(ql <= mid) query(ls(o), l, mid, ql, qr);
if(ans != -1) return;
if(qr > mid) query(rs(o), mid + 1, r, ql, qr);
}
int main() {
int n;
scanf("%d", &n);
int cnt = 0;
for (int i = 1; i <= n; i++) {
scanf("%lld", &op[i].id);
if(op[i].id == 1) {
scanf("%lld%lld", &op[i].x, &op[i].y);
t[++cnt] = op[i].x;
} else if(op[i].id == 2) {
scanf("%lld", &op[i].x);
t[++cnt] = op[i].x;
} else {
scanf("%lld%lld%lld", &op[i].x, &op[i].y, &op[i].z);
t[++cnt] = op[i].x;
t[++cnt] = op[i].y;
}
}
sort(t + 1, t + 1 + cnt);
m = unique(t + 1, t + 1 + cnt) - (t + 1);
for (int i = 1; i <= m; i++) {
mp[t[i]] = i;
}
t[m + 1] = 1e9 + 1;
t[0] = 0;
build(1, 1, m);
s.insert(m + 1);
s.insert(0);
for (int i = 1; i <= n; i++) {
if(op[i].id == 1) {
s.insert(mp[op[i].x]);
it = s.lower_bound(mp[op[i].x]);
it++;
update(1, 1, m, mp[op[i].x], (*it) - 1, op[i].y);
add[mp[op[i].x]] = op[i].y;
} else if(op[i].id == 2) {
it = s.lower_bound(mp[op[i].x]);
it1 = it, it2 = it;
it1--, it2++;
int num;
if((*it1) == 0) {
num = 1;
} else {
num = (*it1);
}
update(1, 1, m, num, (*it2) - 1, add[num]);
add[*it] = 0;
s.erase(it);
} else {
ans = -1;
v = op[i].z;
if(v == 0) {
printf("%d\n", op[i].x);
continue;
}
it = s.lower_bound(mp[op[i].x]);
query(1, 1, m, *it, mp[op[i].y] - 1);
printf("%.10Lf\n", ans);
}
}
}
Codeforces 1109C 线段树的更多相关文章
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...
- Codeforces 938G 线段树分治 线性基 可撤销并查集
Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...
- codeforces 1136E 线段树
codeforces 1136E: 题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了, ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- D - The Bakery CodeForces - 834D 线段树优化dp···
D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...
- B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路
B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...
- CodeForces 343D 线段树维护dfs序
给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...
- Linear Kingdom Races CodeForces - 115E (线段树优化dp)
大意: n条赛道, 初始全坏, 修复第$i$条花费$a_i$, m场比赛, 第$i$场比赛需要占用$[l_i,r_i]$的所有赛道, 收益为$w_i$, 求一个比赛方案使得收益最大. 设$dp[i]$ ...
随机推荐
- 解放内存之搭建自己的 R Server
学校的课五门有四门需要跑R程序,有一些长长长的代码实在是占用了太多的内存,果断决定搭个R的服务器放着自己跑. 愉快的是,R studio server 的搭建真心简单快捷~这个从前被我忽略的东东终于排 ...
- IQ信号理解
可参考http://wenku.baidu.com/link?url=Y3plyK9lgl96QowljJkWhgVaUGbH11j178DkId_vcl9z1V5cjl9ycTiB4Ym4iaypL ...
- fir分布式滤波的fpga实现
此设计的结构包括:1.移位寄存器链,n阶的有n-1个寄存器. 2.第一次累加部分.由fir滤波系数对称可得到对称的寄存器相加可以减小电路规模,所以第一次累加很有必要. 3,锁存并移位部分.此部分是为了 ...
- SPOJ1812 Longest Common Substring II
题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...
- bzoj 3295 (洛谷3157、3193) [Cqoi2011]动态逆序对——树套树 / CDQ分治
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3295 题目--洛谷3157:https://www.luogu.org/problemnew ...
- bzoj 1415 [Noi2005]聪聪和可可——其实无环的图上概率
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1415 乍一看和“游走”一样.于是高斯消元.n^2状态,复杂度n^6…… 看看TJ,发现因为聪 ...
- android栈和队列
android栈和队列 栈和队列是两种特殊的线性表,它们的逻辑结构和线性表相同,只是其运算规则较线性表有更多的限制,故又称它们为运算受限的线性表.LinkedList数据结构是一种双向的链式结构,每一 ...
- 【转】Jmeter安装成功后的目录介绍
1.bin目录 Jmeter.bat 打开Jmeter主界面 Jmeter使用的日志文件名称被定义到Jmeter.properties中,默认在Jmeter.log可查看日志 2.dosc和prin ...
- netty中的UDP
UDP 提供了向多个接收者发送消息的额外传输模式: 多播——传输到一个预定义的主机组: 广播——传输到网络(或者子网)上的所有主机. 本示例应用程序将通过发送能够被同一个网络中的所有主机所接收的消息来 ...
- Java-Runoob:Java 基本数据类型
ylbtech-Java-Runoob:Java 基本数据类型 1.返回顶部 1. Java 基本数据类型 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根 ...