支持:
1.添加边 x,y
2.删边 x,y
3.对于路径x,y上的所有节点的值加上w
4.询问路径x,y上的所有节点的最大权值

分析:
裸的lct...
rev忘了清零死循环了两小时。。。

1:就是link操作

2:就是cut操作

3:维护多一个mx域,mx[x]表示在splay中以节点x为根的子树的最大点权,每次修改时,把x置为splay的根,打通y到x的路径,把y splay到根,那么,直接对y节点的lazy标记加上为w即可。

4:同3操作,把x置为splay的根,打通y到x的路径,把y splay到根,那么,y子树所对应的节点就是路径x到y的所有节点。

另外,题目貌似描述有点问题,不光是询问非法输出-1,是指操作如果是非法就输出-1....

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ const int MAXN = 3e5+5; // 外挂
char op;
inline void Int(int &x){
while( !isdigit(op=getchar()) );
x = op-'0';
while(isdigit(op=getchar()))
x = x*10+op-'0';
} struct LCT{
int ch[MAXN][2],fa[MAXN];
int lz[MAXN],mx[MAXN],val[MAXN];
bool rev[MAXN];
int sta[MAXN],top; inline void init(){
Clear(ch);
Clear(fa);
Clear(lz);
Clear(rev);
mx[0] = 0;
} inline void modify(int x,int d){ // 单点增加lazy标记
if(!x)return;
lz[x] += d;
mx[x] += d;
val[x] += d;
} inline void clear(int x){
if(!x)return;
if(rev[x]){
if(ch[x][0])rev[ch[x][0]] ^= 1;
if(ch[x][1])rev[ch[x][1]] ^= 1;
swap(ch[x][0],ch[x][1]);
rev[x] = 0;
}
if(lz[x]){
modify(ch[x][0],lz[x]);
modify(ch[x][1],lz[x]);
lz[x] = 0;
}
} inline void update(int x){
if(!x)return;
mx[x] = max( val[x],max(mx[ch[x][0]],mx[ch[x][1]]) );
} inline bool isRoot(int x){
return !x || !( (ch[ fa[x] ][0]==x) || (ch[ fa[x] ][1]==x) );
} inline bool sgn(int x){
return ch[ fa[x] ][1]==x;
}
inline void setc(int y,int d,int x){
ch[y][d] = x;
fa[x] = y;
}
inline void rot(int x){
int y = fa[x] , z = fa[y] , d = sgn(x)^1;
setc(y,d^1,ch[x][d]);
if(isRoot(y)) fa[x] = fa[y];
else setc(z,sgn(y),x);
setc(x,d,y);
update(y);
} inline void splay(int x){
if(!x)return;
top = 0;
sta[++top] = x;
for(int y=x;!isRoot(y);y=fa[y])
sta[++top] = fa[y];
while(top)clear(sta[top--]); while(!isRoot(x)){
if(isRoot(fa[x]))rot(x);
else{
sgn(x)==sgn(fa[x]) ? rot(fa[x]) : rot(x);
rot(x);
}
}
update(x);
} inline int access(int x){
int y = 0;
for(;x;x=fa[y = x]){
splay(x);
ch[x][1] = y;
update(x);
}
return y;
} inline void mRoot(int x){
rev[ access(x) ] ^= 1;
splay(x);
} inline int getRoot(int x){
x = access(x);
while(ch[x][0]){
x = ch[x][0];
clear(x);
}
return x;
} inline bool jud(int x,int y){// ok
return getRoot(x)==getRoot(y);
} inline void link(int x,int y){
if(jud(x,y)){
puts("-1");
return;
}
mRoot(x);
fa[x] = y;
//access(x);
} inline void cut(int x,int y){
if(x==y||!jud(x,y)){
puts("-1");
return;
}
mRoot(x);
access(y);
splay(y);
fa[ ch[y][0] ] = 0;
ch[y][0] = 0;
update(y);
} inline void modify(int x,int y,int c){
if(!jud(x,y)){
puts("-1");
return;
}
mRoot(x);
access(y);
splay(y);
modify(y,c);
clear(y);
} inline int ask(int x,int y){
if(!jud(x,y))
return -1;
mRoot(x);
access(y);
splay(y);
return mx[y];
} }lct; struct Edge{
int y,next;
}edge[MAXN<<1]; int po[MAXN],tol; inline void add(int x,int y){
edge[++tol].y = y;
edge[tol].next = po[x];
po[x] = tol;
} void dfs(int x,int pa){
lct.fa[x] = pa;
for(int i=po[x];i;i=edge[i].next)
if(edge[i].y!=pa)
dfs(edge[i].y,x);
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int x,y,w,n,m,op;
while(~RD(n)){
lct.init();
Clear(po);
tol = 0; REP(i,2,n){
Int(x);Int(y);
add(x,y);
add(y,x);
} dfs(1,0); rep1(i,n){
RD(lct.val[i]);
lct.mx[i] = lct.val[i];
}
RD(m);
while(m--){
Int(op);
if(op==1){
Int(x);Int(y);
lct.link(x,y);
}else if(op==2){
Int(x);Int(y);
lct.cut(x,y);
}else if(op==3){
Int(w);Int(x);Int(y);
lct.modify(x,y,w);
}else{
Int(x);Int(y);
printf("%d\n",lct.ask(x,y));
}
}
puts("");
} return 0;
}

  

hdu 4010 Query on The Trees LCT的更多相关文章

  1. 动态树(LCT):HDU 4010 Query on The Trees

    Query on The Trees Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Othe ...

  2. HDOJ 4010 Query on The Trees LCT

    LCT: 分割.合并子树,路径上全部点的点权添加一个值,查询路径上点权的最大值 Query on The Trees Time Limit: 10000/5000 MS (Java/Others)   ...

  3. HDU 4010 Query on The Trees(动态树LCT)

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

  4. HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...

  5. HDU 4010 Query on The Trees

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

  6. HDU 4010.Query on The Trees 解题报告

    题意: 给出一颗树,有4种操作: 1.如果x和y不在同一棵树上则在xy连边 2.如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离 3.如果x和y在同一棵树上则x到y的路径上所有的点 ...

  7. HDU 4010 Query on The Trees(动态树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意:一棵树,四种操作: (1)若x和y不在一棵树上,将x和y连边: (2)若x和y在一棵树上, ...

  8. HDU 4010 Query on The Trees(动态树)

    题意 给定一棵 \(n\) 个节点的树,每个点有点权.完成 \(m\) 个操作,操作四两种,连接 \((x,y)\) :提 \(x\) 为根,并断 \(y\) 与它的父节点:增加路径 \((x,y)\ ...

  9. HDU4010 Query on The Trees(LCT)

    人生的第一道动态树,为了弄懂它的大致原理,需要具备一些前置技能,如Splay树,树链剖分的一些概念.在这里写下一些看各种论文时候的心得,下面的代码是拷贝的CLJ的模板,别人写的模板比较可靠也方便自己学 ...

随机推荐

  1. 【转】2D动画:view的Matrix

    Matrix,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 首先介绍一下矩阵运算.加法和减法就不用说了,太简单了,对应位相加就好.图像处理,主要用到的是乘法 ...

  2. MES取所有部门的函数实例

    USE [ChangHong]GO/****** Object: UserDefinedFunction [dbo].[FN_GetDeptCode] Script Date: 04/26/2016 ...

  3. STL 速解

    STL(Standard Template Library)是C++的标准模版库. STL概述 STL的一个重要概念是数据结构和算法的分离,这使得STL变得十分通用.例如:由于STL的sort()函数 ...

  4. 获取oracle 表字段,表名,以及主键之类等等的信息

    数据库版本号:select * from v$version 数据库名:select * from v$instance 注意: 我在C#项目中查询语句的时候报“ORA-00911: 无效字符” 的错 ...

  5. Eclipse和Android Studio中的DDMS使用时什么不同?

    http://www.jb51.net/softjc/454131.html Eclipse和Android Studio中的DDMS使用时什么不同? 相信很多经常开发Android应用的朋友应该都接 ...

  6. cJSON 使用笔记

    缘      起 最近在stm32f103上做一个智能家居的项目,其中选择的实时操作系统是 rt_thread OS v1.2.2稳定版本,其中涉及到C和java(android)端数据的交换问题,经 ...

  7. CMSIS Example - Mail and Message

    /*---------------------------------------------------------------------------- * RL-ARM - RTX *----- ...

  8. HDU 5583 Kingdom of Black and White 水题

    Kingdom of Black and White Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showpr ...

  9. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  10. uoj #9. 【UTR #1】vfk的数据 水题

    #9. [UTR #1]vfk的数据 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/9 Description ...