题目链接:https://codeforces.com/contest/1089/problem/K

time limit per test: 2 seconds  memory limit per test: 512 megabytes

King Kog got annoyed of the usual laxity of his knights — they can break into his hall without prior notice! Thus, the King decided to build a reception with a queue where each knight chooses in advance the time when he will come and how long the visit will take. The knights are served in the order of the recorded time, but each knight has to wait until the visits of all the knights before him are finished.

Princess Keabeanie wants to see her father. However, she does not want to interrupt the knights so she joins the queue. Unfortunately, the knights change their minds very often — they can join the queue or cancel their visits. Please help the princess to understand how long she will have to wait until she sees her father if she enters the queue at the specified moments of time given the records at the reception.

Input
The first line of the input contains a single integer $q (1 \le q \le 3 \times 10^5)$ — the number of events. An event can be of three types: join, cancel, or query.

Join "+ $t$ $d$" $(1 \le t,d \le 10^6)$ — a new knight joins the queue, where $t$ is the time when the knight will come and $d$ is the duration of the visit.
Cancel "- $i$" $(1 \le i \le q)$ — the knight cancels the visit, where $i$ is the number (counted starting from one) of the corresponding join event in the list of all events.
Query "? $t$" $(1 \le t \le 10^6)$ — Keabeanie asks how long she will wait if she comes at the time $t$.
It is guaranteed that after each event there are no two knights with the same entrance time in the queue. Cancel events refer to the previous joins that were not cancelled yet.

Keabeanie can come at the same time as some knight, but Keabeanie is very polite and she will wait for the knight to pass.

Output
For each query write a separate line with the amount of time Keabeanie will have to wait.

Example
input
19
? 3
+ 2 2
? 3
? 4
+ 5 2
? 5
? 6
+ 1 2
? 2
? 3
? 4
? 5
? 6
? 7
? 9
- 8
? 2
? 3
? 6
output
0
1
0
2
1
3
2
1
2
1
0
0
2
1
1

题意:

国王构建了一个队列,骑士如果要来见国王,都要通过这个队列排队觐见国王。

给出三种操作,第一种代表骑士在 $t$ 时刻前来排队,它要跟国王商谈 $d$ 分钟(保证没有两个骑士同时到来)。

第二种代表第 $i$ 次操作,其所代表的那个来排队的骑士取消了这次觐见。

第三种代表公主在 $t$ 时刻也前来排队,询问她需要等多久才能就到父王(如果她和某个骑士同时到达,则她会礼让骑士)。

题解:

假设在某个时间区间 $[l,r]$ 内所有前来觐见的骑士,他们的 $d$ 之和为 $sum[l,r]$。

那么对于一次查询操作 $t$,必然有某个时刻 $i$ 来的这个骑士,其对应的 $i + sum[i][t]$ 正好就是公主要等到的那个时刻。

也就是说,只要求 $\max_{1 \le i \le t}(sum[i][t]+i) - t$ 即可。

因此,我们可以用线段树来进行维护,线段树节点有两个值 $sum$ 和 $mx$:$sum[l,r]$ 的意义如上所述;而 $mx[l,r]$ 则代表至少要到 $mx[l,r]$ 时刻才能见完 $[l,r]$ 区间内的所有骑士。

这两个值的维护方式如下,特别是 $mx$ 的维护是值得注意的:

node[rt].sum=node[ls].sum+node[rs].sum;
node[rt].mx=max(node[rs].mx,node[ls].mx+node[rs].sum);

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxq=3e5+;
const int maxt=1e6+;
int q;
pii et[maxq]; /********************************* Segment Tree - st *********************************/
#define ls (rt<<1)
#define rs (rt<<1|1)
struct Node{
int l,r;
ll sum,mx;
}node[*maxt];
void pushup(int rt)
{
node[rt].sum=node[ls].sum+node[rs].sum;
node[rt].mx=max(node[rs].mx,node[ls].mx+node[rs].sum);
}
void build(int rt,int l,int r)
{
node[rt].l=l; node[rt].r=r;
if(l==r)
{
node[rt].sum=, node[rt].mx=l;
return;
}
int mid=(l+r)/;
build(ls,l,mid);
build(rs,mid+,r);
pushup(rt);
}
void update(int rt,int pos,int val)
{
if(node[rt].l==node[rt].r)
{
node[rt].sum+=val;
node[rt].mx+=val;
return;
}
int mid=(node[rt].l+node[rt].r)/;
if(pos<=mid) update(ls,pos,val);
if(pos>mid) update(rs,pos,val);
pushup(rt);
}
ll ans;
void query(int rt,int t)
{
if(node[rt].r<=t)
{
ans=max(node[rt].mx,ans+node[rt].sum);
return;
}
int mid=(node[rt].l+node[rt].r)/;
query(ls,t);
if(mid<t) query(rs,t);
}
/********************************* Segment Tree - ed *********************************/ int main()
{
cin>>q;
build(,,);
for(int i=;i<=q;i++)
{
char op[]; scanf("%s",op);
if(op[]=='+')
{
int t,d; scanf("%d%d",&t,&d);
update(,t,d);
et[i]=make_pair(t,d);
}
if(op[]=='-')
{
int id; scanf("%d",&id);
update(,et[id].first,-et[id].second);
}
if(op[]=='?')
{
int t; scanf("%d",&t);
ans=; query(,t);
printf("%I64d\n",max(ans-t,0ll));
}
}
}

Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]的更多相关文章

  1. Codeforces 1089E - Easy Chess - [DFS+特判][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem E]

    题目链接:https://codeforces.com/contest/1089/problem/E Elma is learning chess figures. She learned that ...

  2. 记第一场atcoder和codeforces 2018-2019 ICPC, NEERC, Northern Eurasia Finals Online Mirror

    下午连着两场比赛,爽. 首先是codeforses,我和一位dalao一起打的,结果考炸了,幸亏不计rating.. A Alice the Fan 这个就是记忆化搜索一下预处理,然后直接回答询问好了 ...

  3. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  4. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  5. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  6. Codeforces 444 C. DZY Loves Colors (线段树+剪枝)

    题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...

  7. Codeforces Gym 100513F F. Ilya Muromets 线段树

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

  8. Codeforces 834D The Bakery【dp+线段树维护+lazy】

    D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...

  9. codeforces 1017C - Cloud Computing 权值线段树 差分 贪心

    https://codeforces.com/problemset/problem/1070/C 题意: 有很多活动,每个活动可以在天数为$[l,r]$时,提供$C$个价格为$P$的商品 现在从第一天 ...

随机推荐

  1. VirtualBox虚拟机磁盘瘦身

    操作系统 : windows7_x64 VirtualBox 版本 : 4.3.28 原理: 使用0填充虚拟系统磁盘,然后删除填充文件,再使用VBoxManage进行压缩. Linux系统磁盘瘦身 一 ...

  2. PL/SQL学习笔记之事务

    一:事务自动提交的开启与关闭 1)开启事务自动提交:则每一个INSERT,UPDATE或DELETE命令执行时,都提交一次事务. SET AUTOCOMMIT ON; 2)关闭事务自动提交:则执行到C ...

  3. TensorFlow+Keras 02 深度学习的原理

    1 神经传递的原理 人类的神经元传递及其作用: 这里有几个关键概念: 树突 - 接受信息 轴突 - 输出信息 突触 - 传递信息 将其延伸到神经元中,示意图如下: 将上图整理成数学公式,则有 y = ...

  4. ionic3 表单输入元素input的三种事件

    1.onblur事件 onblur事件即为失去焦点时触发的事件,而所谓的焦点就是一闪一闪的光标.而ionic中onblur则为ionBlur,具体用法如下: //html<ion-input [ ...

  5. .NET内存泄漏(之 静态事件)

    一.事件引起的内存泄露 1.不手动注销事件也不发生内存泄露的情况 我们经常会写EventHandler += AFunction; 如果没有手动注销这个Event handler类似:EventHan ...

  6. Spark RDD、DataFrame原理及操作详解

    RDD是什么? RDD (resilientdistributed dataset),指的是一个只读的,可分区的分布式数据集,这个数据集的全部或部分可以缓存在内存中,在多次计算间重用. RDD内部可以 ...

  7. HDFS: The short-circuit local reads feature cannot be used

    问题: method:org.apache.hadoop.hdfs.DomainSocketFactory.<init>(DomainSocketFactory.java:69) The ...

  8. Linux 用户管理_015

    一.用户基础了解 Linux是一个多任务.多用户的操作系统,每个用户和进程都需要对应一个用户和用户组,用户名相当于姓名, 用户UID相当于身份证号,用户组GID相当于公司的工号.用户与用户组的关系一对 ...

  9. 我们正在招聘java工程师,想来美团工作吗?

    我们希望你有? 1.3年以上Java服务器开发经验,精通Java及面向对象设计开发,熟悉主流web框架 2.熟悉网络编程,熟悉TCP/IP协议,熟悉互联网应用协议 3.有大规模分布式系统设计与开发经验 ...

  10. 【QT】QPixmap在Label中自适应大小铺满

    KeepAspectRatio:设置pixmap缩放的尺寸保持宽高比. setScaledContents:设置label的属性scaledContents,这个属性的作用是允许(禁止)label缩放 ...