本文主要是介绍[HDU4010]Query on The Trees LCT,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
找个要打懒标记的裸题刷一刷Orz
然后数组没清干净结果RE了好久
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define SF scanf
#define PF printf
using namespace std;
typedef long long LL;
const int MAXN = 300000;
const int INF = 2000000000;
struct LCT {int fa[MAXN+10], ch[MAXN+10][2];int Add[MAXN+10], val[MAXN+10], MAX[MAXN+10];int st[MAXN+10];bool rev[MAXN+10];bool isroot(int x) {return ch[fa[x]][0] != x && ch[fa[x]][1] != x;}void up(int x) {MAX[x] = max(val[x], max(MAX[ch[x][0]], MAX[ch[x][1]]));}void pushdown(int x) {int &ls = ch[x][0], &rs = ch[x][1];if(rev[x]) {rev[x] ^= 1; rev[ls] ^= 1; rev[rs] ^= 1;swap(ls, rs);}if(Add[x]) {if(ls) { val[ls] += Add[x]; Add[ls] += Add[x]; MAX[ls] += Add[x]; }if(rs) { val[rs] += Add[x]; Add[rs] += Add[x]; MAX[rs] += Add[x]; }Add[x] = 0;}}void Rotate(int x) {int y = fa[x], z = fa[y];bool d = ch[y][0] == x;if(!isroot(y)) ch[z][y == ch[z][1]] = x;fa[x] = z; fa[y] = x; fa[ch[x][d]] = y;ch[y][!d] = ch[x][d]; ch[x][d] = y;up(y);}void splay(int x) {int top = 0; st[++top] = x;for(int i = x; !isroot(i); i = fa[i]) st[++top] = fa[i];for(int i = top; i; i--) pushdown(st[i]);while(!isroot(x)) {int y = fa[x];if(!isroot(y)) Rotate((x == ch[y][0] ^ y == ch[fa[y]][0]) ? x : y);Rotate(x);}up(x);}void access(int x) {int t = 0;while(x) {splay(x);ch[x][1] = t;up(x);t = x; x = fa[x];}}void makeroot(int x) {access(x); splay(x); rev[x] ^= 1;}void link(int x, int y) {makeroot(x); fa[x] = y; splay(x);}void cut(int x, int y) {makeroot(x); access(y); splay(y); ch[y][0] = fa[ch[y][0]] = 0;up(y);}int find(int x) {access(x); splay(x);int y = x;while(ch[y][0]) y = ch[y][0];return y;}void add(int x, int y, int v) {makeroot(x); access(y); splay(y);val[y] += v; Add[y] += v; MAX[y] += v;}int query(int x, int y) {makeroot(x); access(y); splay(y);return MAX[y];}void init(int n) {memset(MAX, 0, sizeof(MAX));memset(ch, 0, sizeof(ch));memset(fa, 0, sizeof(fa));memset(rev, 0, sizeof(rev));}
} sp;
struct Node {int v, next;
} Edge[MAXN*2+10];
int adj[MAXN+10], ecnt;
int n, m;
queue <int> q;
void addedge(int u, int v) {Node &e = Edge[ecnt];e.v = v; e.next = adj[u]; adj[u] = ecnt++;
}
void bfs() {q.push(1);while(!q.empty()) {int u = q.front(); q.pop();for(int i = adj[u]; ~i; i = Edge[i].next) if(Edge[i].v != sp.fa[u]) {sp.fa[Edge[i].v] = u;q.push(Edge[i].v);}}
}
int main()
{while(~SF("%d", &n)) {memset(adj, -1, sizeof(adj)); ecnt = 0;sp.init(n);for(int i = 1; i < n; i++) {int u, v;SF("%d%d", &u, &v);addedge(u, v); addedge(v, u);}for(int i = 1; i <= n; i++) SF("%d", &sp.val[i]);bfs();SF("%d", &m);for(int i = 1; i <= m; i++) {int op, x, y, v;SF("%d", &op);switch(op) {case 1:SF("%d%d", &x, &y);if(sp.find(x) == sp.find(y)) puts("-1");else sp.link(x, y);break;case 2:SF("%d%d", &x, &y);if(sp.find(x) != sp.find(y) || x == y) puts("-1");else sp.cut(x, y);break;case 3:SF("%d%d%d", &v, &x, &y);if(sp.find(x) != sp.find(y)) puts("-1");else sp.add(x, y, v);break;case 4:SF("%d%d", &x, &y);if(sp.find(x) != sp.find(y)) puts("-1");else PF("%d\n", sp.query(x, y));break;}}puts("");}return 0;
}
这篇关于[HDU4010]Query on The Trees LCT的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!