bzoj3639: Query on a tree VII

时间:2021-11-24 00:08:16

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:

  • 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.
  • u : toggle the color of u(that is, from black to white, or from white to black).
  • 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
 
题解:
http://www.cnblogs.com/chenyushuo/p/5228875.html 这个相似,不过要用set维护通过虚边相连的点的最大权值,lct维护这条链上的最大值。
code:
 #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的更多相关文章

  1. BZOJ 3639&colon; Query on a tree VII

    Description 一棵树,支持三种操作,修改点权,修改颜色,问所有与他路径上颜色相同的点的最大权,包含这两个点. Sol LCT. 用LCT来维护重边,对于每个节点在建一个set用来维护轻边,这 ...

  2. 2019&period;02&period;17 spoj Query on a tree VII(链分治)

    传送门 跟QTREE6QTREE6QTREE6神似,改成了求连通块里的最大值. 于是我们对每条链开一个heapheapheap维护一下即可. MDMDMD终于1A1A1A链分治了. 代码: #incl ...

  3. SP16580 QTREE7 - Query on a tree VII

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

  4. BZOJ 3639&colon; Query on a tree VII LCT&lowbar;set维护子树信息

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

  5. &lbrack;spojQTREE7&rsqb;Query on a tree VII

    即QTREE5和QTREE6组合,即将原本维护子树范围内点数改为维护子树范围内最小值即可,由于最小值没有可减性,因此需要使用set (虽然形式上与QTREE5类似,但QTREE5维护的信息更巧妙一些, ...

  6. 洛谷SP16580 QTREE7 - Query on a tree VII(LCT,multiset)

    洛谷题目传送门 思路分析 维护子树最值还是第一次写QwQ 因为子树的最值会变化,所以不能简单地把最值记下来,还要维护一个平衡树,把每个子树的最大值扔进去,来资磁插入.删除和查询最值. 然后我就懒得手写 ...

  7. SP16580 QTREE7 - Query on a tree VII(LCT)

    题意翻译 一棵树,每个点初始有个点权和颜色(输入会给你) 0 u:询问所有u,v路径上的最大点权,要满足u,v路径上所有点颜色相同 1 u:反转u的颜色 2 u w:把u的点权改成w 题解 Qtree ...

  8. HDU 6191 Query on A Tree(字典树&plus;离线)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  9. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

随机推荐

  1. 最终解决 mouseenter&comma; mouseleave &comma; mouseout mousehover mousemove等事件的区别&quest;

    在jquery中, html页面的div的显示和隐藏, 修改等的功能, 最终都要由 事件 触发来引用, 不管是键盘事件, 还是鼠标事件... mouseenter和mouseleave是成对对应的, ...

  2. springmvc 用注解方式添加事务不生效解决方法

    springmvc 事务注册有很多种方法,在此我只mark 用注解方式添加transaction不生效的解决办法. springmvc 注解方法添加事务步骤: 1.在 spring的 root-con ...

  3. &lbrack;SqlServer&rsqb;创建链接服务器

    把一个数据库中数据表中的内容,从一个SQL SERVER服务器 导出到另一个SQL Server服务器 不同服务器数据库之间的数据操作 --创建链接服务器  exec sp_addlinkedserv ...

  4. Java Web的两种开发模式

    参考文献:http://www.cnblogs.com/xdp-gacl/p/3908610.html 一.Jsp+JavaBean 此模式如下图所示:

  5. Git学习记录

    一.简要说明 Git是分布式版本控制系统,而非集中式版本控制系统.其优势如下: *和开放源码 速度快,体积小 隐式备份(每台用户机上都有一个备份) 安全 不需要强大的硬件 更简单的分支 二.基本概念 ...

  6. c&num; 任意多个数,求最大值

    c#  任意多个数,求最大值 使用parms: 正在研究中,如果有好的方案,可评论,共同进步,共同提高,谢谢!

  7. Qt之移动硬盘热插拔监控

    最近在做一个通用对话框,类似于windows的资源管理器,当然了没有windwos资源管理器那么强大.用户报了一个bug,说通用对话框打开之后不能实时监控U盘插入,随手在百度上搜索了一圈,这个问题还是 ...

  8. LeetCode 49 Group Anagrams(字符串分组)

    题目链接: https://leetcode.com/problems/anagrams/?tab=Description   Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...

  9. XE4 for ios 谨慎处理字符串

    由于xe4 for ios  里面的字符串处理有变化,具体可以参考官方文档,这两天帮一个朋友调试ios 的 应用,由于没有注意这一块,折腾了很长时间.特此记录下来,希望其他人不要走弯路. 以下面代码为 ...

  10. DM816x算法具体解释--之OSD

    简单介绍: 本文介绍DM8168 DVRRDK中传入DSP内部的视频格式以及大概的处理流程. 背景: 可能有非常多人为了加快研发的速度.减少难度,选择在DVRRDk已有的OSD内加入自己的DSP算法. ...