Codeforces 1089K King Kog's Reception(线段树)
题目链接:https://codeforces.com/contest/1089/problem/K
题意:
有一些骑士要去见国王,公主也要见国王
给你q个询问,询问有三种操作,
1、+ t d 表示一个骑士在t分钟的时候去见国王,要见d分钟。
2、 - i 表示取消输入序列的第i个想要见国王的骑士
3、? t 表示如果公主在 t 时间见国王,要等多久
国王每次只能见一个人,其他人要在外面等着,如果公主和一个骑士同时到达,那么公主等着。
根据时间轴来建立线段树,每个节点维护,①这个区间内需要国王处理的总时间sum,②这个区间内的骑士至少到哪个时间点才能都见完国王。
这样对于公主在t时间点到来,必有一个骑士在 i 时间点,i + sum[i][t] 是公主到的时间,
只要求从1-t之间 最晚 处理完所有的骑士的时间T,T-t 即公主等待的时间,如果T <= t 则公主不需要等待
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e6 + 10;struct node{int d, t;
}pos[maxn];struct Tree{int l, r;ll sum, maxx;
}e[maxn << 2];void pushup(int cur){e[cur].sum = e[cur << 1].sum + e[cur << 1 | 1].sum;e[cur].maxx = max(e[cur << 1 | 1].maxx, e[cur << 1].maxx + e[cur << 1 | 1].sum);
}void build(int l, int r, int cur){e[cur].l = l;e[cur].r = r;e[cur].sum = 0;e[cur].maxx = 0;if(l == r) {e[cur].maxx = l;return ;}int mid = l + r >> 1;build(l, mid, cur << 1);build(mid + 1, r, cur << 1 | 1);pushup(cur);
}void update(int l, int r, int cur, int pos, int val){if(l == r){e[cur].maxx += val;e[cur].sum += val;return;}int mid = l + r >> 1;if(pos <= mid) update(l, mid, cur << 1, pos, val);else update(mid + 1, r, cur << 1 | 1, pos, val);pushup(cur);
}
ll ans;
void query(int l, int r, int cur, int pos){if(r <= pos){ans = max(e[cur].maxx, ans + e[cur].sum);return;}int mid = l + r >> 1;query(l, mid, cur << 1, pos);if(pos > mid) {query(mid + 1, r, cur << 1 | 1, pos);}
}int main()
{int q;scanf("%d", &q);build(1, 1e6, 1);for(int i = 1; i <= q; i++){char op[2];int d, t;scanf("%s", op);if(op[0] == '+'){scanf("%d %d", &t, &d);update(1, 1000000, 1, t, d);pos[i].t = t;pos[i].d = d;}else if(op[0] == '-') {scanf("%d", &d);update(1, 1000000, 1, pos[d].t, -pos[d].d);}else {scanf("%d", &t);ans = 0;query(1, 1000000, 1, t);printf("%lld\n", max(0ll, ans - t));}}return 0;
}
Codeforces 1089K King Kog's Reception(线段树)相关推荐
- 2018-2019 ICPC, NEERC, Northern Eurasia Finals K. King Kog's Reception 线段树
K. King Kog's Reception 题意:有q个操作,第 i 次操作若为 + t d,表示在 t 这个时刻插入一个耗时为 d 的骑士,若 - i 表示删除第 i 个操作,若 ? t 表示 ...
- Codeforces - King Kog‘s Reception
题目链接:Codeforces - King Kog's Reception 考虑权值线段树,对每个区间维护这个区间可以延迟到的最后的时间. 考虑区间合并: c.sum=a.sum+b.sum; c. ...
- CF1089K King Kog‘s Reception(权值线段树)
题目是个队列问题,涉及到进队,出队,查询三种操作 t d ,表示在时刻 t 某个骑士来访,需要和国王交谈 d 的时间.(进队) − i ,表示撤销第 i 次操作,第i次请求来访的骑士突然变卦,取消了访 ...
- Codeforces 1004F Sonya and Bitwise OR (线段树)
题目链接 https://codeforces.com/contest/1004/problem/F 题解 这种水题都不会做了怎么.. 考虑一个序列的前缀 \(\text{or}\) 值只会变化 \( ...
- CodeForces - 1529E Trees of Tranquillity(贪心+线段树)
题目链接:https://vjudge.net/problem/CodeForces-1529E 题目大意:给出两棵根节点为 111 的树,分别称为 AAA 树和 BBB 树,现在通过两棵树可以构造出 ...
- Codeforces 1045. A. Last chance(网络流 + 线段树优化建边)
题意 给你 \(n\) 个武器,\(m\) 个敌人,问你最多消灭多少个敌人,并输出方案. 总共有三种武器. SQL 火箭 - 能消灭给你集合中的一个敌人 \(\sum |S| \le 100000\) ...
- [codeforces] 383C Propagating tree(dfs序+线段树)
题意: 给你一棵n个结点的树, 以1为根.每个结点有点权.有m次操作: 1.x结点权值 +val,x的儿子权值 −val,x的孙子们 +val,以此类推. 2.询问x的点权: 题解: 我们首先跑一边d ...
- CodeForces - 1217F Forced Online Queries Problem(线段树分治+并查集撤销)
题目链接:点击查看 题目大意:给出 nnn 个点,初始时互相不存在连边,需要执行 mmm 次操作,每次操作分为两种类型: 1xy1 \ x \ y1 x y:如果 (x,y)(x,y)(x,y) 之间 ...
- CodeForces - 504B Misha and Permutations Summation(线段树模拟康托展开与逆展开)
题目链接:点击查看 题目大意:给出两个排列 ppp 和 qqq,现在要求输出 Perm((Ord(p)+Ord(q))modn!)Perm((Ord(p)+Ord(q)) \bmod n!) Perm ...
最新文章
- 信号量,互斥锁,条件变量的联系与区别
- MySql连接查询与联合查询
- ITK:过滤图像而没有复制其数据
- android环境搭建出错,androidstudio配置环境遇到的各种错误(持续更新中)
- r语言 运算符_R语言运算符
- leetcode - K 站中转内最便宜的航班
- sencha touch 入门学习资料大全
- go语言打印errors类型变量
- [转载] Python中产生随机数
- multipartfile上传文件_Feign文件上传和部分源码解读
- 电脑系统怎么重装?U盘安装Windows XP系统保姆级教程
- ENVI App Store 下载与安装
- Activiti6常见错误汇总
- libGDX-5:文本显示 BitmapFont 和 工具 hiero
- JAVA排课教务系统
- 金山打字专业文章计算机,打字测试-金山打字2003打字测试有哪些文章?金山打字2003打字测试有哪 爱问知识人...
- php使用加密狗,加密狗使用方法
- 手机管理应用研究【5】——应用杂篇
- android APP 跳转到应用商店评分
- canvas画圆环(一)之渐变色,纯色
热门文章
- 交换机的工作模式:IVL和SVL
- 【技术分享】ApolloEDU6.0+LGSVL联合仿真环境搭建
- 前端文档网站快速生成工具
- matlab中的方波信号图片_MATLAB| 望远镜分辨率amp;艾里斑的模拟
- QT学习教程-(1)QT新建项目并打包hellow world
- 04Reverse基础(五)
- 债券ETF认申赎_基金合同_嘉实基金
- JavaFx教程-01初识javaFX
- 跨平台APP----对Cordova,APPCan,DCloud,APICloud四大平台的分析(系列二)
- topojson转换与应用