题目传送门

前置知识

线段树

解法

第一眼感觉和 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. zznu 1632

    表达式求值 递归求解~~ #include<cstdio> #include<cstring> #include<cstdlib> #include<cmat ...

  2. 数字IC设计全流程介绍

    数字IC设计全流程设计 掌握数字集成电路设计的流程 数字设计流程中每个阶段主要做哪些工作? 数字设计流程中每个阶段使用的主要EDA工具? 数字电路常用软件公司Mentor(questasim),Syn ...

  3. Linux-文件压缩-tar-gzip

  4. [转帖]crash工具分析Kdump下vmcore文件常用命令总结(三)(实例易懂)

    一.简介 本文主要介绍使用crash工具对kdump生成的vmcore文件进行分析,解析常见的crash命令,前面已讲述两章关于Kdump的内容,读者感兴趣可以点击下面的链接: 1.Kdump调试机理 ...

  5. [转帖]AMD第四代宵龙 9174F 亮眼

    https://www.amd.com/zh-hans/processors/epyc-9004-series#%E8%A7%84%E6%A0%BC 型号规格   型号 CPU 核心数量 线程数量 最 ...

  6. [转帖]漏洞预警|Apache Tomcat 信息泄露漏洞

    http://www.hackdig.com/03/hack-953615.htm 棱镜七彩安全预警 近日网上有关于开源项目Apache Tomcat 信息泄露漏洞,棱镜七彩威胁情报团队第一时间探测到 ...

  7. [转帖]Ceph优化系列(四):RocksDB 使用 ARM 64 位 CRC32C 硬件优化指令

    一.前言 CRC32(A cyclic redundancy check 32)应用于校验,为了保证数据的正确性,采用的一种检错手段. CRC32C (CRC32 Castagnoli)  与 CRC ...

  8. 【K哥爬虫普法】网盘用的好,“艳照门”跑不了

    我国目前并未出台专门针对网络爬虫技术的法律规范,但在司法实践中,相关判决已屡见不鲜,K哥特设了"K哥爬虫普法"专栏,本栏目通过对真实案例的分析,旨在提高广大爬虫工程师的法律意识,知 ...

  9. 如何通过gRPC传输文件

    在gRPC中,可以通过将文件分割成多个小块,然后使用流式RPC将这些小块发送到服务器来传输文件.以下是一个简单的示例,展示了如何在gRPC中实现文件传输. 首先,我们需要定义一个服务来处理文件传输.在 ...

  10. Gin CORS 跨域请求资源共享与中间件

    Gin CORS 跨域请求资源共享与中间件 目录 Gin CORS 跨域请求资源共享与中间件 一.同源策略 1.1 什么是浏览器的同源策略? 1.2 同源策略判依据 1.3 跨域问题三种解决方案 二. ...