bzoj2157
LCT板子,打个lazy即可
# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <algorithm>
# include <string.h>
# include <map>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
return x * z;
}
const int MAXN(200010);
int n, S[MAXN], ch[2][MAXN], fa[MAXN], mx[MAXN], mi[MAXN], sum[MAXN], rev[MAXN], w[MAXN], tag[MAXN];
IL bool Son(RG int x){ return ch[1][fa[x]] == x; }
IL bool Isroot(RG int x){ return ch[0][fa[x]] != x && ch[1][fa[x]] != x; }
IL void Update(RG int x){
sum[x] = sum[ch[0][x]] + sum[ch[1][x]] + w[x];
mx[x] = max(mx[ch[0][x]], mx[ch[1][x]]);
mi[x] = min(mi[ch[0][x]], mi[ch[1][x]]);
if(x > n) mx[x] = max(mx[x], w[x]), mi[x] = min(mi[x], w[x]);
}
IL void Down(RG int x){ swap(mi[x], mx[x]); sum[x] = -sum[x]; mi[x] = -mi[x]; mx[x] = -mx[x]; w[x] = -w[x]; tag[x] ^= 1; }
IL void Pushdown(RG int x){
if(tag[x]){
tag[x] = 0;
if(ch[0][x]) Down(ch[0][x]);
if(ch[1][x]) Down(ch[1][x]);
}
if(rev[x]) rev[x] = 0, rev[ch[0][x]] ^= 1, rev[ch[1][x]] ^= 1, swap(ch[0][x], ch[1][x]);
}
IL void Rot(RG int x){
RG int y = fa[x], z = fa[y], c = Son(x);
if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z;
ch[c][y] = ch[!c][x]; fa[ch[c][y]] = y;
ch[!c][x] = y; fa[y] = x; Update(y);
}
IL void Splay(RG int x){
RG int top = 0; S[++top] = x;
for(RG int y = x; !Isroot(y); y = fa[y]) S[++top] = fa[y];
while(top) Pushdown(S[top--]);
for(RG int y = fa[x]; !Isroot(x); Rot(x), y = fa[x])
if(!Isroot(y)) Son(x) ^ Son(y) ? Rot(x) : Rot(y);
Update(x);
}
IL void Access(RG int x){ for(RG int y = 0; x; y = x, x = fa[x]) Splay(x), ch[1][x] = y, Update(x); }
IL void Makeroot(RG int x){ Access(x); Splay(x); rev[x] ^= 1; }
IL int Findroot(RG int x){ Access(x); Splay(x); while(ch[0][x]) x = ch[0][x]; return x; }
IL void Split(RG int x, RG int y){ Makeroot(x); Access(y); Splay(y); }
IL void Link(RG int x, RG int y){ Makeroot(x); fa[x] = y; }
IL void Cut(RG int x, RG int y){ Split(x, y); fa[x] = ch[0][y] = 0; }
int main(RG int argc, RG char* argv[]){
n = Read();
for(RG int i = 0; i <= n; i++) mx[i] = -2e9, mi[i] = 2e9;
for(RG int i = 1; i < n; i++){
RG int u = Read() + 1, v = Read() + 1;
Link(u, n + i); Link(n + i, v);
sum[n + i] = mx[n + i] = mi[n + i] = w[n + i] = Read();
}
RG int Q = Read(), u, v; RG char c[10];
while(Q--){
scanf(" %s", c); u = Read(); v = Read();
if(c[0] == 'C') Splay(n + u), w[n + u] = v, Update(n + u);
else{
u++; v++; Split(u, v);
if(c[0] == 'N') Down(v);
else if(c[0] == 'S') printf("%d\n", sum[v]);
else if(c[1] == 'A') printf("%d\n", mx[v]);
else printf("%d\n", mi[v]);
}
}
return 0;
}
bzoj2157的更多相关文章
- 【BZOJ2157】旅游(树链剖分,Link-Cut Tree)
[BZOJ2157]旅游(树链剖分,Link-Cut Tree) 题面 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥 ...
- 【BZOJ2157】旅游 树链剖分+线段树
[BZOJ2157]旅游 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本 ...
- [BZOJ2157]旅游(树链剖分/LCT)
树剖裸题,当然LCT也可以. 树剖: #include<cstdio> #include<algorithm> #define ls (x<<1) #define ...
- bzoj2157旅游
bzoj2157旅游 题意: 给定有权树,支持单边权修改,路径边权取相反数,路径边权求和,路径边权求最大最小值. 题解: 用link-cut tree link-cut tree与树链剖分有些类似,都 ...
- 【BZOJ2157】旅游 LCT
模板T,SB的DMoon..其实样例也是中国好样例...一开始不会复制,yangyang:找到“sample input”按住shift,按page down.... #include <ios ...
- BZOJ2157: 旅游
传送门 先讲一个悲伤地故事 RunID User Problem Result Memory Time Language Code_Length Submit_Time 1635823 Cydiate ...
- BZOJ2157 旅行 模拟
题目内容: Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条 ...
- [bzoj2157]旅游 (lct)
这个应该也算裸的模板题吧..主要是边权的问题,对于每条边u->v,我们可以新建一个节点代替他,把边的信息弄到新的点上,就变成u->x->v了... 当然了这样的话要防止u和v这些没用 ...
- ⌈洛谷1505⌋⌈BZOJ2157⌋⌈国家集训队⌋旅游【树链剖分】
题目链接 [洛谷] [BZOJ] 题目描述 Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T ...
随机推荐
- tcping-安装
1.LINUX安装方法: 下载地址: http://www.linuxco.de/tcping/tcping.html 不过LINUX操作比WINDOWS的差.建议还是使用WINDOWS版本. 2.W ...
- 在C#中几种常见数组复制方法的效率对比
原文是在http://blog.csdn.net/jiangzhanchang/article/details/9998229 看到的,本文在原文基础上增加了新的方法,并对多种数据类型做了更全面的对比 ...
- C++ 值类型和引用类型传递示例
// win32test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" void swap_point(int * &a , in ...
- [转载]LVS+Keepalived之三大模式
LVS + Keepalived之三大模式 ============================================================================== ...
- JavaScript 历史漫谈
话说 JavaScript 其实是诞生在一个特殊的时期,20世纪90年代,正值第三次科技革命时期,互联网刚开始进入人们的生活.设想一下,在网速只有几十比特的那个时代,在登录注册时要花上很长一段时间等待 ...
- 搭建ssr服务器
搭建ssr服务器 首先,先说一下,为什么这么久没写博客. 一方面,最近在搭建自己的服务器.挺忙的. 另一方面,写了许多有关服务器构建,网站构建的word.但没有润色,所以打算等自己服务器做好了整理一下 ...
- lamp环境部署脚本
关于lamp环境的安装脚本,直接复制即可使用 注:apache2.2.X 版本和apache2.4.X版本 本人推荐兼容性版本安装 apache2.4.25 + apr1.5.2 + apr-util ...
- $.ajax的一些坑啊
1.如果发送ajax返回的数据为json务必设置其 Content-Type:application/json;charset=UTF-8 不然会导致其success:function(data)中的 ...
- linux 获取CPU个数
#include<stdio.h> #include<unistd.h> int main() { int cpu_num; cpu_num = sysconf(_SC_NPR ...
- “No module named context_processors”
之前因为django国际化的问题在settings.py里面改了很多东西,国际化是好使了,但是今天要用站点管理admin的时候出!错!了! 我前天用的时候还好好的啊,我的models.py和admin ...