BZOJ 3639: Query on a tree VII LCT_set维护子树信息
用 set 维护子树信息,细节较多。
Code:
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#define setIO(s) freopen(s".in","r",stdin), freopen(s".out","w",stdout)
#define maxn 100009
#define inf -2147483647
using namespace std;
int fat[maxn];
int n,m;
struct Graph{
int head[maxn],to[maxn<<1],nex[maxn<<1],cnt;
void addedge(int u,int v){ nex[++cnt]=head[u],head[u]=cnt,to[cnt]=v; }
void dfs(int u,int father){
fat[u]=father;
for(int v=head[u];v;v=nex[v]) if(to[v]!=father) dfs(to[v],u);
}
void Build(){
for(int i=1;i<n;++i){
int a,b;
scanf("%d%d",&a,&b), addedge(a,b),addedge(b,a);
}
dfs(1,0);
}
}G;
int col[maxn],v[maxn];
struct LCT{
multiset<int>s[maxn];
multiset<int>::reverse_iterator rit;
int ch[maxn][2],f[maxn];
int siz[maxn],mx[maxn];
void init(){ mx[0]=-2147483647; }
int value(int x) { rit=s[x].rbegin(); return (*rit); }
int get(int x){ return ch[f[x]][1]==x; }
int lson(int x){ return ch[x][0];}
int rson(int x){ return ch[x][1];}
int isRoot(int x){ return !(ch[f[x]][0]==x||ch[f[x]][1]==x); }
void pushup(int x){
if(!x) return;
mx[x]=max(v[x],max(mx[lson(x)],mx[rson(x)]));
if(!s[x].empty()) mx[x]=max(mx[x],value(x));
}
void rotate(int x)
{
int old=f[x],oldf=f[old],which=get(x);
if(!isRoot(old)) ch[oldf][ch[oldf][1]==old]=x;
ch[old][which]=ch[x][which^1],f[ch[old][which]]=old;
ch[x][which^1]=old,f[old]=x,f[x]=oldf;
pushup(old),pushup(x);
}
void splay(int x)
{
int u=x;
while(!isRoot(u)) u=f[u];
u=f[u];
for(int fa;(fa=f[x])!=u;rotate(x))
if(f[fa]!=u) rotate(get(fa)==get(x)?fa:x);
}
void Access(int x){
for(int y=0;x;y=x,x=f[x]){
splay(x);
if(rson(x)) s[x].insert(mx[rson(x)]);
if(ch[x][1]=y) s[x].erase(s[x].find(mx[y])) ;
pushup(x);
}
}
void cut(int a,int b){
if(!a) return;
Access(b),splay(b),ch[b][0]=f[ch[b][0]]=0,pushup(b);
}
void link(int a,int b)
{
if(!a) return;
Access(a),splay(a),Access(b),splay(b);
f[b]=a,ch[a][1]=b,pushup(a);
}
int findRoot(int a){
Access(a),splay(a);
while(ch[a][0]) a=ch[a][0];
return a;
}
}tree[2];
int main(){
//setIO("debug");
scanf("%d",&n),G.Build(),tree[0].init(),tree[1].init();
for(int i=1;i<=n;++i) scanf("%d",&col[i]);
for(int i=1;i<=n;++i) scanf("%d",&v[i]);
fat[1]=n+1,v[n+1]=tree[1].mx[n+1]=tree[0].mx[n+1]=inf;
for(int i=1;i<=n;++i) tree[col[i]].link(fat[i],i);
int opt,a;
scanf("%d",&m);
for(int i=1;i<=m;++i){
scanf("%d%d",&opt,&a);
if(opt==0) {
int x=tree[col[a]].findRoot(a);
tree[col[a]].splay(x);
if(col[x]==col[a]) printf("%d\n",tree[col[a]].mx[x]);
else printf("%d\n",tree[col[a]].mx[tree[col[a]].ch[x][1]]);
}
if(opt==1) {
tree[col[a]].cut(fat[a],a),col[a]^=1,tree[col[a]].link(fat[a],a);
}
if(opt==2)
{
tree[col[a]].Access(a),tree[col[a]].splay(a);
scanf("%d",&v[a]);
tree[col[a]].pushup(a);
}
}
return 0;
}
BZOJ 3639: Query on a tree VII LCT_set维护子树信息的更多相关文章
- BZOJ 3637: Query on a tree VI LCT_维护子树信息_点权转边权_好题
非常喜欢这道题. 点权转边权,这样每次在切断一个点的所有儿子的时候只断掉一条边即可. Code: #include <cstring> #include <cstdio> #i ...
- BZOJ 3639: Query on a tree VII
Description 一棵树,支持三种操作,修改点权,修改颜色,问所有与他路径上颜色相同的点的最大权,包含这两个点. Sol LCT. 用LCT来维护重边,对于每个节点在建一个set用来维护轻边,这 ...
- bzoj 3083 遥远的国度——树链剖分+线段树维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3083 int 的范围是 2^31 - 1 ,所以权值是不是爆 int 了…… O( nlog ...
- BZOJ 3306: 树 LCT + set 维护子树信息
可以作为 LCT 维护子树信息的模板,写的还是比较优美的. 本地可过,bzoj 时限太紧,一直 TLE #include<bits/stdc++.h> #define setIO(s) f ...
- bzoj 3637: Query on a tree VI 树链剖分 && AC600
3637: Query on a tree VI Time Limit: 8 Sec Memory Limit: 1024 MBSubmit: 206 Solved: 38[Submit][Sta ...
- [spojQTREE7]Query on a tree VII
即QTREE5和QTREE6组合,即将原本维护子树范围内点数改为维护子树范围内最小值即可,由于最小值没有可减性,因此需要使用set (虽然形式上与QTREE5类似,但QTREE5维护的信息更巧妙一些, ...
- SP16549 QTREE6 - Query on a tree VI LCT维护颜色联通块
\(\color{#0066ff}{ 题目描述 }\) 给你一棵n个点的树,编号1~n.每个点可以是黑色,可以是白色.初始时所有点都是黑色.下面有两种操作请你操作给我们看: 0 u:询问有多少个节点v ...
- bzoj 4530 [Bjoi2014]大融合——LCT维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4530 LCT维护子树 siz .设 sm[ ] 表示轻儿子的 siz 和+1(1是自己的si ...
- bzoj 3779 重组病毒——LCT维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3779 调了很久……已经懒得写题解了.https://www.cnblogs.com/Zinn ...
随机推荐
- [jzoj NOIP2018模拟11.02]
嗯T1忘记取模了,100到20 嗯T2忘记了那啥定理,暴力也写炸了,这题我认 嗯T3线段树合并分裂没有写炸,考场上就知道妥妥的70分.但是,分数出的时候听到有人说暴力也是70分,我???脸黑,枉我敲了 ...
- 整数转罗马数字 C++实现 java实现 leetcode系列(十二)
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- Elasticsearch之四种查询类型和搜索原理(博主推荐)
Elasticsearch Client发送搜索请求,某个索引库,一般默认是5个分片(shard). 它返回的时候,由各个分片汇总结果回来. 官网API https://www.elastic.co/ ...
- jqGrid收藏的链接
http://zld406504302.iteye.com/blog/1694017 http://blog.csdn.net/jiudihanbing/article/details/2455902 ...
- 【原创】rman 全库备份脚本
rman 全库备份脚本 run { allocate channel d1 type disk; allocate channel d2 type disk; backup full database ...
- NetworkX-画图
参考:https://blog.csdn.net/qq951127336/article/details/54586869 1.创建图 networkx有四种图 Graph .DiGraph.Mult ...
- 妙用$.extend
在js中,我们有时需要复制一个对象的值,而不是复制它的引用的时候,可以使用jquery的$.extend方法,简单代码如下 <script> var a = { "name&qu ...
- FCC编程题之中级算法篇(上)
介绍 FCC: 全称为freeCodeCamp,是一个非盈利性的.面向全世界的编程练习网站.这次的算法题来源于FCC的中级算法题. FCC中级算法篇共分为(上).(中).(下)三篇.每篇各介绍7道算法 ...
- vue非空校验
效果图 实现代码 //页面html <div> <ul class="listinfo"> <li> <span class=" ...
- HitHub使用
GitHub是个免费的开源仓库,个人及组织可以将源代码上传,既可以让别人参与到自己的项目中,也可以学习与参与到他人的项目中去.免费的GitHub账号的代码仓库(repository)都是公开的,任何人 ...