bzoj3639: Query on a tree VII
Description
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
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
5
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
char ch;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
const int maxn=;
const int maxm=;
int n,q,op,a,b,v,tot,now[maxn],son[maxm],pre[maxm],fa[maxn],col[maxn];
struct lct{
int id,fa[maxn],son[maxn][],val[maxn],maxv[maxn];
multiset<int> rest[maxn];
int which(int x){return son[fa[x]][]==x;}
bool isroot(int x){return son[fa[x]][]!=x&&son[fa[x]][]!=x;}
void update(int x){
maxv[x]=val[x];
if (!rest[x].empty()) maxv[x]=max(maxv[x],*rest[x].rbegin());
if (son[x][]) maxv[x]=max(maxv[x],maxv[son[x][]]);
if (son[x][]) maxv[x]=max(maxv[x],maxv[son[x][]]);
}
void rotate(int x){
int y=fa[x],z=fa[y],d=which(x),dd=which(y);
fa[son[x][d^]]=y,son[y][d]=son[x][d^],fa[x]=fa[y];
if (!isroot(y)) son[z][dd]=x;
fa[y]=x,son[x][d^]=y,update(y),update(x);
}
void splay(int x){
while (!isroot(x)){
if (isroot(fa[x])) rotate(x);
else if (which(x)==which(fa[x])) rotate(fa[x]),rotate(x);
else rotate(x),rotate(x);
}
}
void access(int x){
for (int p=;x;x=fa[x]){
splay(x);
if (son[x][]) rest[x].insert(maxv[son[x][]]);
if (p) rest[x].erase(rest[x].find(maxv[p]));
son[x][]=p,update(p=x);
}
}
void link(int x,int y){
if (!y) return;
access(y),splay(y),splay(x),fa[x]=y,son[y][]=x,update(y);
}
void cut(int x,int y){
if (!y) return;
access(x),splay(x),fa[son[x][]]=,son[x][]=,update(x);
}
int find_root(int x){for (access(x),splay(x);son[x][];x=son[x][]); return x;}
void query(int x){
int t=find_root(x); splay(t);
printf("%d\n",col[t]==id?maxv[t]:maxv[son[t][]]);
}
void modify(int x,int v){access(x),splay(x),val[x]=v,update(x);}
}T[];
void put(int a,int b){pre[++tot]=now[a],now[a]=tot,son[tot]=b;}
void add(int a,int b){put(a,b),put(b,a);}
void dfs(int u){
for (int p=now[u],v=son[p];p;p=pre[p],v=son[p])
if (v!=fa[u]) fa[v]=u,T[col[v]].link(v,u),dfs(v);
}
int main(){
read(n),T[].id=,T[].id=;
for (int i=;i<n;i++) read(a),read(b),add(a,b);
for (int i=;i<=n;i++) read(col[i]);
for (int i=;i<=n;i++) read(T[].val[i]),T[].maxv[i]=T[].val[i];
for (int i=;i<=n;i++) T[].maxv[i]=T[].val[i]=T[].val[i];
dfs();
for (read(q);q;q--){
read(op),read(a);
if (op==) T[col[a]].query(a);
else if (op==) T[col[a]].cut(a,fa[a]),col[a]^=,T[col[a]].link(a,fa[a]);
else read(v),T[].modify(a,v),T[].modify(a,v);
}
return ;
}
bzoj3639: Query on a tree VII的更多相关文章
- BZOJ 3639: Query on a tree VII
Description 一棵树,支持三种操作,修改点权,修改颜色,问所有与他路径上颜色相同的点的最大权,包含这两个点. Sol LCT. 用LCT来维护重边,对于每个节点在建一个set用来维护轻边,这 ...
- 2019.02.17 spoj Query on a tree VII(链分治)
传送门 跟QTREE6QTREE6QTREE6神似,改成了求连通块里的最大值. 于是我们对每条链开一个heapheapheap维护一下即可. MDMDMD终于1A1A1A链分治了. 代码: #incl ...
- SP16580 QTREE7 - Query on a tree VII
Description 一棵树,每个点初始有个点权和颜色(0/1) 0 u :询问所有u,v 路径上的最大点权,要满足u,v 路径上所有点的颜色都相同 1 u :反转u 的颜色 2 u w :把u 的 ...
- BZOJ 3639: Query on a tree VII LCT_set维护子树信息
用 set 维护子树信息,细节较多. Code: #include <cstring> #include <cstdio> #include <algorithm> ...
- [spojQTREE7]Query on a tree VII
即QTREE5和QTREE6组合,即将原本维护子树范围内点数改为维护子树范围内最小值即可,由于最小值没有可减性,因此需要使用set (虽然形式上与QTREE5类似,但QTREE5维护的信息更巧妙一些, ...
- 洛谷SP16580 QTREE7 - Query on a tree VII(LCT,multiset)
洛谷题目传送门 思路分析 维护子树最值还是第一次写QwQ 因为子树的最值会变化,所以不能简单地把最值记下来,还要维护一个平衡树,把每个子树的最大值扔进去,来资磁插入.删除和查询最值. 然后我就懒得手写 ...
- SP16580 QTREE7 - Query on a tree VII(LCT)
题意翻译 一棵树,每个点初始有个点权和颜色(输入会给你) 0 u:询问所有u,v路径上的最大点权,要满足u,v路径上所有点颜色相同 1 u:反转u的颜色 2 u w:把u的点权改成w 题解 Qtree ...
- HDU 6191 Query on A Tree(字典树+离线)
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
随机推荐
- Win7 64位安装MySQL
1.Win7 64位 安装MySQL5.5版本 安装文件的执行:会提示“已经停止工作”: 2.我下载了mysql-installer-community-5.7.11.0.msi,可以安装成功,中途需 ...
- Android 判断数据库中是否存在某个表
public boolean tabIsExist(String tabName){ boolean result = false; if(tabName == null){ return false ...
- android 16 带返回值的activity
main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...
- limit-进程句柄限制
在Linux下面部署应用的时候,有时候会遇上Socket/File: Can’t open so many files的问题,比如还有Squid做代理,当文件打开数到900多时速能就非常快的下降,有可 ...
- gdb - 列出所有函数调用
How can we list all the functions being called in an application For any realistically sized applica ...
- Java语言基础(二)
Java语言基础(二) 一.变量续 (1).变量有明确的类型 (2).变量必须有声明,初始化以后才能使用 (3).变量有作用域,离开作用域后自动回收 变量作用域在块内有效 (4).在同一定义域中变量不 ...
- collectionViewFlow的界面编写
#import <UIKit/UIKit.h> //这边我们会创建一个scrollView的界面,这个scrollView里面有三张图片构成,我们使用下面的枚举方式来定义这三个位置 typ ...
- JS的事件监听机制
很久以前有个叫Netscape的姑娘,她制订了Javascript的一套事件驱动机制(即事件捕获) 后来又有一个叫“IE”的小子,这孩子比较傲气,他认为“凭什么我要依照你的规则走”,于是他又创造了一套 ...
- 原生JS添加节点方法与jQuery添加节点方法的比较及总结
一.首先构建一个简单布局,来供下边讲解使用 1.HTML部分代码: <div id="div1">div1</div> <div id="d ...
- Adb工具常用操作-转(二)
一. PC与模拟器或真机交换文件(adb pull和adb push) 在开发阶段或其他原因,经常需要将PC上的文件复制到模拟器或真机上,或将模拟机和真机上的文件复制到PC上.使用adb pull和a ...