Luo Gu P4092 [HEOI2016 / TJOI2016] tree

Luo Gu P4092 [HEOI2016 / TJOI2016] tree

Description

  • In 2016, Jia Yuan sister just learning the tree, very happy. Now he wants to solve such a problem: Given a rooted tree root 1, the following two actions:

    1. Marking operation: a node marked with numerals. (In the beginning, only node 1 marked, no marks other nodes, but also for a node, you can play multiple markers.)
    2. Asks the operator: Ask a node of a recent ancestor hit the mark. (This node itself can be considered their ancestors)

    Can you help her?

Input

  • The first line of two positive integers N and Q represents the number of nodes and the number of operations.

    Then N-1 lines, each two positive integers u, v (1⩽u, v⩽n) represents a u to v has a directed edge.

    Next row Q, shaped like oper num, operis Cthat this is a marking operation, when operis Qthat this is a query operation.

Output

  • It outputs a positive integer representing the result

Sample Input

5 5 
1 2 
1 3 
2 4 
2 5 
Q 2 
C 2 
Q 2 
Q 5 
Q 3

Sample Output

1
2
2
1

Data Size

  • 100% data, 1⩽N, Q⩽100000

answer:

  • Tree section.
  • This question was at qbxt when the teacher taught with disjoint-set, after-school teacher Can I ask a tree section. His answer was not the beginning. Later told me difficult to achieve.
  • emmm .... Now I think it is not very simple? (doubt
  • Tree cross chain and tree maintenance, modification is simple. Query on each jump to 1:00 at the same time to see whether the current heavy chain and a> 0, if greater than, the heavy chain to go in this half. Otherwise, continue to jump up. This ensures that the point must be found half a query from the nearest point.
  • Because dfs sequence of the heavy chain is a contiguous range, it is possible to obtain a binary number dfs, to convert it to a point.
#include <iostream>
#include <cstdio>
#include <cmath>
#define N 100005
#define p1 (p << 1)
#define p2 (p << 1 | 1)
using namespace std;

struct T {int l, r, val;} t[N * 4];
struct E {int next, to;} e[N * 2];
int n, q, num, dex, logMax;
int h[N], fat[N], size[N], dep[N];
int p[N], son[N], dfn[N], top[N];
int f[N][25];

int read()
{
    int x = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x;
}

void add(int u, int v)
{
    e[++num].next = h[u];
    e[num].to = v;
    h[u] = num;
}

void dfs1(int x, int fa, int de)
{
    size[x] = 1, fat[x] = fa, dep[x] = de;
    int maxSon = 0;
    for(int i = h[x]; i != 0; i = e[i].next)
        if(e[i].to != fa)
        {
            dfs1(e[i].to, x, de + 1);
            size[x] += size[e[i].to];
            if(size[e[i].to] > maxSon)
            {
                maxSon = size[e[i].to];
                son[x] = e[i].to;
            }
        }
}

void dfs2(int x, int head)
{
    dfn[x] = ++dex, p[dex] = x, top[x] = head;
    if(!son[x]) return;
    dfs2(son[x], head);
    for(int i = h[x]; i != 0; i = e[i].next)
        if(e[i].to != fat[x] && e[i].to != son[x])
            dfs2(e[i].to, e[i].to);
}

void build(int p, int l, int r)
{
    t[p].l = l, t[p].r = r;
    if(l == r) return;
    int mid = l + r >> 1;
    build(p1, l, mid), build(p2, mid + 1, r);
}

void upd(int p, int l, int r)
{
    if(t[p].l >= l && t[p].r <= r) {t[p].val = 1; return;}
    int mid = t[p].l + t[p].r >> 1;
    if(l <= mid) upd(p1, l, r);
    if(r > mid) upd(p2, l, r);
    t[p].val = t[p1].val + t[p2].val;
}

int ask(int p, int l, int r)
{
    if(t[p].l >= l && t[p].r <= r) return t[p].val;
    int mid = t[p].l + t[p].r >> 1, res = 0;
    if(l <= mid) res += ask(p1, l, r);
    if(r > mid) res += ask(p2, l, r);
    return res;
}

int cal(int l, int r)
{
    if(l == r) return l;
    int mid = l + r >> 1;
    if(ask(1, mid + 1, r)) return cal(mid + 1, r);
    return cal(l, mid);
}

int askLink(int x, int y)
{
    while(top[x] != top[y])
    {
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        if(ask(1, dfn[top[x]], dfn[x]))
            return cal(dfn[top[x]], dfn[x]);
        x = fat[top[x]];
    }
    if(dep[x] > dep[y]) swap(x, y);
    return cal(dfn[x], dfn[y]);
}

int main()
{
    cin >> n >> q, logMax = (int)log2(n);
    for(int i = 1; i < n; i++)
    {
        int u = read(), v = read();
        add(u, v), add(v, u);
    }
    dfs1(1, 0, 1), dfs2(1, 1), build(1, 1, n);
    upd(1, dfn[1], dfn[1]);
    for(int i = 1; i <= q; i++)
    {
        char c[3]; scanf("%s", c);
        int x = read();
        if(c[0] == 'C') upd(1, dfn[x], dfn[x]);
        else if(c[0] == 'Q') printf("%d\n", p[askLink(1, x)]);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11784915.html