CF1089K King Kog's Reception 题解
前置知识
解法
第一眼感觉和 luogu P1083 [NOIP2012 提高组] 借教室 很像。本题同样采用线段树维护,\(sum_{l,r}(1 \le l \le r \le 10^6)\) 表示从 \(l \sim r\) 时刻内骑士拜访的总时间,\(maxx_{l,r}(1 \le l \le r \le 10^6)\) 表示从 \(l \sim r\) 时刻内骑士拜访完的最后时间。
build 函数和普通线段树一样。
update 函数和普通单点修改一样。
- 如果不会线段树单点修改,请左转 luogu P3374 【模板】树状数组 1。
pushup 函数是本题一个难点。考虑对于从 \(l \sim r(1 \le l \le r \le 10^6)\) 时刻,如果左区间 \(l \sim mid\) 时刻骑士拜访完的最后时间大于右区间 \(mid+1 \sim r\) 时刻的起始点,则右区间 \(mid+1 \sim r\) 时刻内所有骑士拜访均要向后推迟,即 tree[rt].maxx=max(tree[lson(rt)].maxx+tree[rson(rt)].sum,tree[rson(rt)].maxx);。
query 函数同理,查询时需要额外记录左区间 \(l \sim mid\) 时刻骑士拜访完的最后时间,对于最终时间时需要取 \(\max\),即 max(tree[rt].maxx,tree[rt].sum+maxxx);。最终答案即为最终时间减去起始拜访时间。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long//本题需要开 long long
#define sort stable_sort
#define endl '\n'
ll t[400000],d[400000],ans=0;
struct SegmentTree
{
ll l,r,sum,maxx;
}tree[5000000];
ll lson(ll x)
{
return x*2;
}
ll rson(ll x)
{
return x*2+1;
}
void pushup(ll rt)
{
tree[rt].sum=tree[lson(rt)].sum+tree[rson(rt)].sum;
tree[rt].maxx=max(tree[lson(rt)].maxx+tree[rson(rt)].sum,tree[rson(rt)].maxx);
}
void build(ll rt,ll l,ll r)
{
tree[rt].l=l;
tree[rt].r=r;
if(l==r)
{
tree[rt].maxx=tree[rt].sum=0;
return;
}
ll mid=(l+r)/2;
build(lson(rt),l,mid);
build(rson(rt),mid+1,r);
pushup(rt);
}
void update(ll rt,ll pos,ll val)
{
if(tree[rt].l==tree[rt].r)
{
tree[rt].sum=val;
tree[rt].maxx=tree[rt].l+val;
return;
}
else
{
ll mid=(tree[rt].l+tree[rt].r)/2;
if(pos<=mid)
{
update(lson(rt),pos,val);
}
else
{
update(rson(rt),pos,val);
}
}
pushup(rt);
}
ll query(ll rt,ll l,ll r,ll maxxx)
{
if(l<=tree[rt].l&&tree[rt].r<=r)
{
return max(tree[rt].maxx,tree[rt].sum+maxxx);
}
else
{
ll mid=(tree[rt].l+tree[rt].r)/2;
if(l<=mid)
{
ans=max(ans,query(lson(rt),l,r,maxxx));
}
if(mid<r)
{
ans=max(ans,query(rson(rt),l,r,ans));
}
return ans;
}
}
int main()
{
ll q,n,i,val;
char pd;
cin>>q;
build(1,1,1000000);
for(i=1;i<=q;i++)
{
cin>>pd;
if(pd=='+')
{
cin>>t[i]>>d[i];
update(1,t[i],d[i]);//因为第i位骑士访谈的时间是t[i]到t[i]+d[i]
}
if(pd=='-')
{
cin>>val;
update(1,t[val],0);
}
if(pd=='?')
{
cin>>val;
ans=0;
cout<<max(0ll,query(1,1,val,ans)-val)<<endl;
}
}
return 0;
}
后记
双倍经验:P9801 | CF1089K
CF1089K King Kog's Reception 题解的更多相关文章
- Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]
题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...
- 2018-2019 ICPC, NEERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred) Solution
A. Alice the Fan Solved. 题意: 两个人打网球,要求teamA 的得分与其他队伍拉开尽量大 输出合法的方案 思路: $dp[i][j][k][l] 表示 A 赢i局,其他队伍赢 ...
- Codeforces Round #379 (Div. 2) A B C D 水 二分 模拟
A. Anton and Danik time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Money King【题解】
我又傻了……竟然忘了区别大根堆和小根堆的性质,以至于一个符号打错,debug了半天……(我真是太菜了……) 题目描述 Once in a forest, there lived N aggressiv ...
- POJ 1904 King's Quest(强连通图)题解
题意:n个王子有自己喜欢的ki个公主,有n个公主,每个王子只能娶一个自己喜欢的公主且不能绿别的王子.现在给你一种王子娶公主的方案,并且保证这种方案是正确的.请你给出,每个王子能娶哪些公主,要求娶这些公 ...
- POJ2728:Desert King——题解
http://poj.org/problem?id=2728 题目大意:求一棵生成树使得路费用和/路长之和最小(路的费用是两端点的高度差) 最小比率生成树. 我们还是01分数规划的思想将边权变为路费用 ...
- 【luogu P1456 Monkey King】 题解
题目链接:https://www.luogu.org/problemnew/show/P1456 左偏树并查集不加路径压缩吧... #include <cstdio> #include & ...
- LuoguP7041 [NWRRC2016]King's Heir 题解
Content 给出现在的日期,请从 \(n\) 个人当中选出一个人,使得他是所有成年人(\(\geqslant 18\) 岁的人)中年龄最小的. 数据范围:设日期为 \(yy/mm/dd\),则有 ...
- BZOJ-1087 互不侵犯King 状压DP+DFS预处理
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
随机推荐
- Kubernetes security context capability
注:以下内容基于经验主义,不一定对. Linux capability Linux 中,root 作为特权用户,具有执行所有应用的能力.而普通用户只能执行普通应用.如果普通用户需要执行特权应用,需要进 ...
- 基于java+springboot的外卖点餐网站、外卖点餐管理系统
该系统是基于java+springboot开发的外卖点餐网站.外卖点餐管理系统.是给师弟开发的课程作业.运行过程中的问题,可以在github咨询作者. 演示地址 前台地址: http://food.g ...
- spring启动流程 (4) FactoryBean详解
FactoryBean接口 实现类对象将被用作创建Bean实例的工厂,即调用getObject()方法返回的对象才是真正要使用的Bean实例,而不是直接将FactoryBean对象作为暴露的Bean实 ...
- 神经网络优化篇:详解局部最优的问题(The problem of local optima)
局部最优的问题 在深度学习研究早期,人们总是担心优化算法会困在极差的局部最优,不过随着深度学习理论不断发展,对局部最优的理解也发生了改变.向展示一下现在怎么看待局部最优以及深度学习中的优化问题. 这是 ...
- Go-性能测试-benchmark
- bcc的简单学习
bcc的简单学习 安装 # 安装部分依赖项目 yum install cmake llvm -y dnf install -y bison cmake ethtool flex git iperf3 ...
- [转帖]MySQL的版本情况
Introducing MySQL Innovation and Long-Term Support (LTS) versions (oracle.com) Introducing MySQL Inn ...
- [转帖]jmeter_采样器sampler简介
1.取样器介绍 取样器是用来模拟用户操作的,向服务器发送请求以及接收服务器的响应数据. 取样器是在线程组内部的元件,也就是说取样器只能在线程组中添加. 取样器(Sampler)是性能测试中向服务器发送 ...
- [转帖]Linux系统下cpio命令详解
简介 cpio主要是解压或者将文件压缩到指定文件中即copy-in和copy-out模式. 参数说明 参数 参数说明 -i copy-in模式,解压文件 -o copy-out模式,即压缩文件 -d ...
- [转帖] Linux命令拾遗-剖析工具
https://www.cnblogs.com/codelogs/p/16060472.html 简介# 这是Linux命令拾遗系列的第五篇,本篇主要介绍Linux中常用的线程与内存剖析工具,以及更高 ...