首页 > 其他分享 >G. Unusual Entertainment

G. Unusual Entertainment

时间:2023-11-21 10:15:42浏览次数:32  
标签:10 Unusual le Alex Entertainment int text YES

G. Unusual Entertainment

A tree is a connected graph without cycles.

A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in any order. For example, $[5, 1, 3, 2, 4]$ is a permutation, but $[2, 1, 1]$ is not a permutation (as $1$ appears twice in the array) and $[1, 3, 2, 5]$ is also not a permutation (as $n = 4$, but $5$ is present in the array).

After a failed shoot in the BrMeast video, Alex fell into depression. Even his birthday did not make him happy. However, after receiving a gift from Timofey, Alex's mood suddenly improved. Now he spent days playing with the gifted constructor. Recently, he came up with an unusual entertainment.

Alex builds a tree from his constructor, consisting of $n$ vertices numbered from $1$ to $n$, with the root at vertex $1$. Then he writes down each integer from $1$ to $n$ in some order, obtaining a permutation $p$. After that, Alex comes up with $q$ triples of integers $l, r, x$. For each triple, he tries to determine if there is at least one descendant of vertex $x$ among the vertices $p_l, p_{l+1}, \ldots, p_r$.

A vertex $u$ is a descendant of vertex $v$ if and only if $\mathrm{dist}(1, v) + \mathrm{dist}(v, u) = \mathrm{dist}(1, u)$, where $\mathrm{dist}(a, b)$ is the distance between vertices $a$ and $b$. In other words, vertex $v$ must be on the path from the root to vertex $u$.

Alex told Zakhar about this entertainment. Now Alex tells his friend $q$ triples as described above, hoping that Zakhar can check for the presence of a descendant. Zakhar is very sleepy, so he turned to you for help. Help Zakhar answer all of Alex's questions and finally go to sleep.

Input

The first line of the input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains two integers $n, q$ ($1 \le n, q \le 10^5$) — the number of vertices in the tree and the number of questions, respectively.

Each of the next $n - 1$ lines contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$), indicating that there is an edge between vertices $u_i$ and $v_i$ (it is guaranteed that the resulting graph is a tree).

The next line contains $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$) — the permutation $p$ (it is guaranteed that each integer from $1$ to $n$ appears exactly once).

Then follow $q$ lines describing Alex's questions. The $i$-th line contains three integers $l, r, x$ ($1 \le l \le r \le n$, $1 \le x \le n$), as described in the statement.

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

Output

For each of Alex's questions, print "Yes" (without quotes) if the described descendant exists, otherwise print "No" (without quotes).

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Example

input

3
3 5
1 2
2 3
1 2 3
1 2 2
1 2 3
2 3 1
1 2 3
2 3 3
10 10
2 6
2 7
2 4
1 7
2 8
10 6
8 5
9 4
3 4
10 2 5 9 1 7 6 4 3 8
8 9 8
7 8 1
7 10 6
4 8 9
5 5 10
7 10 1
9 9 2
9 10 6
6 6 2
10 10 6
1 1
1
1 1 1

output

YES
NO
YES
NO
YES

NO
YES
YES
YES
NO
YES
YES
NO
NO
NO

YES

 

解题思路

  问 $p_l \sim p_r$ 中是否存在一个节点是 $x$ 的子节点,等价于问 $p_l \sim p_r$ 中是否存在一个节点出现在以 $x$ 为根的子的子树中。

  为此可以考虑 dfs 序,如果一个节点 $i$ 在以 $x$ 为根的子树中,那么必然有 $\text{tin}_x \leq \text{tin}_i \leq \text{tout}_i \leq \text{tout}_x$。所有问题就变成了是否存在一个 $i$ 同时满足 $\displaylines{\begin{cases} l \leq i \leq r \\ \text{tin}_x \leq \text{tin}_i \leq \text{tout}_x \end{cases}}$。

  这就是一个二维数点问题,可以参考这篇博客。把第一、二个约束分别看作是横坐标和纵坐标的某个区间范围,那么点的坐标就是 $(p_i, \text{tin}_i)$,每次询问就相当于求左下角为 $(l, \text{tin}_x)$ 和右上角为 $(r, \text{tout}_x)$ 这个矩形内点的数量是否大于 $0$。

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

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

typedef long long LL;

const int N = 1e5 + 10, M = N * 2;

int n, m;
int head[N], e[M], ne[M], idx;
int tin[N], tout[N], sz;
int p[N];
struct Node {
    int x, y, c, idx;
}q[N * 4];
int tr[N];
int ans[N];

void add(int u, int v) {
    e[idx] = v, ne[idx] = head[u], head[u] = idx++;
}

void dfs(int u, int pre) {
    tin[u] = ++sz;
    for (int i = head[u]; i != -1; i = ne[i]) {
        if (e[i] != pre) {
            dfs(e[i], u);
        }
    }
    tout[u] = sz;
}

int lowbit(int x) {
    return x & -x;
}

void modify(int x, int c) {
    for (int i = x; i <= n; i += lowbit(i)) {
        tr[i] += c;
    }
}

int query(int x) {
    int ret = 0;
    for (int i = x; i; i -= lowbit(i)) {
        ret += tr[i];
    }
    return ret;
}

void solve() {
    scanf("%d %d", &n, &m);
    memset(head, -1, n + 10 << 2);
    idx = 0;
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        add(u, v), add(v, u);
    }
    for (int i = 1; i <= n; i++) {
        scanf("%d", p + i);
    }
    sz = 0;
    dfs(1, -1);
    for (int i = 0, j = 0; i < m; i++) {
        int l, r, x;
        scanf("%d %d %d", &l, &r, &x);
        int x1 = l, y1 = tin[x], x2 = r, y2 = tout[x];
        q[j++] = {x2, y2, 1, i};
        q[j++] = {x1 - 1, y1 - 1, 1, i};
        q[j++] = {x1 - 1, y2, -1, i};
        q[j++] = {x2, y1 - 1, -1, i};
    }
    sort(q, q + 4 * m, [&](Node &a, Node &b) {
        return a.x < b.x;
    });
    memset(tr, 0, n + 10 << 2);
    memset(ans, 0, n + 10 << 2);
    for (int i = 0, j = 1; i < m << 2; i++) {
        while (j <= n && j <= q[i].x) {
            modify(tin[p[j++]], 1);
        }
        ans[q[i].idx] += q[i].c * query(q[i].y);
    }
    for (int i = 0; i < m; i++) {
        printf("%s\n", ans[i] ? "YES" : "NO");
    }
    printf("\n");
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Разбор Codeforces Round 909 (Div. 3):https://codeforces.com/blog/entry/122407

  Codeforces Round 909 (Div. 3) A-G 讲解:https://www.bilibili.com/video/BV1Uu4y1b7Dc/

标签:10,Unusual,le,Alex,Entertainment,int,text,YES
From: https://www.cnblogs.com/onlyblues/p/17845588.html

相关文章

  • CF1899 G Unusual Entertainment 题解
    LinkCF1899GUnusualEntertainmentQuestion给出一个排列\(p_i\)和一棵树,给出\(Q\)组询问,每组询问\([L,R,x]\)表示求\(p_L\simp_R\)上是否存在\(p_i\)在\(x\)的字数上。Solution这道题确实是一个好题。我们先考虑一个问题,怎么样才能判断子树,我们给书上的每个......
  • cf1899G. Unusual Entertainment(启发式合并)
    https://codeforces.com/contest/1899/problem/G首先将将节点重新映射一下然后就是个启发式合并板题#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<map>#include<vector>#include<set>#include<queue>#in......
  • Codeforces Round #450 (Div. 2) D. Unusual Sequences 数学
    Countthenumberofdistinctsequencesa1, a2, …, an(1 ≤ ai)consistingofpositiveintegerssuchthatgcd(a1, a2, …, an) = xand.Asthisnumbercouldbelarge,printtheanswermodulo109 + 7.gcdheremeansthegreatestcommondivisor.Input......
  • Codeforces 1322 A. Unusual Competitions
    题意:给出一个含有的字符串,让你可以选择一个区间进行重新排序,问一共选择的区间长度是多少可以使得字符串最后变成我们只需要从头开始遍历然后找到这种字符,并且使得和......
  • jcenter(JCEntertainment)
    jcenter里的androidstudio微信sdk是哪个如果你只是要跑起来微信分享的demo,暂时使用它demo里边的debug.keystore就行,具体设置在window-preferences-android-build,在custom......