SP16580 QTREE7 - Query on a tree VII

Description

一棵树,每个点初始有个点权和颜色(0/1) 0 u :询问所有u,v 路径上的最大点权,要满足u,v 路径上所有点的颜色都相同 1 u :反转u 的颜色 2 u w :把u 的点权改成w

Solution

对于每一种颜色,我们开一个 \(LCT\) 来维护
首先为了使得 \(LCT\) 维护的黑树连通,难免会有白点,但是最多只会有一个,因为一旦不连通了就没有必要维护了,对于白树也是同理

对于每一个 \(LCT\) 的节点,只需要维护一个 \(splay\) 中的子树 \(max\) 和虚子树的 \(max\) 就行了,和平时的 \(LCT\) 维护虚子树的方法相同,只需要把虚子树的答案当作这个点的权值就好了,唯一不同的是,这个题是维护 \(max\),不能支持加减操作,所以用一个 \(set\) 或 堆来维护就行了

一个细节:
对于一个连通块,可能根节点是不同色点,对于询问那么就需要加一个特判:
如果是根节点不同色的,答案就是右子树的答案
否则就是根节点的答案

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int pa[N],n,Q,head[N],nxt[N*2],to[N*2],num=0,c[N];
struct lxt{
    int fa[N],ch[N][2],v[N],w[N];
    multiset<int>S[N];
    inline bool isrt(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
    inline void upd(int x){
        v[x]=w[x];
        if(!S[x].empty())v[x]=max(v[x],*--S[x].end());
        if(ch[x][0])v[x]=max(v[x],v[ch[x][0]]);
        if(ch[x][1])v[x]=max(v[x],v[ch[x][1]]);
    }
    inline void rotate(int x){
        int y=fa[x];bool t=(ch[y][1]==x);
        ch[y][t]=ch[x][!t];
        fa[ch[y][t]]=y;
        ch[x][!t]=y;
        fa[x]=fa[y];
        if(!isrt(y))ch[fa[y]][ch[fa[y]][1]==y]=x;
        fa[y]=x;upd(y);upd(x);
    }
    inline void splay(int x){
        while(!isrt(x)){
            int y=fa[x],p=fa[y];
            if(isrt(y))rotate(x);
            else if((ch[p][0]==y)==(ch[y][0]==x))rotate(y),rotate(x);
            else rotate(x),rotate(x);
        }
    }
    inline void access(int x){
        int y=0;
        while(x){
            splay(x);
            if(ch[x][1])S[x].insert(v[ch[x][1]]);
            if(y)S[x].erase(S[x].find(v[y]));
            ch[x][1]=y;upd(x);x=fa[y=x];
        }
    }
    inline int query(int x){
        int co=c[x];
        access(x);splay(x);
        while(ch[x][0])x=ch[x][0];
        splay(x);
        return co==c[x]?v[x]:v[ch[x][1]];
    }
    inline void cut(int x){
        if(!pa[x])return ;
        access(x);splay(x);fa[ch[x][0]]=0;ch[x][0]=0;upd(x);
    }
    inline void link(int x){
        if(!pa[x])return ;
        access(pa[x]);splay(pa[x]);splay(x);
        ch[pa[x]][1]=x;fa[x]=pa[x];upd(pa[x]);
    }
}tr[2];
inline void link(int x,int y){nxt[++num]=head[x];to[num]=y;head[x]=num;}
inline void build(int x,int last){
    for(int u,i=head[x];i;i=nxt[i]){
        if((u=to[i])==last)continue;
        build(u,x);pa[u]=x;
        tr[c[u]].fa[u]=x;tr[c[u]].S[x].insert(tr[c[u]].v[u]);
    }tr[0].upd(x);tr[1].upd(x);
}
int main(){
    freopen("pp.in","r",stdin);
    freopen("pp.out","w",stdout);
    cin>>n;
    int x,y,op;
    for(int i=1;i<n;i++){
        scanf("%d%d",&x,&y);
        link(x,y);link(y,x);
    }
    for(int i=1;i<=n;i++)scanf("%d",&c[i]);
    for(int i=1;i<=n;i++)scanf("%d",&tr[0].w[i]),tr[1].w[i]=tr[0].w[i];
    build(1,0);
    cin>>Q;
    while(Q--){
        scanf("%d%d",&op,&x);
        if(op==0)printf("%d\n",tr[c[x]].query(x));
        else if(op==1){
            tr[c[x]].cut(x);tr[c[x]^1].link(x);
            c[x]^=1;
        }
        else {
            scanf("%d",&y);
            tr[0].access(x);tr[0].splay(x);
            tr[1].access(x);tr[1].splay(x);
            tr[0].w[x]=tr[1].w[x]=y;
            tr[0].upd(x);tr[1].upd(x);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Yuzao/p/8611421.html

时间: 2024-12-15 07:14:08

SP16580 QTREE7 - Query on a tree VII的相关文章

bzoj3639: Query on a tree VII

Description You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are numbered from 1 to n. Each node has a color, white or black, and a weight. We will ask you to perfrom some instructions of the following form: 0

BZOJ 3639: Query on a tree VII

Description 一棵树,支持三种操作,修改点权,修改颜色,问所有与他路径上颜色相同的点的最大权,包含这两个点. Sol LCT. 用LCT来维护重边,对于每个节点在建一个set用来维护轻边,这样Link和Cut是时候就非常好操作了,直接Access一下,Splay一下,直接删掉就可以了. 因为set是不统计重边的,然后对于每个节点的信息由他的父亲来保存,因为一个节点可能有很多儿子但一定只有一个父亲. 还有一个问题就是每个点的权值不能建全局的,因为维护的两颗LCT不能够同时删除,所以每个L

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

【百度之星2014~复赛)解题报告】The Query on the Tree

声明 笔者最近意外的发现 笔者的个人网站http://tiankonguse.com/ 的很多文章被其它网站转载,但是转载时未声明文章来源或参考自 http://tiankonguse.com/ 网站,因此,笔者添加此条声明. 郑重声明:这篇记录<[百度之星2014~复赛)解题报告]The Query on the Tree>转载自 http://tiankonguse.com/ 的这条记录:http://tiankonguse.com/record/record.php?id=673 前言

[hdu 6191] Query on A Tree

Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 733    Accepted Submission(s): 275 Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey

QTREE - Query on a tree

QTREE - Query on a tree 题目链接:http://www.spoj.com/problems/QTREE/ 参考博客:http://blog.sina.com.cn/s/blog_7a1746820100wp67.html 树链剖分入门题 代码如下(附注解): 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #define lson (x<<1) 5 #d

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

HDU 4836 The Query on the Tree lca || 欧拉序列 || 动态树

lca的做法还是很明显的,简单粗暴, 不过不是正解,如果树是长链就会跪,直接变成O(n).. 最后跑的也挺快,出题人还是挺阳光的.. 动态树的解法也是听别人说能ac的,估计就是放在splay上剖分一下,做法还是比较复杂的,,, 来一发lca: #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #inc

SPOJ QTREE 375. Query on a tree

SPOJ Problem Set (classical) 375. Query on a tree Problem code: QTREE 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: CHA