题意

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

\(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. 解决mysql最大允许传输包不足的问题

    一.报错提示内容和原因 在执行“数据传输”或者“运行SQL文件”时报错误:Got a packet bigger than 'max_allowed_packet' bytes With,表明当前所传 ...

  2. sap server笔记

    system 就是sap hana database,如果一个system有多个instance,则必须分散到不同的host中,每个system有唯一的sid. hello各位,jackie建议我们去 ...

  3. c++primer plus笔记

    > 第六版 操作符重载 #include<iostream> using namespace std; class Time { public: Time() { h=m=0; } ...

  4. FPGA基础学习(6) -- 原语

    目录 1. IBUF和IBUFDS(IO) 2. IDDR(Input/Output Functions) 3. IBUFG和IBUFGDS(IO) 原语,即primitive.不同的厂商,原语不同: ...

  5. java中的Lamdba表达式和Stream

    基于JDK 1.8 1.循环: // 以前的循环方式 for (String player : players) { System.out.print(player + "; ") ...

  6. 多个tomcat一起运行

    1.默认为:8005 2.在D:\apache-tomcat-7.0.85\bin目录下,找到startup.bat,打开并修改. 3.在D:\apache-tomcat-7.0.85\bin目录下, ...

  7. Angular material mat-icon 资源参考_Connection

    ul,li>ol { margin-bottom: 0 } dt { font-weight: 700 } dd { margin: 0 1.5em 1.5em } img { height: ...

  8. php尝试调用一个图灵机器人

    1.需要到图灵机器人的网址,去注册一下账号.网址:http://www.tuling123.com/sso-web/index.html?ReturnURL=http%3A%2F%2Fwww.tuli ...

  9. JavaMail 实现发送验证码,带验证码模板的

    package util; import java.io.File; import java.io.FileOutputStream; import java.util.Properties; imp ...

  10. Python操作Excel(将父子级表头生成树状结构)

    import re class Node: ''' 容器,用来存储前后节点信息 ''' __slot__=[] def __init__(self,val,next_,pre,name,no): se ...