题意

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

\(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. CocoaPods的PodSpec.json文件用法

    最近有时候用最新的CocoaPod的第三方库,有时候发现CocoaPod.org能搜到那个Podfile,但是每次在终端Pod search xxx,每次都搜不到,原来是本地的Podspec没用更新, ...

  2. Ant demo

    <?xml version="1.0" encoding="UTF-8"?> <!-- 定义一个工程,默认任务为warFile. --> ...

  3. 2019 CCPC-Wannafly Winter Camp Day5(Div2, onsite)

    solve 5/11 补题:7/11 A Cactus Draw Code:zz Thinking :zz 题意:要在n*n的网格内画上一棵节点数为n树,使得没有边相交. 很好想的构造题,因为网格有n ...

  4. R 拆分EXCEL成多个文件

    setwd("C:/Rworkfile") install.packages("readxl") library(readxl) www<-read_ex ...

  5. android中Zing二维码扫描,二维码生成

    Android中二维码扫描的最常用库是zxing和zbar,zxing项目地址为https://github.com/zxing/zxing,目前还有多个人在维护.zbar主要用C来写的,对速度有要求 ...

  6. js验证码有效时间倒计时

    js验证码有效时间倒计时 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type& ...

  7. Axure原型设计介绍

    在第八周的课堂上,王文娟老师在校园系统上发布了对于自行选择的原型设计软件进行资料查找以及自学的任务.因为之前的课程学习需要,我们大概掌握了原型设计软件Axure的使用,下面是一些我们学习过程中的介绍 ...

  8. 《Crafting Rails 4 Applications》的笔记-第28页

    进行邮件测试,你需要在dummy目录下的虚拟程序添加一个配置 In your config/enviroments/test.rb, by default you should have the li ...

  9. SQL Cookbook—数字、日期

    1.计算不包含最大值和最小值的均值2.把字母数字串转换为数值3.更改累计和中的值–显示存款或取款后的值4.加减日.月.年5.计算两个日期之间的天数6.确定两个日期之间的工作日数目表EMP中,计算BLA ...

  10. linux系统修改route路由

    linux下静态路由修改命令方法一:添加路由route add -net 192.168.0.0/24 gw 192.168.0.1route add -host 192.168.1.1 dev 19 ...