首页 > 其他分享 >Codeforces-343D Water Tree(树链剖分)

Codeforces-343D Water Tree(树链剖分)

时间:2023-02-03 10:05:05浏览次数:40  
标签:剖分 int tree vertex Codeforces Tree include 节点 change

Description:

Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water.

The vertices of the tree are numbered from 1 to n with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of this vertex, and the vertex is connected with each of the children by a pipe through which water can flow downwards.

Mike wants to do the following operations with the tree:

  1. Fill vertex v with water. Then v and all its children are filled with water.
  2. Empty vertex v. Then v and all its ancestors are emptied.
  3. Determine whether vertex v is filled with water at the moment.

Initially all vertices of the tree are empty.

Mike has already compiled a full list of operations that he wants to perform in order. Before experimenting with the tree Mike decided to run the list through a simulation. Help Mike determine what results will he get after performing all the operations.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 500000) — the number of vertices in the tree. Each of the following n - 1 lines contains two space-separated numbers aibi (1 ≤ ai, bi ≤ nai ≠ bi) — the edges of the tree.

The next line contains a number q (1 ≤ q ≤ 500000) — the number of operations to perform. Each of the following q lines contains two space-separated numbers ci (1 ≤ ci ≤ 3), vi (1 ≤ vi ≤ n), where ci is the operation type (according to the numbering given in the statement), and vi is the vertex on which the operation is performed.

It is guaranteed that the given graph is a tree.

Output

For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input.

Examples

input

5 1 2 5 1 2 3 4 2 12 1 1 2 3 3 1 3 2 3 3 3 4 1 2 2 4 3 1 3 3 3 4 3 5

output

0 0 0 1 0 1 0 1

题意:

给出一个具有N个点的树,1号节点是根节点。现在有三种操作:

1 x,表示给x节点灌溉水,并且其儿子都会被灌上水。

2 x,表示将x节点的水抽干,并且其父亲节点会一路被抽干。

3 x,表示询问x节点是否有水。有水输出1,否则输出0。

对于操作1:DFS的时候可以记录从每个点进入的时刻L[x]和最后出来的时刻R[X],更新就是[L[X],R[X]];对于操作2:就利用树链剖分从节点x向节点1靠拢并更新。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<vector>
using namespace std;
typedef long long ll;
typedef double ld;
const int MAX=6e5;
vector<int>e[MAX];
struct lenka
{
    int l,r;
    int ans,tag;
}A[MAX<<2];
int d[MAX],son[MAX],fa[MAX],siz[MAX],top[MAX],L[MAX],R[MAX],all;
void build(int k,int l,int r)
{
    A[k].l=l,A[k].r=r;
    A[k].ans=A[k].tag=0;
    if(l==r)return;
    build(2*k,l,(l+r)/2);
    build(2*k+1,(l+r)/2+1,r);
}
void change(int k,int x,int y,int tag)
{
    if(x==A[k].l&&y==A[k].r)
    {
        A[k].ans=tag;
        if(x!=y)A[k].tag=1;
        return;
    }
    if(A[k].tag)
    {
        change(2*k,A[2*k].l,A[2*k].r,A[k].ans);
        change(2*k+1,A[2*k+1].l,A[2*k+1].r,A[k].ans);
        A[k].tag=0;
    }
    if(y<=A[2*k].r)change(2*k,x,y,tag);
    else if(x>=A[2*k+1].l)change(2*k+1,x,y,tag);
    else
    {
        change(2*k,x,A[2*k].r,tag);
        change(2*k+1,A[2*k+1].l,y,tag);
    }
}
int ask(int k,int x)
{
    if(x==A[k].l&&x==A[k].r)
        return A[k].ans;
    if(A[k].tag)
    {
        change(2*k,A[2*k].l,A[2*k].r,A[k].ans);
        change(2*k+1,A[2*k+1].l,A[2*k+1].r,A[k].ans);
        A[k].tag=0;
    }
    if(x<=A[2*k].r)
    return ask(2*k,x);
    return ask(2*k+1,x);
}
void dfs1(int k,int f,int dep)
{
    d[k]=dep;
    siz[k]=1;
    son[k]=0;
    fa[k]=f;
    for(int i=0;i<e[k].size();i++)
    {
        int nex=e[k][i];
        if(nex==f)continue;
        dfs1(nex,k,dep+1);
        siz[k]+=siz[nex];
        if(siz[son[k]]<siz[nex])son[k]=nex;
    }
}
void dfs2(int k,int tp)
{
    top[k]=tp;
    L[k]=++all;
    if(son[k])dfs2(son[k],tp);
    for(int i=0;i<e[k].size();i++)
    {
        if(e[k][i]==fa[k]||e[k][i]==son[k])
            continue;
        dfs2(e[k][i],e[k][i]);
    }
    R[k]=all;
}
void slove(int x,int y)
{
    while(top[x]!=top[y])
    {
        if(d[top[x]]<d[top[y]])swap(x,y);
        change(1,L[top[x]],L[x],0);
        x=fa[top[x]];
    }
    if(d[x]>d[y])swap(x,y);
    change(1,L[x],L[y],0);

}
int main()
{
    int n,m;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        e[x].push_back(y);
        e[y].push_back(x);
    }
    all=0;
    dfs1(1,0,1);
    dfs2(1,1);
    build(1,1,all);
    scanf("%d",&m);
    while(m--)
    {
        int op,x;
        scanf("%d%d",&op,&x);
        if(op==1)
            change(1,L[x],R[x],1);
        else if(op==2)
        slove(1,x);
        else
        printf("%d\n",ask(1,L[x]));
    }
    return 0;
}

 

标签:剖分,int,tree,vertex,Codeforces,Tree,include,节点,change
From: https://blog.51cto.com/u_15952369/6034925

相关文章

  • Codeforces Round #596 B2. TV Subscriptions
    题意就是让你在N个数中找到D个连续的数,使这D个数中不同的数最小。hard数据较大,优化到nlogn才能过。具体怎么优化看代码吧AC代码:#include<cstdio>#include<cstring>......
  • SPOJ375--Query on a tree(树链剖分)
    Description:Youaregivenatree(anacyclicundirectedconnectedgraph)with N nodes,andedgesnumbered1,2,3...N-1.Wewillaskyoutoperfromsomeins......
  • Codeforces Round #596 C. p-binary
    给定N和p,让你找到满足2^x+p最少有多少不同的项。就把N转成二进制然后枚举P的个数就是答案,昨天特判没写好,今天早上起来发现被卡掉了。rank又出1000了。AC代码:#include<......
  • Educational Codeforces Round 75 D. Salary Changing(二分)
    题意就是:给n个人发工资,总钱数为s。每个人有一个工资范围。要求一个发工资方案,使得工资中位数最大,求这个中位数。考虑到中位数最大,于是我们可以二分。但是每个人的工资......
  • 树链剖分
    “在一棵树上进行路径的修改、求极值、求和”乍一看只要线段树就能轻松解决,实际上,仅凭线段树是不能搞定它的。我们需要用到一种貌似高级的复杂算法——树链剖分。树链剖分......
  • Educational Codeforces Round 75 C Minimize The Integer
    这道题的意思就是给出一个由数字组成的字符串,相邻的数字可以互换位置,但是如果相邻的为同奇同偶这样就不能交换。让我们求交换任意次数可以产生的最小数。这条限制就是说......
  • codeforces 595 C2. Good Numbers (hard version)
    给出Q组查询,每组给出一个N找到一个>=n的m,m可以分解成不同的3的幂次相加。可以看题意解释,就是转化为3^0,3^1,...,3^m,每个只能出现最多一次,但是可以不同组合,输出符合条件最......
  • POJ3468 A Simple Problem with Integers(SplayTree做法)
    DescriptionYouhave N integers, A1, A2,..., AN.Youneedtodealwithtwokindsofoperations.Onetypeofoperationistoaddsomegivennumbertoea......
  • codeforces 595 B2 Books Exchange (hard version)
    这道题的意思就是有n本书,每本书都有自己的编号,每次可以移动一本书,把这个本书移动到当前编号对应的位置,求移动几次可以使得编号和位置对应起来。比如样例32312第一......
  • [LeetCode] 1145. Binary Tree Coloring Game
    Twoplayersplayaturnbasedgameonabinarytree.Wearegiventhe root ofthisbinarytree,andthenumberofnodes n inthetree. n isodd,andeach......