首页 > 其他分享 >P2486 [SDOI2011]染色

P2486 [SDOI2011]染色

时间:2022-10-28 21:23:39浏览次数:39  
标签:lc SDOI2011 int 染色 rhs color res P2486 px

#include<iostream>
#include<vector>
using namespace std;
#define int long long
const int N = 1e5 + 1;
int n , m ;
vector < int > to [ N ] ;
int dep [ N ] , fa [ N ] ,mson [ N ] , siz [ N ];
int value [ N ] ;
int top [ N ] ,vt [ N ], cnt, a [ N ] ;
void merge ( int x , int y ){ to [ x ] . push_back ( y ) ; to [ y ] . push_back ( x ) ; }
struct node {
    int lc , rc , color ;
    node(){lc = 0 , rc = 0 , color = 0 ;}
    node operator + ( const node & rhs ) const {
        node res ;
        if ( lc >  0 ) res . lc = lc;
        else
            res . lc = rhs . lc ;
        res . rc = rhs . rc;
        if ( rhs . lc == rc && rc > 0 ) res . color = -1;
        res . color += rhs . color;
        res . color += color;
        return res;
    }
} tree [ N << 2 ];
int tag [ N << 2 ];
namespace xds
{
    void build ( int k , int l , int r )
    {
        tag [ k ] = 0;
        if ( l == r ) {
                tree [ k ] . lc = tree [ k ] . rc = a [ l ] ;
                tree [ k ] . color = 1 ;
                return ;
        }
        int mid = l + r >> 1;
        build ( k << 1 , l , mid );
        build ( k << 1 | 1 , mid + 1 , r ) ;
        tree [ k ] = tree [ k << 1 ] + tree [ k << 1 | 1 ];
        return;
    }
    void push_down ( int k )
    {
        if ( tag [ k ] == 0 ) return;

        tree [ k << 1 ] . color = 1;
        tree [ k << 1 ] . lc = tree[ k << 1 ] . rc = tree [ k ] . lc;
        tag [ k << 1 ] = tag [ k ];

        tree [ k << 1 | 1 ] . color = 1 ;
        tree [ k << 1 | 1 ] . lc = tree [ k << 1 | 1 ] . rc = tree [ k ] . lc ;
        tag [ k << 1 | 1 ] = tag [ k ] ;

        tag [ k ] = 0;
    }
    void update ( int k , int l , int r , int x , int y , int c )
    {
        if ( l >= x && r <= y )
        {
            tree [ k ] . lc = tree [ k ] . rc = c;
            tree [ k ] . color = 1;
            tag [ k ] = c ;
            return;
        }
        int mid = l + r >> 1;
        push_down( k );
        if ( x <= mid ) update ( k << 1 , l , mid , x , y , c );
        if ( y > mid ) update ( k << 1 | 1 , mid + 1 , r , x , y , c ) ;
        tree [ k ] = tree [ k << 1 ] + tree [ k << 1 | 1 ];
        return ;
    }
    node query ( int k , int l , int r , int x , int y )
    {
        if ( l >= x && r <= y )
        {
            return tree [ k ]  ;
        }
        int mid = l + r >> 1;
        push_down ( k );
        node rhs ;
        if ( x <= mid ) rhs = rhs + query ( k << 1 , l , mid , x , y );
        if ( y > mid ) rhs = rhs + query ( k << 1 | 1 , mid + 1 , r , x , y );
        tree [ k ] = tree [ k << 1 ] + tree [ k << 1 | 1 ];
        return rhs ;
    }
}
namespace pf
{
    void dfs_1 ( int x , int father )
    {
        dep [ x ] = dep [ father ] + 1;
        fa [ x ] = father;
        siz [ x ] = 1;
        int maxsize = -1;
        for ( auto y : to [ x ] )
        {
            if ( y == father ) continue;
            dfs_1 ( y , x );
            siz [ x ] += siz [ y ] ;
            if ( siz [ y ] > maxsize )
                maxsize = siz [ y ] , mson [ x ] = y;
        }
        return ;
    }
    void dfs_2 ( int x , int topfa )
    {
        top [ x ] = topfa;
        vt [ x ] = ++ cnt;
        a [ cnt ] = value [ x ];
        if ( mson [ x ] > 0 )
            dfs_2 ( mson [ x ] , topfa );
        for ( auto y : to [ x ] )
        {
            if ( y == fa [ x ] ) continue;
            if ( y == mson [ x ] ) continue;
            dfs_2 ( y , y ) ;
        }
    }
    void cz_1 ( int x , int y , int z )
    {
        if ( dep [ x ] < dep [ y ] ) swap ( x , y );
        while ( top [ x ] != top [ y ] )
        {
            if ( dep [ top [ x ] ] <= dep [ top [ y ] ] ) swap ( x , y );
            xds :: update ( 1 , 1 , n , vt [ top [ x ] ] , vt [ x ] , z );
            x = top [ x ] ;
            x = fa [ x ] ;
        }
        if ( dep [ x ] < dep [ y ] ) swap ( x , y );
        xds :: update ( 1 , 1 , n , vt [ y ] , vt [ x ] , z ) ;
    }
    int cz_2 ( int x , int y )
    {
        node px , py ;
        while ( top [ x ] != top [ y ] ) {
            if ( dep [ top [ x ] ] <= dep [ top [ y ] ] ) {
                py = xds :: query ( 1 , 1 , n , vt [ top [ y ] ] , vt [ y ] ) + py;
                y = fa [ top [ y ] ];
            }
            else {
                px = xds :: query ( 1 , 1 , n , vt [ top [ x ] ] , vt [ x ] ) + px;
                x = fa [ top [ x ] ];
            }
        }
        if ( dep [ x ] > dep [ y ] )
        {
            px = xds :: query ( 1 , 1 , n , vt [ y ] , vt [ x ] ) + px;
            if ( px . lc == py . lc ) px . color -- ;
            return px . color + py . color;
        }
        else
        {
            py = xds :: query ( 1 , 1 , n , vt [ x ] , vt [ y ] ) + py;
            if ( px . lc == py . lc ) px . color -- ;
            return px . color + py . color;
        }
    }
}
signed main()
{
    freopen ( "text.in", "r" , stdin );
    cin >> n >> m;
    for ( int i = 1 ; i <= n ; i ++ )
        cin >> value [ i ] ;
    for ( int i = 1 ; i < n ; i ++ )
    {
        static int x , y ;
        cin >> x >> y;
        merge ( x , y );
    }
    pf :: dfs_1 ( 1 , 0 ) ;
    pf :: dfs_2 ( 1 , 1 );
    xds :: build ( 1 , 1 , n );
    for ( int i = 1 ; i <= m ; i++ )
    {
        char op ;
        int x , y , z;
        cin >> op >> x;
        if ( op == 'C' )
        {
            cin >> y >> z ;
            pf :: cz_1 ( x , y , z );
        }
        if ( op == 'Q' )
        {
            cin >> y;
            cout << pf :: cz_2 ( x , y ) << endl ;
        }
    }
    return 0;
}

标签:lc,SDOI2011,int,染色,rhs,color,res,P2486,px
From: https://www.cnblogs.com/dadidididi/p/16837518.html

相关文章

  • 银河之星(记忆化搜索+9点染色)
    Problem3银河之星(galaxy.cpp/c/pas)数据组数不超过10.这题就是记忆化搜索9点染色减少状态,map记忆化b[i][j][k]表示棋子可否从k方向到(i,j)#include<cstdio>#include<cstd......
  • [JSOI2015]染色问题
    P6076JSOI2015染色问题也是BZOJ4497容斥原理:将条件容斥第一步:先处理掉至少用一种颜色的:设f[i]表示用至多i种颜色,每行每列都染色的格子的方案数/答案为(-......
  • 基因组组装: 3D-DNA 染色体挂载
    导读本文将介绍基因组组装过程中,如何利用HiC测序数据,进行染色体级别基因组的组装。该过程主要利用Juicer和3D-DNA进行,有关第一步Juicer的过程,已经下方的文章中介绍了,......
  • 洛谷 P1506 拯救oibh总部(DFS / 染色法)
    https://www.luogu.com.cn/problem/P1506题目描述给定n*m的图形,由*和0组成。让我们求出被*四面包围了的0的数量。输入#1450000000*000*0*000*00输出......
  • Q3.1.1.4. 边的染色
    Q3.1.1.4.边的染色题目描述给一个允许有重边和自环的无向图,你需要将每条边染色成红色或蓝色,使得所有度数的点,都既与一条蓝色边相连,又与一条红色边相连。问是否有解,有......
  • P2491 [SDOI2011] 消防
    P2491SDOI2011消防算法竞赛进阶指南P374解法3(解法2为P1099树网的核),7FA4.3.2.5.3,LuoguP2491SDOI2011二分答案mid在树的直径上找离两端最远且距离小于mid......
  • 红绿正方形染色问题
    红绿正方形染色问题作者:Grey原文地址:博客园:红绿正方形染色问题CSDN:红绿正方形染色问题题目描述有一些排成一行的正方形。每个正方形已经被染成红色或者绿色。现在可......
  • 2019ACM-ICPC 西安邀请赛 D.Miku and Generals——二分图染色+01背包
    目录题意思路代码目录题意将n个将军卡片分成两份,要求两份卡片之间的差值尽可能小,求最大的那一份卡片的和,这里有m组卡片是不能放在同一份的思路对有矛盾的组我们建图进......
  • 染色法二分图
    #include<bits/stdc++.h>usingnamespacestd;constintN=1e5+10,M=1e5+10,INF=0x3f3f3f3f;intn,m,h[N],e[M<<1],ne[M<<1],idx,color[N];vo......
  • NC19996 [HAOI2015]树上染色
    题目链接题目题目描述有一棵点数为N的树,树边有边权。给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染成白色。将所有点染色后,你会......