首页 > 其他分享 >D. Robot Queries

D. Robot Queries

时间:2023-12-04 21:00:45浏览次数:27  
标签:le point robot 偏移量 Robot Queries YES first

D. Robot Queries

There is an infinite $2$-dimensional grid. Initially, a robot stands in the point $(0, 0)$. The robot can execute four commands:

  • U — move from point $(x, y)$ to $(x, y + 1)$;
  • D — move from point $(x, y)$ to $(x, y - 1)$;
  • L — move from point $(x, y)$ to $(x - 1, y)$;
  • R — move from point $(x, y)$ to $(x + 1, y)$.

You are given a sequence of commands $s$ of length $n$. Your task is to answer $q$ independent queries: given four integers $x$, $y$, $l$ and $r$; determine whether the robot visits the point $(x, y)$, while executing a sequence $s$, but the substring from $l$ to $r$ is reversed (i. e. the robot performs commands in order $s_1 s_2 s_3 \dots s_{l-1} s_r s_{r-1} s_{r-2} \dots s_l s_{r+1} s_{r+2} \dots s_n$).

Input

The first line contains two integers $n$ and $q$ ($1 \le n, q \le 2 \cdot 10^5$) — the length of the command sequence and the number of queries, respectively.

The second line contains a string $s$ of length $n$, consisting of characters U, D, L and/or R.

Then $q$ lines follow, the $i$-th of them contains four integers $x_i$, $y_i$, $l_i$ and $r_i$ ($-n \le x_i, y_i \le n$; $1 \le l \le r \le n$) describing the $i$-th query.

Output

For each query, print YES if the robot visits the point $(x, y)$, while executing a sequence $s$, but the substring from $l$ to $r$ is reversed; otherwise print NO.

Examples

input

8 3
RDLLUURU
-1 2 1 7
0 0 3 4
0 1 7 8

output

YES
YES
NO

input

4 2
RLDU
0 0 2 2
-1 -1 2 3

output

YES
NO

input

10 6
DLUDLRULLD
-1 0 1 10
-1 -2 2 5
-4 -2 6 10
-1 0 3 9
0 1 4 7
-3 -1 5 8

output

YES
YES
YES
NO
YES
YES

Note

In the first query of the first sample, the path of the robot looks as follows:


In the second query of the first sample, the path of the robot looks as follows:


In the third query of the first sample, the path of the robot looks as follows:

 

解题思路

  定义 U 关于 $x$ 和 $y$ 的偏移量为 $(0,1)$,同理 R 的偏移量为 $(1,0)$,D 的偏移量为 $(0,-1)$,L 的偏移量为 $(-1,0)$。

  定义 $f(i) = (x_i, y_i)$ 表示从坐标 $(0,0)$ 开始执行字符串 $s$ 的前 $i$ 条命令所能到达的坐标(前 $i$ 个 $x$ 的偏移量的和与前 $i$ 个 $y$ 的偏移量的和),容易知道可以通过 $f(i-1)$ 来得到,其中 $f(0) = (0,0)$。对于询问 $(x,y,l,r)$,将 $s$ 的区间 $[l,r]$ 翻转得到 $s'$,再对 $s'$ 求 $f(i)$,是否经过坐标 $(x,y)$ 就等价于是否存在 $i \in [1, n]$ 使得 $f(i) = (x,y)$。可以知道如果直接暴力模拟的话时间复杂度是 $O(n \cdot q)$ 的,容易想到能否只用 $s$ 求得的 $f(i)$ 来处理每一个询问呢。

  将 $s'$ 分成以下 $3$ 段区间来考虑:$[0,l-1]$,$[l,r]$,$[r+1,n]$。

  对于区间 $[0,l-1]$,没有受到翻转的影响,即 $s'[0,l-1] = s[0,l-1]$,因此只需查看 $i \in [0,l-1]$ 中是否存在 $f(i) = (x,y)$。为了快速判断这里直接开一个 std::map<std::pair<int, int>, std::vector<int>> 记录每个 $f(i) = (x_i, y_i)$ 出现的位置(下标)。如果询问的 $(x,y)$ 有记录且最小下标不超过 $l-1$,那么在这个最小的下标处就有 $f(i) = (x,y)$,否则无解。

  对于区间 $[r+1,n]$,同样没有受到翻转的影响,有 $s'[r+1,n] = s[r+1,n]$。这是因为 $x_i$ 本质上是由前 $i$ 个 $x$ 的偏移量求和得到的,因此只要起点不变,无论前 $i$ 条命令以什么顺序执行,求和得到的结果都是一样的。同理 $y_i$。如果询问的 $(x,y)$ 有记录且最大下标至少是 $r+1$,那么在这个最大的下标处就有 $f(i) = (x,y)$,否则无解。

  对于区间 $[l,r]$,为了方便这里定义 $g(i) = (x_i, y_i)$ 表示 $s[i, n]$ 中 $x$ 的偏移量的和与 $y$ 的偏移量的和,可以通过 $g(i+1)$ 来得到,同时有 $g(i) = f(n) - f(i-1)$。对于位置 $i \in [l,r]$,可以理解成从起点 $f(l-1)$ 走 $i - (l-1)$ 步到达的坐标,这是对于 $s'$ 而言的。转换到 $s$ 中,这 $i - (l-1)$ 步的偏移量就是 $g(i) - g(r+1)$,那么这个坐标就是 $f(l-1) + g(i) - g(r+1)$。因此若存在解就等价于存在 $i \in [l,r]$ 使得

\begin{align*}
& \quad\;\;  f(l-1) + g(i) - g(r+1) = (x,y) \\
&\Rightarrow g(i) = (x,y) - f(l-1) + g(r+1) \\
&\Rightarrow f(n) - f(i-1) = (x,y) - f(l-1) + f(n) - f(r) \\
&\Rightarrow f(i-1) = f(l-1) + f(r) - (x,y)
\end{align*}

  为此我们只需在坐标 $f(l-1) + f(r) - (x,y)$ 出现的所有下标中,二分出大于等于 $l-1$ 的最小值,然后判断是否不超过 $r-1$ 即可。

  所以我们只需求出关于 $s$ 的 $f(i)$,以及出现 $f(i)$ 的所有下标。

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

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 2e5 + 10;

char s[N];
PII f[N];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string ss = "URDL";

int main() {
    int n, m;
    scanf("%d %d %s", &n, &m, s + 1);
    map<PII, vector<int>> mp;
    mp[make_pair(0, 0)].push_back(0);
    for (int i = 1; i <= n; i++) {
        f[i] = f[i - 1];
        for (int j = 0; j < 4; j++) {
            if (s[i] == ss[j]) {
                f[i].first += dx[j];
                f[i].second += dy[j];
            }
        }
        mp[f[i]].push_back(i);
    }
    while (m--) {
        int x, y, l, r;
        scanf("%d %d %d %d", &x, &y, &l, &r);
        PII t = make_pair(x, y);
        if (mp.count(t) && mp[t][0] <= l - 1) {
            printf("YES\n");
        }
        else if (mp.count(t) && mp[t].back() >= r + 1) {
            printf("YES\n");
        }
        else {
            t.first = f[l - 1].first + f[r].first - t.first;
            t.second = f[l - 1].second + f[r].second - t.second;
            if (!mp.count(t)) {
                printf("NO\n");
            }
            else {
                auto it = lower_bound(mp[t].begin(), mp[t].end(), l - 1);
                printf("%s\n", it != mp[t].end() && *it < r ? "YES" : "NO");
            }
        }
    }
    
    return 0;
}

 

参考资料

  【A~F 讲解】Educational Codeforces Round 159 (Rated for Div. 2):https://www.bilibili.com/video/BV1Kg4y1f7nX/

标签:le,point,robot,偏移量,Robot,Queries,YES,first
From: https://www.cnblogs.com/onlyblues/p/17875949.html

相关文章

  • CF1902 D Robot Queries 题解
    LinkCF1902DRobotQueriesQuestionRobot初始在\((0,0)\),有一个字符串\(s\),表示运行列表\(U\):y+1\(D\):y-1\(L\):x-1\(R\):x+1之后有\(Q\)次询问,有\(L,R,x,y\),问把运行序列的\([L,R]\)反转,Robot是否经过了点\((x,y)\)Solution显然,对于一个区间\([L,R]\)......
  • SP1716 GSS3 - Can you answer these queries III 题解
    题意:给定一个长度为$n$的序列$a$,$q$次操作,每次操作为以下之一:\(0\)\(x\)\(y\):将\(a_x\)修改为\(y\)\(1\)\(l\)\(r\):询问区间\([l,r]\)的最大连续子序列和思路:考虑线段树维护区间最大连续子序列和:线段树每个节点需要维护的信息:区间左端点$l$,区......
  • CodeForces 1526F Median Queries
    洛谷传送门CF传送门首先,如果我们确定了\(1,2\)或\(n-1,n\)的位置,我们就能求出原排列。因为题目给了\(p_1<p_2\),所以可以不考虑对称性。也就是说我们知道两个位置是\(1,2\)或\(n-1,n\)但不确定究竟是\(1,2\)还是\(n-1,n\)也可以。然后,如果我们确定......
  • Problem: B. Queries on a String
    题意简述:给出一个字符串,每次给定l,r,k,选择一个子串l-r,然后子串向右移动k个单位.做法:每次k对子串的长度取模,然后模拟即可(使用substr函数截取前半段和后半段,交换前半段和后半段即可)点击查看代码//Problem:B.QueriesonaString//Contest:Codeforces-EducationalCo......
  • Web_XCTF_WriteUp | Training-WWW-Robots
    题目分析标题大致翻译:训练WWW网络爬虫。场景内部文段大致翻译:在这个小小的训练挑战中,您将学习Robots_exclusion_standard(网络爬虫排除标准)。robots.txt文件用于网络爬虫检查它们是否被允许抓取和索引您的网站或仅部分网站。有时,这些文件揭示了目录结构,而不是保护内......
  • SP19543 GSS8 - Can you answer these queries VIII 题解
    更好的阅读体验SP19543GSS8-CanyouanswerthesequeriesVIIIfhq+二项式定理。提供一个不太一样的思路。默认下标从\(1\)开始。首先插入删除,区间查询,想到可以平衡树维护或者离线下来做线段树。本文中是用的是fhq,好写一些。\(k\)非常的小,考虑对于每一个\(k\)的答......
  • SP1557 GSS2 - Can you answer these queries II 题解
    SP1557GSS2-CanyouanswerthesequeriesII更好的阅读体验扫描线。把询问挂在右端点上,扫描右端点,纵轴仍为序列维。对于这种出现多次的数只算一次的,记\(pre_i\)表示\(i\)这个值上一次的出现位置,套路化的可以强制让出现多次的在\(pre_i<l\wedgei\)统计,用二维线段树状......
  • RoboTAP笔记
    title:RoboTAP笔记banner_img:https://drive.studyinglover.com/api/raw/?path=/photos/blog/background/1679396994125.pngindex_img:https://cdn.studyinglover.com/pic/2023/08/15ff4915dff842e47e91d580d0d0fe5c.pngdate:2023-9-112:35:00categories:-笔记tags:-......
  • robots后台泄露
     [^来源:ctfshow-vip题目限免  考点:robots.txt文件泄露后台路径WP1.题目 唉,就是一道简单robots文件泄露,但是我为什么要写这个呢,因为我真的大可爱,一直搁那/robots,,,,,我说怎么没反应,,,无语,,,是robots.txt文件啊,文件我不加后缀名,我服了,我记得之前也是做过两次这种的题......
  • 【misc】[CISCN 2021初赛]robot --流量包数据提取,坐标画图
    打开附件的流量包可以发现有很多的tcp协议数据,追踪tcp协议数据看看可以发现tcp数据流中有很多类似坐标的东西,先把这些数据另存为txt保存,如何用正则表达式提取这些数据,提取脚本如下:importrewithopen("data.txt","r",encoding="utf-8")asf:    data=f.read......