版本比较
题解:CF160E Buses and People
正文
增加 6 行删除 6 行
2 行未改动3456778910119 行未改动212223242525262728282930313289 行未改动1221231241251261261271281291305 行未改动13613713813914014014114214314314414514614779 行未改动2 行未改动有 $n$ 辆车,规定起点 $s$,终点 $f$ 和发车时间 $t$。还有 $m$ 个人,规定出发点 $l$,目的地 $r$ 和到出发点的时间 $b$。问你每个人能坐的时间最早的车的编号。
## 思路 #1
一个人能坐这辆车的条件是 $s\le l,r \le f$ 且 $b\le t$。 一个人能坐这辆车的条件是 $s\le l,r \le f$ 且 $b\le t$。三个条件不好维护,我们可以对于车按 $s$ 排序,人按 $l$ 排序,现在只需要满足 $r\le f$ 和 $b\le t$。不难想到用数据结构维护一个东西对应的另一个。
如果维护 $f$ 对应的 $t$ 的话,每次询问需要查询一段区间中大于等于一个数中最小的那个,不好维护。
9 行未改动#define tcout TRACE && cout#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);#define fir first#define sec secondconst int P = 998244353; const int P = 998244353;const int Base = 33331;#ifdef intconst int INF = 0x3f3f3f3f3f3f3f3f; const int INF = 0x3f3f3f3f3f3f3f3f;#elseconst int INF = 0x3f3f3f3f;#endifconst int N = 1e6 + 10, M = 2e6 + 10;89 行未改动```## 思路 #2在线段树上维护最大值的编号,我们要找满足条件的最靠左的位置,所以说我们遍历整棵线段树,如果发现往左走满足条件就往左走,否则往右走,很容易找到答案。因为时间复杂度 $O\left(n\log n\right)$。
为什么单次是 $O\left(\log n\right)$ 的呢? 为什么单次是 $O\left(\log n\right)$ 的呢?我们考虑你确定了答案在线段树的某一个区间内,答案不在左子树里就在右子树里,也就是说,不可能说你遍历到一个区间,左右子树都无解,所以说时间复杂度就是线段树的层高,就是 $O\left(\log n\right)$。
## 代码 #2``` cpp5 行未改动#define tcout TRACE && cout#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);#define fir first#define sec secondconst int P = 998244353; const int P = 998244353;const int Base = 33331;#ifdef intconst int INF = 0x3f3f3f3f3f3f3f3f; const int INF = 0x3f3f3f3f3f3f3f3f;#elseconst int INF = 0x3f3f3f3f;#endifconst int N = 1e6 + 10, M = 2e6 + 10;79 行未改动