题意

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

\(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. six

    团队序号:6组 团队名称:拯救地球小分队 团队项目的码云地址:https://gitee.com/lwj5950/seflash 此次博客撰写人姓名:刘威骏 学号:2017*****7168 团队中的 ...

  2. [转] HBase 深入浅出

    [From] https://www.ibm.com/developerworks/cn/analytics/library/ba-cn-bigdata-hbase/index.html HBase ...

  3. jenkins 重置构建历史

    item = Jenkins.instance.getItemByFullName("98")//THIS WILL REMOVE ALL BUILD HISTORYitem.bu ...

  4. genkins的报错排查

    [ERROR] /root/.jenkins/workspace/car/src/main/java/com/zhengxin/tool/code/Code.java:[20,64] diamond ...

  5. Unix_JDK安装及配置

    CentOS 下过程 JDK 在 CentOS 和 Ubuntu 下安装过程是一样的,所以这里不再讲 Ubuntu 系统下的安装 JDK 1.8 下载 此时(20170906)最新版本:jdk-8u1 ...

  6. mojing SDK根据坐标进行移动

    using UnityEngine; using UnityEngine.UI; using System.Collections; public class transforms : MonoBeh ...

  7. display:inline-block会出现的问题

    用一个父元素包裹三个子元素,将父元素的white-space设置为nowrap;这样子元素就会排在父元素中而不会换行了,通过这种方式,我们也就可以在移动端使用包裹元素的margin值实现类似的单页应用 ...

  8. Android 开发 命名规范(基础回顾)

    标识符命名法标识符命名法最要有四种: 1 驼峰(Camel)命名法:又称小驼峰命名法,除首单词外,其余所有单词的第一个字母大写. 2 帕斯卡(pascal)命名法:又称大驼峰命名法,所有单词的第一个字 ...

  9. 【随笔】win7下安装Apache服务器

    1.首先下载64位的apache服务器httpd-2.4.10-win64.zip 2.解压,可以是默认路径,也可以自定义路径,我这里是E:/ 3.打开E:/Apache24/conf文件夹 4.随便 ...

  10. [Unity3D]Shader编程之动态屏幕遮罩

    转载 https://blog.csdn.net/u012741077/article/details/78425834 屏幕可视范围跟随目标物体移动,可修改可视范围大小,边缘渐变大小.以及遮罩颜色, ...