poj 3237 Tree

就是简单的树链剖分,但标记下传的时候一定要 ^1 而不能直接 = 1,我竟然WA在这么逗比的错误上不如一头撞死……

上代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define N 1100000
#define inf 0x7f7f7f7f
using namespace std; struct sss
{
int minnum, maxnum;
int push;
}t[N*];
int n, nowplace, bianp[N];
int p[N], next[N*], v[N*], c[N*], bnum;
int fa[N], son[N], siz[N], deep[N], top[N], w[N]; void build_tree(int now, int l, int r)
{
t[now].minnum = inf; t[now].maxnum = -inf; t[now].push = ;
if (l == r) return;
int mid = (l+r)/;
build_tree(now*, l, mid); build_tree(now*+, mid+, r);
} void downdate(int now)
{
if (!t[now].push) return; t[now].push = ;
t[now*].push ^= ; t[now*+].push ^= ;
swap(t[now*].maxnum, t[now*].minnum);
swap(t[now*+].maxnum, t[now*+].minnum);
t[now*].maxnum *= -; t[now*].minnum *= -;
t[now*+].maxnum *= -; t[now*+].minnum *= -;
} void update(int now)
{
t[now].maxnum = max(t[now*].maxnum, t[now*+].maxnum);
t[now].minnum = min(t[now*].minnum, t[now*+].minnum);
} void addbian(int x, int y)
{
bnum++; next[bnum] = p[x]; p[x] = bnum; v[bnum] = y;
bnum++; next[bnum] = p[y]; p[y] = bnum; v[bnum] = x;
} void dfs_1(int now, int nowfa, int nowdeep)
{
int k = p[now]; fa[now] = nowfa; deep[now] = nowdeep;
int maxson = ; son[now] = ; siz[now] = ;
while (k)
{
if (v[k] != nowfa)
{
bianp[(k+)/] = v[k];
dfs_1(v[k], now, nowdeep+);
siz[now] += siz[v[k]];
if (siz[v[k]] > maxson)
{
maxson = siz[v[k]];
son[now] = v[k];
}
}
k = next[k];
}
} void dfs_2(int now, int nowfa, int nowtop)
{
int k = p[now]; top[now] = nowtop; w[now] = ++nowplace;
if (son[now]) dfs_2(son[now], now, nowtop);
while (k)
{
if (v[k] != nowfa && v[k] != son[now])
dfs_2(v[k], now, v[k]);
k = next[k];
}
} int task(int now, int l, int r, int al, int ar)
{
if (al <= l && r <= ar) return t[now].maxnum;
int mid = (l+r)/, ans = -inf;
downdate(now);
if (al <= mid) ans = task(now*, l, mid, al, ar);
if (ar > mid) ans = max(ans, task(now*+, mid+, r, al, ar));
update(now); return ans;
} void tneg(int now, int l, int r, int tl, int tr)
{
if (tl <= l && r <= tr)
{
downdate(now);
swap(t[now].maxnum, t[now].minnum);
t[now].maxnum *= -; t[now].minnum *= -;
t[now].push ^= ; return;
}
int mid = (l+r)/;
downdate(now);
if (tl <= mid) tneg(now*, l, mid, tl, tr);
if (tr > mid) tneg(now*+, mid+, r, tl, tr);
update(now); return;
} void chan(int now, int l, int r, int cplace, int cnum)
{
if (l == r)
{
t[now].maxnum = t[now].minnum = cnum;
return;
}
int mid = (l+r)/;
downdate(now);
if (cplace <= mid) chan(now*, l, mid, cplace, cnum);
else chan(now*+, mid+, r, cplace, cnum);
update(now); return;
} void neg(int u, int v)
{
int f1 = top[u], f2 = top[v];
if (deep[f1] < deep[f2]) { swap(f1, f2); swap(u, v); }
if (f1 == f2)
{
if (u == v) return;
if (w[u] > w[v]) swap(u, v);
tneg(, , n, w[son[u]], w[v]);
return;
}
tneg(, , n, w[f1], w[u]); neg(fa[f1], v);
} int find(int u, int v)
{
int f1 = top[u],f2 = top[v];
if (deep[f1] < deep[f2]) { swap(f1, f2); swap(u, v); }
if (f1 == f2)
{
if (u == v) return -inf;
if (w[u] > w[v]) swap(u, v);
return task(, , n, w[son[u]], w[v]);
}
int ans = task(, , n, w[f1], w[u]);
return max(ans, find(fa[f1], v));
} int main()
{
int T; scanf("%d", &T);
while (T--)
{
scanf("%d", &n); memset(p, , sizeof(p));
build_tree(, , n); nowplace = ; bnum = ;
for (int i = ; i < n; ++i)
{
int x, y, z; scanf("%d%d%d", &x, &y, &z);
addbian(x, y); c[i] = z;
}
dfs_1(, , );
dfs_2(, , );
for (int i = ; i < n; ++i)
chan(, , n, w[bianp[i]], c[i]);
char s[];
while (scanf("%s", s) != EOF)
{
if (s[] == 'D') break;
int x, y; scanf("%d%d", &x, &y);
if (s[] == 'Q') printf("%d\n", find(x, y));
else if (s[] == 'C') chan(, , n, w[bianp[x]], y);
else if (s[]=='N') neg(x, y);
}
}
return ;
}
上一篇:封装jQuery Validate扩展验证方法


下一篇:.Net插件编程模型:MEF和MAF[转载]