poj 3237(树链剖分+线段树)

poj 3237

树剖+线段树。
刚开始想用记录该区域被NEGATE了几次,结果发现不可行,翻别人博客发现了原来维护最大值$maxv$和最小值$minv$,NEGATE就是$maxv=-minv, minv=maxv$, 正确性显然。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i, j, k) for (i=(j);i<=(k);i++)
#define fd(i, k, j) for (i=(k);i>=(j);i--)
#define fe(i, u) for (i=head[u];i!=-1;i=e[i].next)
#define rd(a) scanf("%d", &a)
#define rd2(a, b) scanf("%d%d", &a, &b)
#define rd3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define ms(i, j) memset(i, j, sizeof i)
#define FN2 "poj3237" 
using namespace std;

const int MAXN = 10000 + 5, INF = 200000000;

struct data{int to, next;}e[MAXN*2];
int n, wi[MAXN], ai[MAXN], bi[MAXN], head[MAXN], cnt;
int p[MAXN], top[MAXN], son[MAXN], dep[MAXN], fa[MAXN], siz[MAXN];
int pre;
void dfs1(int u, int pa) {
    int i;
    dep[u] = dep[pa] + 1, fa[u] = pa, siz[u] = 1;
    fe (i, u) {
        int v = e[i].to;
        if (v!=pa) {
            dfs1(v, u);
            siz[u] += siz[v];
            if (son[u]==-1||siz[son[u]]<siz[v]) son[u] = v;
        }
    }
}
void dfs2(int u, int chain) {
    int i;
    p[u] = ++pre, top[u] = chain;
    if (son[u]!=-1) {
        dfs2(son[u], chain);
        fe (i, u) {
            int v = e[i].to;
            if (v!=fa[u]&&v!=son[u]) dfs2(v, v);
        }
    }
}
#define lc (o<<1)
#define rc (o<<1|1)
#define M ((l+r)>>1)
int maxv[MAXN*4], minv[MAXN*4], lazy[MAXN*4];
void proc(int &a, int &b) {int t = a; a = -b, b = -t;}
void pushup(int o) {
    maxv[o] = max(maxv[lc], maxv[rc]);
    minv[o] = min(minv[lc], minv[rc]);
}
void pushdown(int o, int l, int r) {
    if (l==r) return ;
    if (lazy[o]) {
        lazy[lc] ^= 1, lazy[rc] ^= 1;
        proc(maxv[lc], minv[lc]), proc(maxv[rc], minv[rc]);
        lazy[o] = 0;
    }
}
void update(int o, int l, int r, int p, int v) {
    pushdown(o, l, r);
    if (l==r) {
        maxv[o] = minv[o] = v;
        return ;
    }
    if (p<=M) update(lc,l,M,p,v); else if (M<p) update(rc,M+1,r,p,v);
    pushup(o);
}
void ngt(int o, int l, int r, int x, int y) {
    pushdown(o, l, r);
    if (x<=l&&r<=y) {
        proc(maxv[o], minv[o]);
        lazy[o] = 1;
        return ;
    }
    if (x<=M) ngt(lc,l,M,x,y); 
    if (M<y)  ngt(rc,M+1,r,x,y);
    pushup(o);
}
int query(int o, int l, int r, int x, int y) {
    pushdown(o, l, r);
    int ret = -INF;
    if (x<=l&&r<=y) return maxv[o];
    if (x<=M) ret=max(ret,query(lc,l,M,x,y)); 
    if (M<y)  ret=max(ret,query(rc,M+1,r,x,y));
    return ret;
}
void tongt(int u, int v) {
    int f1 = top[u], f2 = top[v];
    while (f1!=f2) {
        if (dep[f1]<dep[f2]) swap(f1, f2), swap(u, v);
        ngt(1,1,n,p[f1], p[u]);
        u = fa[f1], f1 = top[u];
    }
    if (dep[u]<dep[v]) swap(u, v);
    ngt(1,1,n,p[v]+1, p[u]);
}
int toget(int u, int v) {
    int f1 = top[u], f2 = top[v], ret = -INF;
    while (f1!=f2) {
        if (dep[f1]<dep[f2]) swap(f1, f2), swap(u, v);
        ret = max(ret, query(1,1,n,p[f1], p[u]));
        u = fa[f1], f1 = top[u];
    }
    if (dep[u]<dep[v]) swap(u, v);
    return max(ret, query(1,1,n,p[v]+1, p[u]));
}
void ins(int u, int v) {
    cnt++; e[cnt].to = v, e[cnt].next = head[u], head[u] = cnt;
    cnt++; e[cnt].to = u, e[cnt].next = head[v], head[v] = cnt;
}
void init() {
    int i; rd(n); cnt = pre = 0;
    fo (i, 1, n) head[i] = -1, p[i] = 0, top[i] = 0, son[i] = -1, dep[i] = 0, fa[i] = 0, siz[i] = 0;
    fo (i, 1, n*2) e[i].to = 0, e[i].next = -1;
    fo (i, 1, n*4) maxv[i] = -INF, minv[i] = INF, lazy[i] = 0;
    fo (i, 1, n-1) {
        rd3(ai[i], bi[i], wi[i]);
        ins(ai[i],bi[i]);
    }
}
void solve() {
    int i;
    dfs1(1, 0), dfs2(1, 1);
    fo (i, 1, n-1) {
        int a1 = ai[i], b1 = bi[i];
        if (dep[a1]>dep[b1]) update(1,1,n, p[a1], wi[i]);
        if (dep[a1]<dep[b1]) update(1,1,n, p[b1], wi[i]);
    }
    char ch[10];
    while (1) {
        scanf("%s", ch);
        if (ch[0]=='D') break;
        if (ch[0]=='C') {
            int x, v; rd2(x, v);
            int a1 = ai[x], b1 = bi[x];
            if (dep[a1]>dep[b1]) update(1,1,n, p[a1], v);
            if (dep[a1]<dep[b1]) update(1,1,n, p[b1], v);
        }
        if (ch[0]=='N') {
            int u, v; rd2(u, v);
            tongt(u, v);
        } 
        if (ch[0]=='Q') {
            int u, v; rd2(u, v);
            printf("%d\n", toget(u, v));
        } 
    }
}
int main() {
    #ifndef ONLINE_JUDGE
    freopen(FN2".in","r",stdin);freopen(FN2".out","w",stdout);
    #endif
    int t; rd(t);
    while (t--) init(), solve();
    return 0;
}

一个很弱的数据:

input:
1

12
1 2 6
2 3 7
3 4 1
3 5 -4
5 6 -3
1 7 2
7 8 3
7 9 6
7 10 7
9 11 -1
11 12 4
QUERY 6 12
CHANGE 2 10
QUERY 6 12
NEGATE 5 2
QUERY 6 12
CHANGE 4 10
QUERY 6 12
NEGATE 8 10
QUERY 6 12
DONE

output:
7
10
6
10
10
------ 本文结束 ------