首页 > 其他分享 >D. Andrey and Escape from Capygrad

D. Andrey and Escape from Capygrad

时间:2023-08-13 19:44:18浏览次数:40  
标签:teleport le 14 point Andrey Escape segment 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 $0$. There are $n$ portals all over Tyagoland, each of which is characterised by four integers $l_i$, $r_i$, $a_i$ and $b_i$ ($1 \le l_i \le a_i \le b_i \le r_i \le 10^9$). Note that the segment $[a_i, b_i]$ is contained in the segment $[l_i, r_i]$.

If Andrey is on the segment $[l_i, r_i]$, then the portal can teleport him to any point on the segment $[a_i, b_i]$. Andrey has a pass that allows him to use the portals an unlimited number of times.

Andrey thinks that the point $x$ is on the segment $[l, r]$ if the inequality $l \le x \le r$ is satisfied.

Andrey has $q$ options for where to start his escape, each option is characterized by a single integer $x_i$ — 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 $q$ positions.

Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of sets of test cases. The description of test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of portals.

Each of the next $n$ lines contains four integers $l_i$, $r_i$, $a_i$, and $b_i$ $(1 \le l_i \le a_i \le b_i \le r_i \le 10^9)$ — the characteristics of the portals.

The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of positions.

The following line contains $q$ integers $x_1, x_2, \ldots, x_q$ ($1 \le x_i \le 10^9$) — the position from which Andrey will start his escape in the $i$-th options.

It is guaranteed that the sum of $n$ and the sum of $q$ over all test cases does not exceed $2 \cdot 10^5$.

Output

For each test case, output a single line of $q$ 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:

  1. Use portal $1$ and teleport to point $b_1 = 14$.
  2. Use portal $2$ first and teleport to point $6$, which is on the segment $[l_1, r_1] = [6, 17]$, then use portal $1$ and teleport to point $b_1 = 14$.
  3. Stay at point $x_3=23$ without using any portals.
  4. Stay at point $x_4=15$ without using any portals.
  5. Point $x_5=28$ is not on any segment, so Andrey won't be able to teleport anywhere.
  6. Point $x_6=18$ is only on the segment $[l_3, r_3] = [16, 24]$, use portal $3$ and teleport to point $b_3 = 22$.

In the fifth test case:

Optimal actions when starting from each position:

  1. Use portal $1$ first and teleport to point $15$ on the segment $[a_1, b_1] = [14, 20]$, then use portal $2$ and teleport to point $21$, which is on the segment $[l_4, r_4] = [21, 33]$ and on the segment $[a_2, b_2] = [13, 24]$, then teleport to point $b_4 = 32$.
  2. Use portal $6$ first and teleport to point $20$ on the segment $[a_6, b_6] = [20, 21]$, then use portal $2$ and teleport to point $22$, which is simultaneously on the segment $[l_4, r_4] = [21, 33]$ and on the segment $[a_2, b_2] = [13, 24]$, then teleport to point $b_4 = 32$.
  3. Perform the same actions as from the first position.
  4. Stay at point $x_4=5$ without using any portals.
  5. Point $8$ is not on any segment, so Andrey won't be able to teleport anywhere.
  6. Stay at point $x_6=33$ without using any portals.
  7. Use portal $5$ and teleport to point $b_5 = 4$.
  8. Perform the same actions as from the first position.

 

解题思路

  对于一段区间$(l_i, r_i, a_i, b_i)$,实际上我们只关心其$[l_i, b_i]$这一部分。我们把原本的区间分成两段,分别是$[l_i, b_i]$和$[b_i+1, r_i]$,当下标$x \in [l_i, b_i]$,那么$x$就可以通过该区间传送到$b_i$处,$x$不会变小;当下标$x \in [b_i+1, r_i]$,如果要通过该区间传送,那么$x$最大只能传送到$b_i$,$x$只会变小。因此对于每个区间的$[b_i+1, r_i]$这部分可以直接舍弃,因为如果$x$位于这个区间范围内,若要传送则$x$只会变小,不如保持原来的不变。

  同时如果$x \in [l_i, b_i]$,那么$x$一定可以变成$b_i$,又因为可以无限传送,因此如果区间存在交集那么继续往最右边传送。所以可以进行区间合并,那么就会得到若干个互不相交的区间(否则就可以合并成一个),然后对合并后的区间按左端点排序,对于每一个询问$x$可以二分出左端点不超过$x$的最大区间,那么$x$最终能传送到最远的地方就是$x$本身与这个区间右端点中最大的一个。

  AC代码如下,时间复杂度为$O(n \log{n})$:

 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

标签:teleport,le,14,point,Andrey,Escape,segment,Capygrad
From: https://www.cnblogs.com/onlyblues/p/17627102.html

相关文章

  • C# HttpUtility.UrlEncode与 Uri.EscapeDataString区别
    相同点均是对url进行编码区别HttpUtility.UrlEncode会将空格转换为加号(+)Uri.EscapeDataString会将空格转换为%20适用场景HttpUtility.UrlEncode适用于url是查询参数Uri.EscapeDataString适用于url是作为文件路径使用......
  • 3-2 编写一个函数 escape(s, t),将字符串 t 复制到字符串 s 中,并在复制 过程中将换行
    ArchlinuxGCC13.1.1 202304292023-07-3012:57:46星期日 点击查看代码#include<stdio.h>voidescape(chars[],chart[]){inti,j;i=j=0;while(t[i]!='\0'){switch(t[i]){case�......
  • 题解:【AT icpc2015summer day2-G】 Escape
    题目链接目前AT的最优解。树的话就是根叶链的最大点权和路径,DP随便搞。考虑扩展到图上,反复删除掉所有度数为\(1\)的节点,显然剩下的东西是可以全部取完的,因为它的形态类似于菊花套环,且末端必定为环。将这部分缩起来再跑上面的DP就好了。事实上两部分可以同时进行,一个bfs......
  • 【代码片段分享】比 url.QueryEscape 快 7.33 倍的 FastQueryEscape
    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!cnblogs博客zhihuGithub公众号:一本正经的瞎扯做profile发现url.QueryEscape占用的CPU时间较多,于是搜索到了一个资料:net/url:optimizeunescapeandescape.于是在这个代码的基础上改了FastQueryString的版......
  • Kubescape入门
    Kubescape是一个K8sopen-source工具,提供multi-cloudK8s单层玻璃,包括风险分析、安全合规性、RBAC可视化工具和图像漏洞扫描。Kubescape在CI/CD管道的早期阶段扫描K8s集群、YAML文件和HELM图表,根据多个框架(例如NSA-CISA、MITREATT&CK®)、软件漏洞和RBAC(role-based-access-control)......
  • Jmeter函数助手41-unescapeHtml
    unescapeHtml函数用于将HTML转义过的字符串反转义为Unicode字符串。Stringtounescape:填入字符 1、escapeHtml函数是将字符进行HTML转义,unescapeHtml函数函数则是将HTML转义过的字符反转,unescapeHtml函数和escapeHtml函数功能刚好相反。${__unescapeHtml(value="hello"+"w......
  • HDU 5389 Zero Escape(DP + 滚动数组)
    ZeroEscapeTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:131072/131072K(Java/Others)TotalSubmission(s):864    AcceptedSubmission(s):438ProblemDescriptionZeroEscape,isavisualnoveladventurevideogamedirectedbyKotar......
  • lshell escape
    lshell(LimitedShell)escape lshell是表示当前用户的shell是受限的,只能执行几个指定的指令可参考Lshell-aldeid 先确定自己是否被受限user:~$helpcdclear......
  • escape 和 encodeURI 和 encodeURIComponent 区别?
    在日常开发中,我们经常会用到  escape和encodeURI和encodeURIComponent  这三个方法对url或某些字符串进行转义,那这三个方法有什么区别呢?escape官方文档:https:......
  • JSON_UNESCAPED_SLASHES json 不转义反斜杠
    JSON_UNESCAPED_SLASHESJSON_UNESCAPED_UNICODE的意思不要转移汉字,我们在学习使用的时候经常使用这个选项。而JSON_UNESCAPED_SLASHES是用于不要转义反斜杠,在这里选择这......