题意

一棵树,每个点初始有个点权和颜色

\(0 \ u\) :询问所有\(u,v\) 路径上的最大点权,要满足\(u,v\) 路径上所有点的颜色都相同

$1 \ u \(:反转\)u$ 的颜色

\(2 \ u \ w\) :把\(u\) 的点权改成\(w\)

\(color_i∈[0,1],w_i∈[−10^9,10^9],n,m≤10^5\)

Sol

\(LCT\)

和\(QTREE6\)一样,黑白两棵\(LCT\)

不过这次我们用数据结构维护虚子树内的最大权的同色点

可以用\(multiset\),但我还是习惯可删除的\(priority\_queue\)

然后每个点维护一下所有子树的最大权的同色点

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 5);
const int INF(2e9);
typedef int Arr[_]; IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} Arr w;
struct Heap{
priority_queue <int> A, B; IL void Push(RG int x){
A.push(x);
} IL void Del(RG int x){
B.push(x);
} IL int Top(){
while(!B.empty() && B.top() == A.top()) A.pop(), B.pop();
return A.empty() ? -INF : A.top();
}
}; struct LCT{
Arr fa, ch[2], mxv;
Heap mx[_]; IL int Son(RG int x){
return ch[1][fa[x]] == x;
} IL int Isroot(RG int x){
return ch[0][fa[x]] != x && ch[1][fa[x]] != x;
} IL void Update(RG int x){
mxv[x] = max(max(mxv[ch[0][x]], mxv[ch[1][x]]), max(w[x], mx[x].Top()));
} IL void Rotate(RG int x){
RG int y = fa[x], z = fa[y], c = Son(x);
if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z;
ch[c][y] = ch[!c][x], fa[ch[c][y]] = y;
ch[!c][x] = y, fa[y] = x, Update(y);
} IL void Splay(RG int x){
for(RG int y = fa[x]; !Isroot(x); Rotate(x), y = fa[x])
if(!Isroot(y)) Son(x) ^ Son(y) ? Rotate(x) : Rotate(y);
Update(x);
} IL void Access(RG int x){
for(RG int y = 0; x; y = x, x = fa[x]){
Splay(x);
mx[x].Push(mxv[ch[1][x]]), mx[x].Del(mxv[y]);
ch[1][x] = y, Update(x);
}
} IL int Findroot(RG int x){
Access(x), Splay(x);
while(ch[0][x]) x = ch[0][x];
Splay(x);
return x;
} IL void Link(RG int x, RG int y){
if(!y) return;
Access(y), Splay(x), Splay(y);
fa[x] = y, ch[1][y] = x, Update(y);
} IL void Cut(RG int x, RG int y){
if(!y) return;
Access(x), Splay(x);
ch[0][x] = fa[ch[0][x]] = 0, Update(x);
}
} T[2];
Arr fa, col;
int n, m;
vector <int> G[_]; IL void Dfs(RG int u, RG int ff){
for(RG int i = 0, l = G[u].size(); i < l; ++i){
RG int v = G[u][i];
if(v == ff) continue;
T[col[v]].Link(v, u), fa[v] = u;
Dfs(v, u);
}
} int main(RG int argc, RG char *argv[]){
n = Input();
for(RG int i = 1; i < n; ++i){
RG int u = Input(), v = Input();
G[u].push_back(v), G[v].push_back(u);
}
for(RG int i = 1; i <= n; ++i) col[i] = Input();
for(RG int i = 1; i <= n; ++i) w[i] = Input();
T[0].mxv[0] = T[1].mxv[0] = -INF;
Dfs(1, 0), m = Input();
for(RG int i = 1; i <= m; ++i){
RG int op = Input(), x = Input(), ff, v, &c = col[x];
if(op == 1) T[c].Cut(x, fa[x]), c ^= 1, T[c].Link(x, fa[x]);
else if(op == 2){
v = Input(), T[c].Access(x), T[c].Splay(x);
w[x] = v, T[c].Update(x);
}
else{
T[c].Access(x), ff = T[c].Findroot(x);
if(col[ff] == c) printf("%d\n", T[c].mxv[ff]);
else printf("%d\n", T[c].mxv[T[c].ch[1][ff]]);
}
}
return 0;
}

SPOJ QTREE7的更多相关文章

  1. 【SPOJ】QTREE7(Link-Cut Tree)

    [SPOJ]QTREE7(Link-Cut Tree) 题面 洛谷 Vjudge 题解 和QTREE6的本质是一样的:维护同色联通块 那么,QTREE6同理,对于两种颜色分别维护一棵\(LCT\) 每 ...

  2. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  3. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  4. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  5. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

  6. SPOJ bsubstr

    题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...

  7. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...

  8. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  9. 【SPOJ 8222】Substrings

    http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...

随机推荐

  1. dbproxy-main函数

    main主函数 /home/id/lua.lua/home/id/lua-c/lua.lua/home/id/lua-c/metatable.lua/home/id/lua-c/1.lua int m ...

  2. HTML5本地存储——IndexedDB二:索引

    HTML5本地存储——IndexedDB(二:索引)   在HTML5本地存储——IndexedDB(一:基本使用)中介绍了关于IndexedDB的基本使用方法,很不过瘾,这篇我们来看看indexed ...

  3. [Java]去除html中的标签或者元素属性(正则表达式)

    后台的数据库中某个字段是富文本框输入的 带有Html的标签 ,去掉标签后返回给前台 1.去掉Html 标签的代码 //过滤html标签 Pattern p_html = Pattern.compile ...

  4. JavaWeb学习笔记(十八)—— DBUtils的使用

    一.DBUtils概述 1.1 什么是DBUtils commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbuti ...

  5. POJ_2886 Who Gets the Most Candies? 【二分+树状数组】

    一.题目 POJ2886 二.分析 这个题目吧,开始没读懂,做的时候也没懂,WA的时候懂了.假设是第p个出圈的人有一个对应的因子个数$F(p)$,那么,题目求的就是这个$F(p)$最大的对应的人. 1 ...

  6. 解决Maven本地仓库没有Jar包问题,请求中央仓库自动下载以及手动下载方法

    一.首先指定本地仓库 <localRepository>D:\software\Maven_Home\mvn_repository</localRepository> 二.修改 ...

  7. Linux -定时任务调度

    l crond 任务调度   crontab 进行定时任务的设置,. 概述 任务调度:是指系统在某个时间执行的特定的命令或程序. 任务调度分类:1.系统工作:有些重要的工作必须周而复始地执行.如病毒扫 ...

  8. jconsole 和jvisualVM 监控远程 spring boot程序

    监控java 程序 增加启动参数 java  \ -Djava.rmi.server.hostname=192.168.2.39 \ -Dcom.sun.management.jmxremote \- ...

  9. Pitfalls of using opencv GpuMat data in CUDA kernel code

    Please note that cv::cuda::GpuMat and cv::Mat using different memory allocation method. cv::cuda::Gp ...

  10. mysql Access denied for user root@localhost错误解决方法总结

    原文:http://www.111cn.net/database/mysql/44142.htm Access denied for user 'root'@'localhost' (using pa ...