Water Tree CodeForces - 343D (dfs序+线段树)

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

思路:

应该算是固定套路题了,不过多了点细节,因为还有一个清空它和它祖先的操作(如果上树链剖分的话,那就没啥细节)

对处理祖先节点的操作,可以这样考虑:先直接进行单点更新,再进行区间更新或者查询的时候,只要判断这个区间的和是否满足全1即可,如果不满足,那么说明这个子树上有空,那么它一定为空。在区间更新时,也进行一次查询,如果发现不满足全1,就先把它的父亲也置0。

代码:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+7;
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
int n;
int L[maxn],R[maxn];
int dfn;
int head[maxn],num;
int f[maxn];
struct Edge
{
    int u,v,next;
}edge[maxn<<2];
void addEdge(int u,int v)
{
    edge[num].u=u;
    edge[num].v=v;
    edge[num].next=head[u];
    head[u]=num++;
}
void dfs(int u,int pre)
{
    L[u]=++dfn;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==pre) continue;
        f[v]=u;
        dfs(v,u);
    }
    R[u]=dfn;
}
void init()
{
    dfn=0,num=0;
    memset(head,-1,sizeof(head));
}
struct Tree
{
    int l,r,lazy,sum;
}tree[maxn<<2];
void push_up(int rt)
{
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void push_down(int rt)
{
    if(tree[rt].lazy!=-1)
    {
        int tmp=tree[rt].lazy;
        tree[rt<<1].sum=tmp*(tree[rt<<1].r-tree[rt<<1].l+1);
        tree[rt<<1].lazy=tmp;
        tree[rt<<1|1].sum=tmp*(tree[rt<<1|1].r-tree[rt<<1|1].l+1);
        tree[rt<<1|1].lazy=tmp;
        tree[rt].lazy=-1;
    }
}
void Build(int l,int r,int rt)
{
    tree[rt].l=l,tree[rt].r=r;
    tree[rt].lazy=-1;
    tree[rt].sum=0;
    if(l==r) return;
    int m=(l+r)>>1;
    Build(Lson);
    Build(Rson);
}
void updata(int L,int R,int f,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        tree[rt].sum=f*(tree[rt].r-tree[rt].l+1);
        tree[rt].lazy=f;
        return;
    }
    push_down(rt);
    int m=(l+r)>>1;
    if(L<=m) updata(L,R,f,Lson);
    if(R>m) updata(L,R,f,Rson);
    push_up(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return tree[rt].sum;
    }
    push_down(rt);
    int m=(l+r)>>1;
    int ans=0;
    if(R>m) ans+=query(L,R,Rson);
    if(L<=m) ans+=query(L,R,Lson);
    push_up(rt);
    return ans;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    scanf("%d",&n);
    init();
    for(int i=1,x,y;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        addEdge(x,y);
        addEdge(y,x);
    }
    int q;
    dfs(1,0);
    Build(1,n,1);
    scanf("%d",&q);
    while(q--)
    {
        int op,x;
        scanf("%d%d",&op,&x);
        if(op==1)
        {
            int tmp=query(L[x],R[x],1,n,1);
            if(tmp!=(R[x]-L[x]+1)&&x!=1)//细节
            {
                updata(L[f[x]],L[f[x]],0,1,n,1);
            }
            updata(L[x],R[x],1,1,n,1);
        }
        else if(op==2)
        {
           updata(L[x],L[x],0,1,n,1);
        }
        else
        {
            int tmp=query(L[x],R[x],1,n,1);
            if(tmp!=(R[x]-L[x]+1))
            {
                printf("0\n");
            }
            else
            {
                printf("1\n");
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值