bzoj2157
来源:互联网 发布:js 点击div刷新页面 编辑:程序博客网 时间:2024/05/20 14:26
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;}
阅读全文
0 0
- bzoj2157
- [BZOJ2157]旅游
- 【BZOJ2157】旅游
- BZOJ2157旅游
- bzoj2157: 旅游
- BZOJ2157 旅游
- BZOJ2157: 旅游
- 【bzoj2157】旅游
- bzoj2157 旅游
- 【BZOJ2157】旅游 裸树链剖分
- BZOJ2157【Link Cut Tree】
- 【BZOJ2157】旅游,树链剖分练习
- BZOJ2157:旅游 树链剖分
- BSOJ1495(BZOJ2157) 旅游 树链剖分
- [BZOJ2157] 旅游 边权树链剖分
- 【bzoj2157】旅游 LCT
- 【bzoj2157】旅游(树链剖分边权)
- bzoj2157 旅游 树链剖分
- 启明星第八周学习总结
- Java —— 数据统计图的实现(柱形图,饼图,折线图)
- 堆排(模板)
- 数据结构第七周项目-排队看病模拟(队列)
- 广告拦截之Adblock Plus 安装
- bzoj2157
- 栈的简单运用
- Androidstudio控制台分层输出接口日志.类似BeJSON,HiJson格式化JSON
- 做题修改1
- HAProxy 1.8 新特性
- 输出定长的string
- 机器学习中Precision,Recall的理解
- Day011
- SSH网上商城1_数据库设计