原题链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3639

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 u : ask for the maximum weight among the nodes which are connected to u, two nodes are connected iff all the node on the path from u to v (inclusive u and v) have a same color.

1 u : toggle the color of u(that is, from black to white, or from white to black).

2 u w: change the weight of u to w.

Input

The first line contains a number n denoted how many nodes in the tree(1 ≤ n ≤ 105). The next n - 1 lines, each line has two numbers (u,  v) describe a edge of the tree(1 ≤ u,  v ≤ n).

The next 2 lines, each line contains n number, the first line is the initial color of each node(0 or 1), and the second line is the initial weight, let’s say Wi, of each node(|Wi| ≤ 109).

The next line contains a number m denoted how many operations we are going to process(1 ≤ m ≤ 105). The next m lines, each line describe a operation (t,  u) as we mentioned above(0 ≤ t ≤ 2, 1 ≤ u ≤ n, |w| ≤ 109).

Output

For each query operation, output the corresponding result.

Sample Input

5
1 2
1 3
1 4
1 5
0 1 1 1 1
1 2 3 4 5
3
0 1
1 1
0 1

Sample Output

1
5

题目大意

询问同色联通块内的最大值,支持修改颜色,修改权值。

题解

前置知识:维护联通块。

Qtree\mathcal{Qtree}Qtree系列,板子题一家。

维护联通块的方法见上面链接,剩下的就是维护一个子树的最大值了。此时,如果仅仅维护左右儿子的最大值,显然是不行的,Splay\mathcal{Splay}Splay时也没法继续维护。所以,我们需要给每个节点开一个multiset\mathcal{multiset}multiset,将所有虚子树的最大值都丢进去,就可以做到O(log2n)O(log_2n)O(log2​n)更新了。

其他维护大同小异。

借此学习一波STL\mathcal{STL}STL姿势。

代码
#include<bits/stdc++.h>
#define ls son[v][0]
#define rs son[v][1]
#define inf INT_MAX
#define C lct[col[a]]
using namespace std;
const int M=1e5+5,N=M<<1;
int col[M],fa[M],val[M],n,m;
vector<int>mmp[M];
struct LCT{int son[N][2],dad[N],mx[N];multiset<int>s[N];LCT(){mx[0]=-inf;}bool notroot(int v){return son[dad[v]][0]==v||son[dad[v]][1]==v;}void up(int v){mx[v]=max(val[v],max(mx[ls],mx[rs]));if(!s[v].empty())mx[v]=max(mx[v],*s[v].rbegin());}void spin(int v){int f=dad[v],ff=dad[f],k=son[f][1]==v,w=son[v][!k];if(notroot(f))son[ff][son[ff][1]==f]=v;son[v][!k]=f;son[f][k]=w;if(w)dad[w]=f;dad[f]=v;dad[v]=ff;up(f);}void splay(int v){int f,ff;while(notroot(v)){f=dad[v];ff=dad[f];if(notroot(f))spin((son[f][0]==v)^(son[ff][0]==f)?v:f);spin(v);}up(v);}void access(int v){for(int f=0;v;v=dad[f=v]){splay(v);if(rs)s[v].insert(mx[rs]);if(rs=f)s[v].erase(s[v].find(mx[f]));up(v);}}int root(int v){access(v);splay(v);while(ls)v=ls;splay(v);return v;}void link(int v){splay(v);int f=dad[v]=fa[v];access(f);splay(f);son[f][1]=v;up(f);}void cut(int v){access(v);splay(v);ls=dad[ls]=0;up(v);}
}lct[2];
void dfs(int v,int f)
{fa[v]=f;int to;for(int i=mmp[v].size()-1;i>=0;--i){to=mmp[v][i];if(to==f)continue;dfs(to,v);}lct[col[v]].link(v);
}
void in()
{int a,b;scanf("%d",&n);for(int i=1;i<n;++i)scanf("%d%d",&a,&b),mmp[a].push_back(b),mmp[b].push_back(a);for(int i=1;i<=n;++i)scanf("%d",&col[i]);for(int i=1;i<=n;++i)scanf("%d",&val[i]);
}
void ac()
{int op,a;dfs(1,n+1);scanf("%d",&m);for(int i=1;i<=m;++i){scanf("%d%d",&op,&a);switch(op){case 0:printf("%d\n",C.mx[C.son[C.root(a)][1]]);break;case 1:C.cut(a);col[a]^=1;C.link(a);break;case 2:C.access(a);C.splay(a);scanf("%d",&val[a]);C.up(a);}}
}
int main(){in();ac();}

BZOJ3639 Query on a tree VII相关推荐

  1. BZOJ 3639: Query on a tree VII LCT_set维护子树信息

    用 set 维护子树信息,细节较多. Code: #include <cstring> #include <cstdio> #include <algorithm> ...

  2. SPOJ 375. Query on a tree (树链剖分)

    题目链接: http://www.spoj.com/problems/QTREE/ 375. Query on a tree Problem code: QTREE You are given a t ...

  3. XXI Open Cup. Grand Prix of Korea I. Query On A Tree 17 树剖 + 二分 + 树带权重心

    传送门 文章目录 题意: 思路: 题意: 给你一棵树,每棵树初始权值都为000,现在给你两个操作: (1)(1)(1)将uuu的子树权值全部加111. (2)(2)(2)将(u,v)(u,v)(u,v ...

  4. [BZOJ1095][ZJOI2007]捉迷藏 Query on a tree IV(树链剖分)

    首先,我们求出树的重链,然后对于每一条链,建一颗线段树 树大概长这样: (其中用红边连起来的是一条条重链) 在线段树上,我们维护: Opt(u):经过 u节点代表的链的其中一段 的两个白点间的最长路径 ...

  5. LCA SP913 QTREE2 - Query on a tree II

    SP913 QTREE2 - Query on a tree II 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点 ...

  6. hdu 4836 The Query on the Tree(线段树or树状数组)

    The Query on the Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. Query on a tree(LCT版)

    文章目录 题目 分析 代码 题目 Query on a tree 分析 动态树(LCT)初探 代码 #include<cstdio> #include<vector> #inc ...

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

    题目链接: http://www.spoj.com/problems/QTREE/ 题意: 给一颗树,每条边有一个权值.有两种操作: 1.修改某条边的值: 2.询问a.b两点路径上边权的最大值. 分析 ...

  9. HDU - 3804 Query on a tree(主席树维护最大值+离散化)

    题目链接:点击查看 题目大意:给出一棵树,每条边上都有一个权值,给出m个查询:a,b:问从点1到点a的唯一路径上,在边权小于等于b的边中选出边权最大的值输出,若没有符合条件的边则输出-1: 题目分析: ...

  10. SPOJ - QTREE Query on a tree(树链剖分+线段树)

    题目链接:点击查看 题目大意:给出一棵由n个点组成的树,再给出数个操作,每次操作分为下列几种类型: QUERY x y:询问点x-点y这条路径上的所有边权的最大值 CHANGE x y:将第x条边的权 ...

最新文章

  1. Flash气泡回弹效果
  2. 举重若轻的人人车移动端数据平台
  3. android标尺自定义view,android尺子的自定义view——RulerView详解
  4. 通过SQL Server操作MySQL的步骤和方法
  5. php网站开发期末大作业,大学生期末网页大作业
  6. java声明和初始化数组_Java 中初始化数组
  7. mysql添加临时索引_mysql 中添加索引的三种方法
  8. pip/pip3 install 报错 “Could not find a version that satisfies the requriement xxx” 的解决方法
  9. Android学习总汇
  10. Tokyo Tyrant (ttserver)的master-slave复制协议分析
  11. 实验:基于keepalived实现两台realserver服务器中的nginx和php-fpm服务互为主从
  12. LeetCode问题7
  13. 基于Java的高校社团管理系统
  14. java万能万年历的程序_Java万年历
  15. 二进制文件和文本文件的区别
  16. oracle的安装(因为oracle版本安装各有差异,今天先讲最简单的安装,至于每个版本的具体安装度妈会讲,下一章同样会介绍可视化工具)
  17. ES查询中.keyword详解
  18. 为什么默认排除 junit-vintage-engine ?
  19. MVC框架的简单描述
  20. 暑假实习证明格式范文

热门文章

  1. 简单的notepad将\n转化为换行
  2. 【题解】Luogu P5294 [HNOI2019]序列
  3. 拦截器(Interceptor)和过滤器(Filter)区别
  4. 【BZOJ3894】文理分科(最小割)
  5. luvcview-0.2.4移植
  6. java架构《并发线程中级篇》
  7. mysql从一个表中拷贝数据到另一个表中sql语句
  8. android 屏幕宽高
  9. 微软面试题 麻将胡牌算法
  10. 常见并发工具的使用和原理解析——Condition(重点在第五节)