SPOJ QTREE Query on a tree (lct)
2016-03-31 23:15
423 查看
修改边权,询问最大值
#include <bits/stdc++.h> #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; #pragma comment(linxer, "/STACK:102400000,102400000") #define LL long long #define pii pair<int, int> #define MP make_pair #define ls i << 1 #define rs ls | 1 #define md (ll + rr >> 1) #define lson ll, md, ls #define rson md + 1, rr, rs #define mod 1000000007 #define inf 0x3f3f3f3f #define N 10010 #define M 20020 int fst , vv[M], nxt[M], cost[M], e; int tot, pre , ch [2], val ; int mx , buttom ; bool rt ; int n, m; void init(){ memset(fst, -1, sizeof fst); for(int i = 0; i <= n; ++i) ch[i][0] = ch[i][1] = pre[i] = 0; memset(rt, 1, sizeof rt); mx[0] = -inf; e = 0; } void add(int u, int v, int c){ vv[e] = v, nxt[e] = fst[u], cost[e] = c, fst[u] = e++; } void dfs(int u, int p){ pre[u] = p; for(int i = fst[u]; ~i; i = nxt[i]){ int v = vv[i], c = cost[i]; if(v == p) continue; buttom[i/2+1] = v; val[v] = mx[v] = c; dfs(v, u); } } bool judge(int u, int v){ while(pre[u]) u = pre[u]; while(pre[v]) v = pre[v]; return u == v; } void push_up(int x){ mx[x] = max(val[x], max(mx[ch[x][0]], mx[ch[x][1]])); } void push_down(int x){ } void P(int x){ while(!rt[x]) P(pre[x]); push_down(x); } void rot(int x){ int y = pre[x], d = ch[y][1] == x; ch[y][d] = ch[x][!d]; if(ch[x][!d]) pre[ch[x][!d]] = y; ch[x][!d] = y; pre[x] = pre[y]; pre[y] = x; if(rt[y]) rt[y] = 0, rt[x] = 1; else ch[pre[x]][ch[pre[x]][1] == y] = x; push_up(y); } void splay(int x){ //P(x); while(!rt[x]){ int f = pre[x], ff = pre[f]; if(rt[f]) rot(x); else if((ch[ff][1] == f) == (ch[f][1] == x)) rot(f), rot(x); else rot(x), rot(x); } push_up(x); } int Access(int x){ int y = 0; for(; x; y = x, x = pre[x]){ splay(x); rt[ch[x][1]] = 1; ch[x][1] = y; rt[y] = 0; push_up(x); } return y; } void lca(int &u, int &v){ Access(v), v = 0; for(splay(u); pre[u]; v = u, u = pre[u], splay(u)){ rt[ch[u][1]] = 1; ch[u][1] = v; rt[v] = 0; push_up(u); } } int main(){ int cas; scanf("%d", &cas); while(cas--){ scanf("%d", &n); init(); for(int i = 1; i < n; ++i){ int u, v, c; scanf("%d%d%d", &u, &v, &c); add(u, v, c); add(v, u, c); } dfs(1, 0); char s[10]; while(1){ scanf("%s", s); if(s[0] == 'D') break; if(s[0] == 'C'){ int u, c; scanf("%d%d", &u, &c); u = buttom[u]; splay(u); val[u] = c; push_up(u); } else{ int u, v; scanf("%d%d", &u, &v); lca(u, v); printf("%d\n", max(mx[v], mx[ch[u][1]])); } } } return 0; }
相关文章推荐
- [BZOJ2594][WC2006][LCT][MST]水管局长数据加强版
- 【LCT】BZOJ2049[Sdoi2008]Cave 洞穴勘测
- URAL 1553【CAVE and TUNNEL】
- LCT解法解决数据结构神薙bzoj1036
- bzoj2049[洞穴勘测]纯粹的LCT
- [补充]LCT有关细节的处理及理解
- LCT的初步理解
- LCT
- SPOJ QTREE LCT
- BZOJ1180 [CROATIAN2009]OTOCI
- bzoj2049: [Sdoi2008]Cave 洞穴勘测
- bzoj-2001 City 城市建设
- bzoj-2555 SubString
- bzoj-1180 OTOCI
- Aizu 2450 Do use segment tree LCT
- 【NOI2014】【BZOJ3669】魔法森林
- 【WC2006】【BZOJ2594】水管局长数据加强版
- 【BZOJ4025】二分图
- 【SDOI2014】【BZOJ3531】旅行
- 【WC2005】【BZOJ1453】Dface双面棋盘