zoukankan      html  css  js  c++  java
  • water tree

    题目链接:

    洛谷:题目链接

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define re register
    #define pb push_back
    const int N=1e6+10;
    void read(int &a)
    {
        a=0;int d=1;char ch;
        while(ch=getchar(),ch>'9'||ch<'0')
            if(ch=='-')
                d=-1;
        a=ch^48;
        while(ch=getchar(),ch>='0'&&ch<='9')
            a=(a<<3)+(a<<1)+(ch^48);
        a*=d;
    }
    int f[N],son[N],dep[N],siz[N],id[N],cnt,top[N],n,rt=1;
    struct note{int l,r,sum,lazy;}tree[N<<1];
    vector <int> v[N];
    void dfs1(int x)
    {
        siz[x]=1,dep[x]=dep[f[x]]+1;
        for(auto i:v[x])
        {
            if(i!=f[x])
            {
                f[i]=x;dfs1(i),siz[x]+=siz[i];
                if(siz[son[x]]<siz[i]) son[x]=i;
            }
        }
    }
    void dfs2(int x,int tp)
    {
        top[x]=tp,id[x]=++cnt;
        if(son[x]) dfs2(son[x],tp);
        for(auto i:v[x]) if(i!=f[x]&&i!=son[x]) dfs2(i,i);
    }
    void build(int l,int r,int now)
    {
        tree[now].l=l,tree[now].r=r;
        tree[now].lazy=-1;
        if(l==r) return;
        int m=l+r>>1;
        build(l,m,now<<1),build(m+1,r,now<<1|1);
    }
    void work(int now,int k)
    {
        tree[now].sum=(tree[now].r-tree[now].l+1)*k;
        tree[now].lazy=k;
    }
    void pushdown(int now)
    {
        work(now<<1,tree[now].lazy);
        work(now<<1|1,tree[now].lazy);
        tree[now].lazy=-1;
    }
    void modify(int l,int r,int now,int w)
    {
        if(tree[now].l>=l&&tree[now].r<=r) {work(now,w);return;}
        if(tree[now].lazy!=-1) pushdown(now);
        int m=tree[now].l+tree[now].r>>1;
        if(m>=l) modify(l,r,now<<1,w);
        if(m<r) modify(l,r,now<<1|1,w);
        tree[now].sum=tree[now<<1].sum+tree[now<<1|1].sum;
    }
    int query(int l,int r,int now)
    {
        if(tree[now].l>=l&&tree[now].r<=r) {return tree[now].sum;}
        if(tree[now].lazy!=-1) pushdown(now);
        int m=tree[now].l+tree[now].r>>1;
        int res=0;
        if(m>=l) res+=query(l,r,now<<1);
        if(m<r) res+=query(l,r,now<<1|1);
        tree[now].sum=tree[now<<1].sum+tree[now<<1|1].sum;
        return res;
    }
    void update(int x,int y,int z)
    {
        while(top[x]!=top[y])
        {
            if(dep[top[x]]<dep[top[y]]) swap(x,y);
            modify(id[top[x]],id[x],1,z);
            x=f[top[x]];
        }
        if(dep[x]>dep[y]) swap(x,y);
        modify(id[x],id[y],1,z);
    }
    int main()
    {
        read(n);
        for(re int i=1,x,y;i<n;i++) read(x),read(y),v[x].pb(y),v[y].pb(x);
        dfs1(rt),dfs2(rt,rt);
        build(1,n,1);
        int T; read(T);
        for(re int i=1,op,x;i<=T;i++)
        {
            read(op),read(x);
            if(op==1) modify(id[x],id[x]+siz[x]-1,1,1);
            else if(op==2) update(1,x,0);
            else printf("%d
    ",query(id[x],id[x],1));
        }
        return 0;
    }
  • 相关阅读:
    Integer中计算int位数的方法
    Spark学习---常见的RDD转和行动操作
    Spark学习---RDD编程
    《教父》中的经典台词以及英文原版
    关于MATSIM中,如何关闭自动加载dtd的问题
    源发行版8需要目标发行版1.8
    关于Mysql中GROUP_CONCAT函数返回值长度的坑
    【转】通过xml处理sql语句时对小于号与大于号的处理转换
    XmlDocument根据节点的属性值获取节点
    【转】使用SevenZipSharp压缩、解压文件
  • 原文地址:https://www.cnblogs.com/acm1ruoji/p/11852050.html
Copyright © 2011-2022 走看看