题意:给你一棵树,边带权,支持两种操作:修改某条边的权值;查询两点之间的最短路。

lct主要实现单点修改和路径和。

修改x结点的值只需将x Splay到其所在辅助树的根,然后修改其值,再maintain一下即可。

路径和询问要这样做:

我们先 ACCESS(u), 然后在 ACCESS(v) 的过程中, 一旦 走到了包含根结点(也就是包含 u)的 Auxiliary Tree, 这时我们走到的结点恰好就是 u 和 v 的最近公共祖 先, 不妨称这个结点为 d. 这时 d 所在的 Auxiliary Tree 中 d 的右子树的 maxcost 即为 u 到 d 的路径的 最大边权, v 所在的 Auxiliary Tree 的 maxcost 则是 v 到 d 的路径的最大边权. 于是我们所求的答案就是 6 这两个 maxcost 中的最大值. 因为 Access 操作的均摊复杂度为 O(log n), 所以回答这个询问所花时间也 是 O(log n) 的.

Update:换了一种写法,能避免边权的鬼畜问题。即在每条边上添加一个虚点,权值为该边边权;而原先的所有点的点权置为零。这样虽然空间多了一倍,但是非常好处理了!(也即,询问可以用先ChangeRoot,再Access,再Splay,再询问左子树的方法了。)

代码更新在最下方。

<法一>

#include<cstdio>
#include<iostream>
#include<stack>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 100005
int fa[maxn],c[maxn][2],siz[maxn];
bool is_root[maxn];
int val[maxn],totalval[maxn];
void Maintain(int x)
{
siz[x]=siz[c[x][0]]+siz[c[x][1]]+1;
totalval[x]=totalval[c[x][0]]+totalval[c[x][1]]+val[x];
}
void Rotate(int x,bool flag)
{
int y=fa[x];
c[y][!flag]=c[x][flag];
if(c[x][flag]){
fa[c[x][flag]]=y;
}
if(fa[y] && c[fa[y]][c[fa[y]][1]==y]==y){
c[fa[y]][c[fa[y]][1]==y]=x;
}
fa[x]=fa[y];
c[x][flag]=y;
fa[y]=x;
if(is_root[y]){
is_root[y]=0;
is_root[x]=1;
}
Maintain(y);
Maintain(x);
}
stack<int>st;
void Splay(int x)
{
if(!x || is_root[x]){
return;
}
int y;
while(y=fa[x],(!is_root[x])){
if(is_root[y]){
Rotate(x,c[y][0]==x);
}
else{
if((c[y][0]==x)==(c[fa[y]][0]==y)){
Rotate(y,c[fa[y]][0]==y);
}
else{
Rotate(x,c[y][0]==x);
y=fa[x];
}
Rotate(x,c[y][0]==x);
}
}
Maintain(x);
}
void Spla2(int x)
{
if(!x || is_root[fa[x]]){
return;
}
int y;
while(!is_root[y=fa[x]]){
if(is_root[fa[y]]){
Rotate(x,c[y][0]==x);
}
else{
if((c[y][0]==x)==(c[fa[y]][0]==y)){
Rotate(y,c[fa[y]][0]==y);
}
else{
Rotate(x,c[y][0]==x);
y=fa[x];
}
Rotate(x,c[y][0]==x);
}
}
Maintain(x);
}
void Access(int x){
int y;
Splay(x);
while(fa[x]){
y=fa[x];
Splay(y);
if(c[y][1]){
is_root[c[y][1]]=1;
}
is_root[x]=0;
c[y][1]=x;
Splay(x);
}
if(c[x][1]){
is_root[c[x][1]]=1;
c[x][1]=0;
}
}
int Calc(int x,int y){
if(x==y){
return 0;
}
Splay(y);
if(!fa[y]){
Spla2(x);
return totalval[c[x][0]]+val[x];
}
int z;
while(fa[y]){
z=fa[y];
Splay(z);
if(!fa[z]){
return totalval[c[z][1]]+totalval[y];
}
if(c[z][1]){
is_root[c[z][1]]=1;
}
is_root[y]=0;
c[z][1]=y;
Splay(y);
}
}
int n,m,cur;
int v[maxn<<1],nex[maxn<<1],first[maxn],w[maxn<<1],e;
void AddEdge(int U,int V,int W){
v[++e]=V;
w[e]=W;
nex[e]=first[U];
first[U]=e;
}
int bel[maxn];
bool vis[maxn];
void dfs(int U){
vis[U]=1;
for(int i=first[U];i;i=nex[i]){
if(!vis[v[i]]){
bel[i+1>>1]=v[i];
siz[v[i]]=1;
is_root[v[i]]=1;
val[v[i]]=totalval[v[i]]=w[i];
fa[v[i]]=U;
dfs(v[i]);
}
}
}
int main(){
int op,x,y,z;
// freopen("poj2763.in","r",stdin);
// freopen("poj2763.out","w",stdout);
scanf("%d%d%d",&n,&m,&cur);
for(int i=1;i<n;++i){
scanf("%d%d%d",&x,&y,&z);
AddEdge(x,y,z);
AddEdge(y,x,z);
}
siz[1]=1;
is_root[1]=1;
dfs(1);
for(int i=1;i<=m;++i){
scanf("%d%d",&op,&x);
if(!op){
Access(x);
printf("%d\n",Calc(x,cur));
cur=x;
}
else{
scanf("%d",&y);
Splay(bel[x]);
val[bel[x]]=y;
Maintain(bel[x]);
}
}
return 0;
}

<法二>

#include<cstdio>
#include<iostream>
#include<stack>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 200005
int fa[maxn],c[maxn][2],siz[maxn];
bool is_root[maxn],delta[maxn];
int val[maxn],totalval[maxn];
void Mark(int x){
if(x){
delta[x]^=1;
}
}
void Maintain(int x)
{
siz[x]=siz[c[x][0]]+siz[c[x][1]]+1;
totalval[x]=totalval[c[x][0]]+totalval[c[x][1]]+val[x];
}
void pushdown(int x){
if(delta[x]){
Mark(c[x][0]);
Mark(c[x][1]);
swap(c[x][0],c[x][1]);
delta[x]=0;
}
}
void Rotate(int x,bool flag)
{
int y=fa[x];
c[y][!flag]=c[x][flag];
if(c[x][flag]){
fa[c[x][flag]]=y;
}
if(fa[y] && c[fa[y]][c[fa[y]][1]==y]==y){
c[fa[y]][c[fa[y]][1]==y]=x;
}
fa[x]=fa[y];
c[x][flag]=y;
fa[y]=x;
if(is_root[y]){
is_root[y]=0;
is_root[x]=1;
}
Maintain(y);
Maintain(x);
}
stack<int>st;
void Splay(int x)
{
pushdown(x);
if(!x || is_root[x]){
return;
}
int U=x;
while(!is_root[U]){
st.push(U);
U=fa[U];
}
st.push(U);
while(!st.empty()){
pushdown(st.top());
st.pop();
}
int y;
while(y=fa[x],(!is_root[x])){
if(is_root[y]){
Rotate(x,c[y][0]==x);
}
else{
if((c[y][0]==x)==(c[fa[y]][0]==y)){
Rotate(y,c[fa[y]][0]==y);
}
else{
Rotate(x,c[y][0]==x);
y=fa[x];
}
Rotate(x,c[y][0]==x);
}
}
Maintain(x);
}
void Access(int x){
int y;
Splay(x);
while(fa[x]){
y=fa[x];
Splay(y);
Splay(x);
if(c[y][1]){
is_root[c[y][1]]=1;
}
is_root[x]=0;
c[y][1]=x;
Splay(x);
}
if(c[x][1]){
is_root[c[x][1]]=1;
c[x][1]=0;
}
}
void ChangeRoot(int x){
Access(x);
Splay(x);
Mark(x);
}
int QSum(int U,int V){
ChangeRoot(U);
Access(V);
Splay(V);
return val[V]+totalval[c[V][0]];
}
int v[maxn<<1],nex[maxn<<1],first[maxn],w[maxn<<1],e;
void AddEdge(int U,int V,int W){
v[++e]=V;
w[e]=W;
nex[e]=first[U];
first[U]=e;
}
int tot,bel[maxn];
void dfs(int U){
for(int i=first[U];i;i=nex[i]){
if(!siz[v[i]]){
bel[i+1>>1]=++tot; siz[tot]=1;
is_root[tot]=1;
val[tot]=totalval[tot]=w[i];
fa[tot]=U; siz[v[i]]=1;
is_root[v[i]]=1;
val[v[i]]=totalval[v[i]]=0;
fa[v[i]]=tot; dfs(v[i]);
}
}
}
int n,m,cur;
int main(){
int op,x,y,z;
// freopen("poj2763.in","r",stdin);
scanf("%d%d%d",&n,&m,&cur);
tot=n;
for(int i=1;i<n;++i){
scanf("%d%d%d",&x,&y,&z);
AddEdge(x,y,z);
AddEdge(y,x,z);
}
siz[1]=1;
is_root[1]=1;
dfs(1);
for(int i=1;i<=m;++i){
scanf("%d%d",&op,&x);
if(!op){
printf("%d\n",QSum(x,cur));
cur=x;
}
else{
scanf("%d",&y);
Splay(bel[x]);
val[bel[x]]=y;
Maintain(bel[x]);
}
}
return 0;
}

【lct】poj2763 Housewife Wind的更多相关文章

  1. POJ2763 Housewife Wind 树链剖分 边权

    POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...

  2. 【BZOJ2049】【LCT】Cave 洞穴勘测

    Description 辉 辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通 道组成,并且每条通道连接了 ...

  3. 【HDU4010】【LCT】Query on The Trees

    Problem Description We have met so many problems on the tree, so today we will have a query problem ...

  4. ZJOI2012网络 题解报告【LCT】

    题目描述 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环,同色的环指相同颜色的边构成的环. 在这 ...

  5. BZOJ2631 tree 【LCT】

    题目 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的边( ...

  6. POJ2763 Housewife Wind

      Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 9701   Accepted: 2661 Description Aft ...

  7. POJ2763 Housewife Wind(树剖+线段树)

    After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy lif ...

  8. [BZOJ 3669] [Noi2014] 魔法森林 【LCT】

    题目链接:BZOJ - 3669 题目分析 如果确定了带 x 只精灵A,那么我们就是要找一条 1 到 n 的路径,满足只经过 Ai <= x 的边,而且要使经过的边中最大的 Bi 尽量小. 其实 ...

  9. [BZOJ 2594] [Wc2006]水管局长数据加强版 【LCT】

    题目链接:BZOJ - 2594 题目分析 这道题如果没有删边的操作,那么就是 NOIP2013 货车运输,求两点之间的一条路径,使得边权最大的边的边权尽量小. 那么,这条路径就是最小生成树上这两点之 ...

随机推荐

  1. NSURLSession---iOS-Apple苹果官方文档翻译

    CHENYILONG Blog NSURLSession---iOS-Apple苹果官方文档翻译 NSURLSession 技术博客http://www.cnblogs.com/ChenYilong/ ...

  2. docker 加速

    Docker配置阿里云加速地址 打开阿里云网站https://cr.console.aliyun.com,登陆自己的阿里云账号. 然后只需要在服务器配置docker配置文件,只需要修改"Ex ...

  3. Linux内核多线程实现方法 —— kthread_create函数【转】

    转自:http://blog.csdn.net/sharecode/article/details/40076951 Linux内核多线程实现方法 —— kthread_create函数 内核经常需要 ...

  4. 4B/5B编码原理

    4B/5B编码原理 什么是4B/5B编码? 4B/5B编码是百兆以太网(即快速以太网)中线路层编码类型之一,就是用5bit的二进制数来表示4bit二进制数,映射方式如下表所示: 为什么要进行4B/5B ...

  5. React 16 源码瞎几把解读 【三 点 一】 把react组件对象弄到dom中去(矛头指向fiber,fiber不解读这个过程也不知道)

    一.ReactDOM.render 都干啥了 我们在写react的时候,最后一步肯定是 ReactDOM.render( <div> <Home name="home&qu ...

  6. Oracle-AWR报告简介及如何生成【转】

    AWR报告 awr报告是oracle 10g及以上版本提供的一种性能收集和分析工具,它能提供一个时间段内整个系统资源使用情况的报告,通过这个报告,我们就可以了解Oracle数据库的整个运行情况,比如硬 ...

  7. java各种链路工具性能监控工具

    Zipkin , Instana 和 Jaeger cat链路追踪系统 用于监控spring 的运行情况,比如内存,线程,池等宏观数据 spring boot admin java反编译 jar xv ...

  8. C++学习笔记--从虚函数说开去

    虚函数与纯虚函数: 虚函数:在某基类中声明为virtual并在一个或多个派生类中被重新定义的成员函数,virtual  函数返回类型  函数名(参数表){函数体;} ,实现多态性,通过指向派生类的基类 ...

  9. .pnts点云

    一种3d tiles格式 MIME格式: <configuration> <system.webServer> <staticContent> <remove ...

  10. Butter Knife:一个安卓视图注入框架

    Butter Knife:一个安卓视图注入框架 2014年5月8日 星期四 14:52 官网: http://jakewharton.github.io/butterknife/ GitHub地址: ...