D. Andrey and Escape from Capygrad
D. Andrey and Escape from Capygrad
An incident occurred in Capygrad, the capital of Tyagoland, where all the capybaras in the city went crazy and started throwing mandarins. Andrey was forced to escape from the city as far as possible, using portals.
Tyagoland is represented by a number line, and the city of Capygrad is located at point . There are portals all over Tyagoland, each of which is characterised by four integers , , and (). Note that the segment is contained in the segment .
If Andrey is on the segment , then the portal can teleport him to any point on the segment . Andrey has a pass that allows him to use the portals an unlimited number of times.
Andrey thinks that the point is on the segment if the inequality is satisfied.
Andrey has options for where to start his escape, each option is characterized by a single integer — the starting position of the escape. He wants to escape from Capygrad as far as possible (to the point with the maximum possible coordinate). Help Andrey determine how far he could escape from Capygrad, starting at each of the positions.
Input
Each test consists of multiple test cases. The first line contains a single integer () — the number of sets of test cases. The description of test cases follows.
The first line of each test case contains a single integer () — the number of portals.
Each of the next lines contains four integers , , , and — the characteristics of the portals.
The next line contains a single integer () — the number of positions.
The following line contains integers () — the position from which Andrey will start his escape in the -th options.
It is guaranteed that the sum of and the sum of over all test cases does not exceed .
Output
For each test case, output a single line of integers, containing the answers to Andrey's questions.
Example
input
5 3 6 17 7 14 1 12 3 8 16 24 20 22 6 10 2 23 15 28 18 3 3 14 7 10 16 24 20 22 1 16 3 14 9 2 4 6 8 18 23 11 13 15 2 1 4 2 3 3 9 6 7 3 4 8 1 5 18 24 18 24 1 8 2 4 11 16 14 14 26 32 28 30 5 10 6 8 9 15 14 13 27 22 17 31 1 7 6 9 22 14 20 11 26 13 24 21 33 22 23 21 33 25 32 1 6 3 4 18 29 20 21 8 11 23 16 5 8 33 2 21
output
14 14 23 15 28 22 14 14 14 14 22 23 14 14 15 7 8 7 15 14 14 30 24 17 31 4 8 32 32 32 5 8 33 4 32
Note
In the first test case:
Optimal actions when starting from each position:
- Use portal and teleport to point .
- Use portal first and teleport to point , which is on the segment , then use portal and teleport to point .
- Stay at point without using any portals.
- Stay at point without using any portals.
- Point is not on any segment, so Andrey won't be able to teleport anywhere.
- Point is only on the segment , use portal and teleport to point .
In the fifth test case:
Optimal actions when starting from each position:
- Use portal first and teleport to point on the segment , then use portal and teleport to point , which is on the segment and on the segment , then teleport to point .
- Use portal first and teleport to point on the segment , then use portal and teleport to point , which is simultaneously on the segment and on the segment , then teleport to point .
- Perform the same actions as from the first position.
- Stay at point without using any portals.
- Point is not on any segment, so Andrey won't be able to teleport anywhere.
- Stay at point without using any portals.
- Use portal and teleport to point .
- Perform the same actions as from the first position.
解题思路
对于一段区间,实际上我们只关心其这一部分。我们把原本的区间分成两段,分别是和,当下标,那么就可以通过该区间传送到处,不会变小;当下标,如果要通过该区间传送,那么最大只能传送到,只会变小。因此对于每个区间的这部分可以直接舍弃,因为如果位于这个区间范围内,若要传送则只会变小,不如保持原来的不变。
同时如果,那么一定可以变成,又因为可以无限传送,因此如果区间存在交集那么继续往最右边传送。所以可以进行区间合并,那么就会得到若干个互不相交的区间(否则就可以合并成一个),然后对合并后的区间按左端点排序,对于每一个询问可以二分出左端点不超过的最大区间,那么最终能传送到最远的地方就是本身与这个区间右端点中最大的一个。
AC代码如下,时间复杂度为:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 typedef pair<int, int> PII; 6 7 const int N = 2e5 + 10; 8 9 PII p[N]; 10 11 void solve() { 12 int n, m; 13 scanf("%d", &n); 14 for (int i = 0; i < n; i++) { 15 int l, r; 16 scanf("%d %*d %*d %d", &l, &r); // 对于区间(l,r,a,b),只关心l和b 17 p[i] = {l, r}; 18 } 19 // 对区间按左端点排序,然后进行区间合并 20 sort(p, p + n); 21 vector<PII> q; 22 int l = -1, r = -1; 23 for (int i = 0; i < n; i++) { 24 if (p[i].first > r) { 25 if (l != -1) q.push_back({l, r}); 26 l = p[i].first, r = p[i].second; 27 } 28 else { 29 r = max(r, p[i].second); 30 } 31 } 32 q.push_back({l, r}); 33 scanf("%d", &m); 34 while (m--) { 35 int x; 36 scanf("%d", &x); 37 int l = 0, r = q.size() - 1; 38 while (l < r) { 39 int mid = l + r + 1 >> 1; 40 if (q[mid].first <= x) l = mid; 41 else r = mid - 1; 42 } 43 if (q[l].first <= x && q[l].second >= x) printf("%d ", q[l].second); // 存在左端点不超过x的区间,且区间右端点超过x 44 else printf("%d ", x); 45 } 46 printf("\n"); 47 } 48 49 int main() { 50 int t; 51 scanf("%d", &t); 52 while (t--) { 53 solve(); 54 } 55 56 return 0; 57 }
参考资料
Codeforces Round 892 (Div. 2)A-D:https://zhuanlan.zhihu.com/p/649683357
Codeforces Round #892 (Div. 2) Editorial:https://codeforces.com/blog/entry/119287
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17627102.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效