题目传送门

前置知识

线段树

解法

第一眼感觉和 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 函数和普通单点修改一样。

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 题解的更多相关文章

  1. 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 ...

  2. 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局,其他队伍赢 ...

  3. 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 ...

  4. Money King【题解】

    我又傻了……竟然忘了区别大根堆和小根堆的性质,以至于一个符号打错,debug了半天……(我真是太菜了……) 题目描述 Once in a forest, there lived N aggressiv ...

  5. POJ 1904 King's Quest(强连通图)题解

    题意:n个王子有自己喜欢的ki个公主,有n个公主,每个王子只能娶一个自己喜欢的公主且不能绿别的王子.现在给你一种王子娶公主的方案,并且保证这种方案是正确的.请你给出,每个王子能娶哪些公主,要求娶这些公 ...

  6. POJ2728:Desert King——题解

    http://poj.org/problem?id=2728 题目大意:求一棵生成树使得路费用和/路长之和最小(路的费用是两端点的高度差) 最小比率生成树. 我们还是01分数规划的思想将边权变为路费用 ...

  7. 【luogu P1456 Monkey King】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1456 左偏树并查集不加路径压缩吧... #include <cstdio> #include & ...

  8. LuoguP7041 [NWRRC2016]King's Heir 题解

    Content 给出现在的日期,请从 \(n\) 个人当中选出一个人,使得他是所有成年人(\(\geqslant 18\) 岁的人)中年龄最小的. 数据范围:设日期为 \(yy/mm/dd\),则有 ...

  9. BZOJ-1087 互不侵犯King 状压DP+DFS预处理

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...

  10. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

随机推荐

  1. 类的MRO属性 C3算法

    C3算法 class A(object): pass class B(A): pass class C(A): pass class D(B): pass class E(C): pass class ...

  2. 04 Tcl字符串

    Tcl字符串 4.1 Tcl将说有的变量值视作字符串,并将他们作为字符串进行保存. 命令 描述 append 将值追加到字符串尾 binary 二进制化字符串 format 字符串格式化 regexp ...

  3. Android——共享参数SharedPreferences

    4数据存储 共享参数SharedPreferences.数据库SQLite.SD卡文件.App的全局内存 4.1共享参数SharedPreferences SharedPreferences是一个轻量 ...

  4. 达梦数据库varchar和nvarchar的验证

    达梦数据库varchar和nvarchar的验证 测试SQL create tablespace zhaobsh datafile '/opt/dmdbms/data/DAMENG/zhaobsh.d ...

  5. [转帖]NVIDIA超级AI服务器NVIDIA DGX GH200性能介绍

    https://zhuanlan.zhihu.com/p/633219396 2023 年 5 月 28 日NVIDIA宣布推出 NVIDIA DGX GH200,这是首款 100 TB级别的GPU ...

  6. Windows平台文件拆分与完整性检查的过程

    Windows平台文件拆分与完整性检查的过程 场景 有时候在没有linux主机的情况下, 自己下载下来的文件比较大. 比较难以上传到一些特殊的系统/主机上面. 这个时候需要将文件进行拆分. 所以可以通 ...

  7. [转帖]xtrabackup2.4备份恢复脚本

    https://developer.aliyun.com/article/534230#:~:text=xtrabackup2.4%E5%A4%87%E4%BB%BD%E6%81%A2%E5%A4%8 ...

  8. [转帖]详解:Linux Chrony 设置服务器集群同步时间

    https://www.linuxprobe.com/centos7-chrony-time.html 导读 Chrony是一个开源的自由软件,像CentOS 7或基于RHEL 7操作系统,已经是默认 ...

  9. vCenter6.7 无法启动

    Get service 567f6edd-d4f7-4bfb-905b-1834c758a99d_com.vmware.vsphere.clientDon't update service 567f6 ...

  10. 【转帖】JAVA GC日志分析

    https://zhuanlan.zhihu.com/p/613592552 ​ 目录 1. GC分类 针对HotSpot VM的实现,它里面的GC按照回收区域又分为两大种类型:一种是部分收集(Par ...