CodeForces 343D water tree(树链剖分)

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:

Fill vertex v with water. Then v and all its children are filled with water.
Empty vertex v. Then v and all its ancestors are emptied.
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 ai, bi (1 ≤ ai, bi ≤ n, ai ≠ 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 将u的子树修改为1

2 将u及其所有祖先修改为0

3 查询树上标号为u的点的值

题解:非常裸的树链剖分,连push_up都不用,嗯,就是这样。

代码如下:

#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson root<<1
#define rson root<<1|1
using namespace std;

struct node
{
    int l,r,sum,lazy;
}tr[2000020];
vector<int> g[500050];
int deep[500050],size[500050],fa[500050],son[500050],id[500050],top[500050],cnt;

void push_down(int root)
{
    int mid=(tr[root].l+tr[root].r)>>1;
    tr[lson].sum=tr[root].lazy*(mid-tr[root].l+1);
    tr[lson].lazy=tr[root].lazy;
    tr[rson].sum=tr[root].lazy*(tr[root].r-mid);
    tr[rson].lazy=tr[root].lazy;
    tr[root].lazy=-1;
}

void build(int root,int l,int r)
{
    if(l==r)
    {
        tr[root].l=l;
        tr[root].r=r;
        tr[root].lazy=-1;
        tr[root].sum=0;
        return ;
    }
    tr[root].l=l;
    tr[root].r=r;
    tr[root].lazy=-1;
    int mid=(l+r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
}

void update(int root,int l,int r,int val)
{
    if(tr[root].l==l&&tr[root].r==r)
    {
        tr[root].sum=val;
        tr[root].lazy=val;
        return ;
    }
    if(~tr[root].lazy)
    {
        push_down(root);
    }
    int mid=(tr[root].l+tr[root].r)>>1;
    if(mid<l)
    {
        update(rson,l,r,val);
    }
    else
    {
        if(mid>=r)
        {
            update(lson,l,r,val);
        }
        else
        {
            update(lson,l,mid,val);
            update(rson,mid+1,r,val);
        }
    }
}

int query(int root,int l,int r)
{
    if(tr[root].l==l&&tr[root].r==r)
    {
        return tr[root].sum;
    }
    if(~tr[root].lazy)
    {
        push_down(root);
    }
    int mid=(tr[root].l+tr[root].r)>>1;
    if(mid<l)
    {
        return query(rson,l,r);
    }
    else
    {
        if(mid>=r)
        {
            return query(lson,l,r);
        }
        else
        {
            return query(lson,l,mid)+query(rson,mid+1,r);
        }
    }
}

void dfs1(int now,int f,int dep)
{
    fa[now]=f;
    size[now]=1;
    deep[now]=dep;
    int maxson=-1;
    for(int i=0;i<g[now].size();i++)
    {
        if(g[now][i]==f)
        {
            continue;
        }
        dfs1(g[now][i],now,dep+1);
        size[now]+=size[g[now][i]];
        if(size[g[now][i]]>maxson)
        {
            maxson=size[g[now][i]];
            son[now]=g[now][i];
        }
    }
}

void dfs2(int now,int topf)
{
    id[now]=++cnt;
    top[now]=topf;
    if(!son[now])
    {
        return ;
    }
    dfs2(son[now],topf);
    for(int i=0;i<g[now].size();i++)
    {
        if(g[now][i]==son[now]||g[now][i]==fa[now])
        {
            continue;
        }
        dfs2(g[now][i],g[now][i]);
    }
}

void sub_update(int x)
{
    update(1,id[x],id[x]+size[x]-1,1);
}

void path_update(int u,int v)
{
    while(top[u]!=top[v])
    {
        if(deep[top[u]]<deep[top[v]])
        {
            swap(u,v);
        }
        update(1,id[top[u]],id[u],0);
        u=fa[top[u]];
    }
    if(deep[u]>deep[v])
    {
        swap(u,v);
    }
    update(1,id[u],id[v],0);
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int from,to;
        scanf("%d%d",&from,&to);
        g[from].push_back(to);
        g[to].push_back(from);
    }
    dfs1(1,0,1);
    dfs2(1,1);
    build(1,1,n);
    int m;
    scanf("%d",&m);
    while(m--)
    {
        int kd,u;
        scanf("%d%d",&kd,&u);
        if(kd==1)
        {
            sub_update(u);
        }
        else
        {
            if(kd==2)
            {
                path_update(1,u);
            }
            else
            {
                printf("%d\n",query(1,id[u],id[u]));
            }
        }
    }
}

原文地址:https://www.cnblogs.com/stxy-ferryman/p/9002304.html

时间: 2024-10-07 06:58:41

CodeForces 343D water tree(树链剖分)的相关文章

codeforces 343D Water Tree 树链剖分 dfs序 线段树 set

题目链接 这道题主要是要考虑到同一棵子树中dfs序是连续的 然后我就直接上树剖了... 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int MAXN=600005; 4 5 struct Node 6 { 7 int l,r; 8 int value; 9 void init() 10 { 11 l=r=value=0; 12 } 13 }tree[4*MAXN]; 14 vector<int>nei[MAXN]

Water Tree(树链剖分+dfs时间戳)

Water Tree http://codeforces.com/problemset/problem/343/D time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Ea

CF343D Water Tree 树链剖分

问题描述 LG-CF343D 题解 树剖,线段树维护0-1序列 yzhang:用珂朵莉树维护多好 \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; template <typename Tp> void read(Tp &x){ x=0;char ch=1;int fh; while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); if(

SPOJ375 Query on a tree 树链剖分

SPOJ375  Query on a tree   树链剖分 no tags You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of

SPOJ - QTREE 375 Query on a tree 树链剖分+线段树

操作1:修改第k条边权. 操作2:询问两点间最大边权. 树链剖分,然后线段树维护最大值 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #inclu

Codeforces 343D Water Tree & 树链剖分教程

原题链接 题目大意 给定一棵根为1,初始时所有节点值为0的树,进行以下三个操作: 将以某点为根的子树节点值都变为1 将某个节点及其祖先的值都变为0 *询问某个节点的值 解题思路 这是一道裸的树链剖分题.下面详细地介绍一下树链剖分. 树链剖分预备知识: 线段树.DFS序 树链剖分想法|起源 首先,如果一棵树退化成一条链,那么它会有非常好的性质.我们可以用线段树等数据结构来维护相关操作,使得效率更高.那么我们考虑一般的树,它是否能被分成一些链,使它们也能更高效地进行某些操作? 算法流程 以下以点带权

Aizu 2450 Do use segment tree 树链剖分+线段树

Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show.php?pid=39566 Description Given a tree with n (1 ≤ n ≤ 200,000) nodes and a list of q (1 ≤ q ≤ 100,000) queries, process the queries in order and out

spoj 375 Query on a tree (树链剖分)

Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of the i-th edge to ti or Q

SPOJ Query on a tree 树链剖分 水题

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of the i-th edge to tior QUERY a b : ask fo