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 ...
随机推荐
- IntelliJ IDEA中JAVA连接MYSQL
1.下载mysql包 2.项目中引入mysql包 3.连接数据库,查询结果 看jdbc数据库连接类 package Facade; import java.sql.*; /** * Created b ...
- Objective-c 中如何重写父类的初始化方法
在我们的日常开发中我们经常会定义一些自己的子类继承一些UIKit 库中的类,那我们应该如何重写的这些初化方法呢?那我们先看看这些类有哪些初初化方法吧.(这里就用UIView为例) - (id)init ...
- ES索引模板——就是在新建索引时候指定的正则匹配来设置mapping而已,对于自动扩容有用
索引模板 扩容设计 » 索引模板 Elasticsearch 不要求你在使用一个索引前创建它. 对于日志记录类应用,依赖于自动创建索引比手动创建要更加方便. Logstash 使用事件中的时间戳来生成 ...
- [jzoj 6087] [GDOI2019模拟2019.3.26] 获取名额 解题报告 (泰勒展开+RMQ+精度)
题目链接: https://jzoj.net/senior/#main/show/6087 题目: 题解: 只需要统计$\prod_{i=l}^r (1-\frac{a_i}{x})$ =$exp(\ ...
- HDU 4609 FFT+各种分类讨论
思路: http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html 其实我是懒得写了.... 一定要define int long ...
- Kettle和ETL的基本构成
不多说,直接上干货! 这里,我说的通俗易懂点,好方便大家的理解. ETL解决方案就像业务流程一样,具有输入.输出,以及一个或多个工作环节,处理步骤.同样的,这些步骤也具有输入和输出,并可以执行将一个输 ...
- 线上服务CPU100%问题快速定位实战--转
来自微信公众号 架构师之路 功能问题,通过日志,单步调试相对比较好定位. 性能问题,例如线上服务器CPU100%,如何找到相关服务,如何定位问题代码,更考验技术人的功底. 58到家架构部,运维部,58 ...
- Bringing up the Avnet MicroZed with Vivado
Bringing up the Avnet MicroZed with Vivado I recently received the Adam Taylor Edition of Avnet's Zy ...
- 获取鼠标经过处的标签的标签名和id
<script> var el = window.document.body; // 声明一个变量,默认值为body window.document.body.onmouseover = ...
- 紫书 习题 8-15 UVa 1617 (贪心)
先排序, 然后每个线段先放右端点, 然后往下放, 如果不能放就整体往左移动, 当不能往左移动的时候就ans++ 开始下一个整块.判断能不能向左移动要用一个变量储存每个已经放了的区间中线段与左端点距离的 ...