Uva1553 Caves and Tunnels LCT

简单题,主要为了练手。

  1 #include <cstdio>
  2 #include <iostream>
  3 #define maxn 100010
  4 using namespace std;
  5
  6 namespace L {
  7     int pnt[maxn], pre[maxn], son[maxn][2], rtg[maxn], val[maxn], mxv[maxn];
  8
  9     inline void update( int nd ) {
 10         mxv[nd] = max( val[nd], max(mxv[son[nd][0]],mxv[son[nd][1]]) );
 11     }
 12     void rotate( int nd, int d ) {
 13         int p = pre[nd];
 14         int s = son[nd][!d];
 15         int ss = son[s][d];
 16         son[nd][!d] = ss;
 17         son[s][d] = nd;
 18         if( p ) son[p][ nd==son[p][1] ] = s;
 19         else pnt[s] = pnt[nd];
 20         pre[nd] = s;
 21         pre[ss] = nd;
 22         pre[s] = p;
 23         update(nd);
 24         update(s);
 25     }
 26     void pushdown( int nd ) {
 27         if( rtg[nd] ) {
 28             int &ls=son[nd][0], &rs=son[nd][1];
 29             swap(ls,rs);
 30             rtg[ls] ^= 1;
 31             rtg[rs] ^= 1;
 32             rtg[nd] = 0;
 33         }
 34     }
 35     void bigpush( int nd ) {
 36         if( pre[nd] ) bigpush(pre[nd]);
 37         pushdown(nd);
 38     }
 39     void splay( int nd, int top=0 ) {
 40         bigpush(nd);
 41         while( pre[nd]!=top ) {
 42             int p = pre[nd];
 43             int nl = nd==son[p][0];
 44             if( pre[p]==top ) {
 45                 rotate( p, nl );
 46             } else {
 47                 int pp = pre[p];
 48                 int pl = p==son[pp][0];
 49                 if( nl==pl ) {
 50                     rotate( pp, pl );
 51                     rotate( p, nl );
 52                 } else {
 53                     rotate( p, nl );
 54                     rotate( pp, pl );
 55                 }
 56             }
 57         }
 58     }
 59     void access( int nd ) {
 60         int u = nd;
 61         int v = 0;
 62         while( u ) {
 63             splay(u);
 64             int s = son[u][1];
 65             pre[s] = 0;
 66             pnt[s] = u;
 67             pre[v] = u;
 68             son[u][1] = v;
 69             update(u);
 70             v = u;
 71             u = pnt[u];
 72         }
 73         splay(nd);
 74     }
 75     void init( int n ) {
 76         for( int i=0; i<=n; i++ )
 77             pnt[i] = pre[i] = son[i][0] = son[i][1]
 78                 = rtg[i] = val[i] = mxv[i] = 0;
 79     }
 80     void makeroot( int nd ) {
 81         access(nd);
 82         rtg[nd] ^= 1;
 83     }
 84     void link( int u, int v ) {
 85         makeroot(u);
 86         makeroot(v);
 87         pnt[u] = v;
 88     }
 89     void inc_val( int nd, int w ) {
 90         splay( nd );
 91         val[nd] += w;
 92         update( nd );
 93     }
 94     int qu_max( int u, int v ) {
 95         makeroot(u);
 96         access(v);
 97         return max( val[v], mxv[son[v][0]] );
 98     }
 99 };
100
101 int n, q;
102
103 int main() {
104     scanf( "%d", &n );
105     L::init(n);
106     for( int i=2,u,v; i<=n; i++ ) {
107         scanf( "%d%d", &u, &v );
108         L::link(u,v);
109     }
110     scanf( "%d", &q );
111     while( q-- ) {
112         char ch[10];
113         int u, v, w;
114         scanf( "%s", ch );
115         if( ch[0]==‘I‘ ) {
116             scanf( "%d%d", &u, &w );
117             L::inc_val(u,w);
118         } else {
119             scanf( "%d%d", &u, &v );
120             printf( "%d\n", L::qu_max(u,v) );
121         }
122     }
123 }

时间: 2024-10-25 20:56:55

Uva1553 Caves and Tunnels LCT的相关文章

URAL 题目1553. Caves and Tunnels(Link Cut Tree 改动点权,求两点之间最大)

1553. Caves and Tunnels Time limit: 3.0 second Memory limit: 64 MB After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to research it using remote controlled robots. It was found out that ther

URAL 题目1553. Caves and Tunnels(Link Cut Tree 修改点权,求两点之间最大)

1553. Caves and Tunnels Time limit: 3.0 second Memory limit: 64 MB After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to research it using remote controlled robots. It was found out that ther

Caves and Tunnels

Caves and Tunnels Time limit: 3.0 secondMemory limit: 64 MB After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to research it using remote controlled robots. It was found out that there exist

URAL 1553. Caves and Tunnels 树链拆分

一颗树 每次出发点右键值是0 2操作模式1.第一i右键点值添加x 2.乞讨u至v在这条路上右上方值 树为主的连锁分裂称号 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100010; struct edge { int v, next; }e[maxn*2]; int first[maxn], cnt; void AddEdg

URAL 1553. Caves and Tunnels 树链剖分

一棵树 开始每个点的权值都为0 2种操作1.将第i个点的权值增加x 2.求u到v这条路上最大的权值 树链剖分基础题 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100010; struct edge { int v, next; }e[maxn*2]; int first[maxn], cnt; void AddEdge(i

luoguP2590 [ZJOI2008]树的统计 [树链剖分] [TLE的LCT]

题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节点的权值和 注意:从点u到点v的路径上的节点包括u和v本身 输入输出格式 输入格式: 输入文件的第一行为一个整数n,表示节点的个数. 接下来n – 1行,每行2个整数a和b,表示节点a和节点b之

[BZOJ2049] [CodeVS1839] [SDOI2008] Cave 洞穴勘测 (LCT)

Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如两个洞穴可以通过一条或者多条通道按一定顺序连接起来,那么这两个洞穴就是连通的,按顺序连接在一起的这些通道则被称之为这两个洞穴之间的一条路径.洞穴都十分坚固无法破坏,然而通道不太稳定,时常因为外界影响而发生改变,比如,根据有关仪器的监测结果,123号洞穴和127号洞穴之间有时会出现一条通

一些LCT裸题

又来回炉lct了= = [bzoj3514]: Codechef MARCH14 GERALD07加强版 模版题.常见姿势,把边也当成点. 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 const int maxn=200233<<1; 6 const int inf=1000023333; 7 struct zs{ 8 int u,v

[BZOJ2002][Hnoi2010]Bounce弹飞绵羊 LCT

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 建图,每次往后面跳就往目标位置连边,将跳出界的点设为同一个点.对于修改操作发现可以用LCT维护图的连通性,然后用size域维护跳的点的次数就行了. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int inline readint(