Tree 【POJ - 3237】【树链剖分+一些特殊的处理】

106 篇文章 0 订阅
24 篇文章 0 订阅

题目链接


  这道题,说来还的确困扰了我一个多小时,当时就在想,我该如何处理那些边权(我将边化为点)以及点(默认权值为0)的取相反数后的处理(因为点取相反数之后还是0),会困扰到那些边的。

  然后,我想到了,如果这段区间的返回的值为0,那么就说明了肯定是点的,我们不妨返回的是(-INF)即可。然后,再pushup()的时候,也做了相似的处理,毕竟,两点之间确定一条边,所以,我们线段树的操作的时候,如果,左儿子或者右儿子为0,我们取另外一头,绝对是可以的,于是,就这样子敲了,A了!

  最后,附上一组测试数组,那个“-48”过了,基本问题不大(我就是一直卡在“-48”),过了,就没其他问题了。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 40000007
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 20005;
int N, all_point, W[maxN], cnt, head[maxN], deep[maxN], root[maxN], siz[maxN], W_son[maxN], top[maxN], id[maxN], new_W[maxN], num, tree[maxN<<2], minn[maxN<<2], lazy[maxN<<2];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN<<1];
void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void dfs1(int u, int fa, int depth)
{
    root[u] = fa;
    deep[u] = depth;
    siz[u] = 1;
    int Minn = -1;
    for(int i=head[u]; i!=-1; i=edge[i].nex)
    {
        int v = edge[i].to;
        if(v == fa) continue;
        dfs1(v, u, depth+1);
        siz[u] += siz[v];
        if(siz[v] > Minn)
        {
            Minn = siz[v];
            W_son[u] = v;
        }
    }
}
inline void dfs2(int u, int topf)
{
    top[u] = topf;
    id[u] = ++num;
    new_W[num] = W[u];
    if(W_son[u] == 0) return;
    dfs2(W_son[u], topf);
    for(int i=head[u]; i!=-1; i=edge[i].nex)
    {
        int v = edge[i].to;
        if(v == root[u] || v == W_son[u]) continue;
        dfs2(v, v);
    }
}
void pushup(int rt)
{
    if(tree[rt<<1] == 0)
    {
        tree[rt] = tree[rt<<1|1];
        minn[rt] = minn[rt<<1|1];
        return;
    }
    if(tree[rt<<1|1] == 0)
    {
        tree[rt] = tree[rt<<1];
        minn[rt] = minn[rt<<1];
        return;
    }
    tree[rt] = max(tree[rt<<1], tree[rt<<1|1]);
    minn[rt] = min(minn[rt<<1], minn[rt<<1|1]);
}
inline void buildTree(int rt, int l, int r)
{
    lazy[rt] = 0;
    if(l == r)
    {
        tree[rt] = minn[rt] = new_W[l];
        return;
    }
    int mid = (l + r)>>1;
    buildTree(rt<<1, l, mid);
    buildTree(rt<<1|1, mid+1, r);
    pushup(rt);
}
void pushdown(int rt)
{
    if(lazy[rt])
    {
        lazy[rt<<1] ^= 1;
        lazy[rt<<1|1] ^= 1;
        int t1 = tree[rt<<1], t2 = tree[rt<<1|1];
        tree[rt<<1] = -minn[rt<<1];
        minn[rt<<1] = -t1;
        tree[rt<<1|1] = -minn[rt<<1|1];
        minn[rt<<1|1] = -t2;
        lazy[rt] = 0;
    }
}
void update_1(int rt, int l, int r, int qx, int val)
{
    if(l == r)
    {
        tree[rt] = minn[rt] = val;
        return;
    }
    pushdown(rt);
    int mid = (l + r)>>1;
    if(qx<=mid) update_1(rt<<1, l, mid, qx, val);
    else update_1(rt<<1|1, mid+1, r, qx, val);
    pushup(rt);
}
void update_2(int rt, int l, int r, int ql, int qr)
{
    if(ql<=l && qr>=r)
    {
        lazy[rt] ^= 1;
        int tmp = tree[rt];
        tree[rt] = -minn[rt];
        minn[rt] = -tmp;
        return;
    }
    pushdown(rt);
    int mid = (l + r)>>1;
    if(ql>mid) update_2(rt<<1|1, mid+1, r, ql, qr);
    else if(qr<=mid) update_2(rt<<1, l, mid, ql, qr);
    else
    {
        update_2(rt<<1, l, mid, ql, qr);
        update_2(rt<<1|1, mid+1, r, ql, qr);
    }
    pushup(rt);
}
void change_line(int pos, int val) { update_1(1, 1, all_point, id[pos + N], val); }
void Negate_line(int x, int y)
{
    while(top[x] != top[y])
    {
        if(deep[top[x]] < deep[top[y]]) swap(x, y);
        update_2(1, 1, all_point, id[top[x]], id[x]);
        x = root[top[x]];
    }
    if(deep[x] > deep[y]) swap(x, y);
    update_2(1, 1, all_point, id[x], id[y]);
}
int query(int rt, int l, int r, int ql, int qr)
{
    if(ql<=l && qr>=r) return tree[rt] == 0?(-INF):tree[rt];
    pushdown(rt);
    int mid = (l + r)>>1;
    if(ql > mid) return query(rt<<1|1, mid+1, r, ql, qr);
    else if(qr <= mid) return query(rt<<1, l, mid, ql, qr);
    else return max(query(rt<<1, l, mid, ql, qr), query(rt<<1|1, mid+1, r, ql, qr));
}
int query_range(int x, int y)
{
    int ans = -INF;
    while(top[x] != top[y])
    {
        if(deep[top[x]] < deep[top[y]]) swap(x, y);
        ans = max(ans, query(1, 1, all_point, id[top[x]], id[x]));
        x = root[top[x]];
    }
    if(deep[x] > deep[y]) swap(x, y);
    ans = max(ans, query(1, 1, all_point, id[x], id[y]));
    return ans;
}
inline void init()
{
    cnt = num = 0;
    memset(W, 0, sizeof(W));
    memset(W_son, 0, sizeof(W_son));
    memset(head, -1, sizeof(head));
}
char op[10];
int main()
{
    int T;  scanf("%d", &T);
    while(T--)
    {
        init();
        scanf("%d", &N);
        all_point = N;
        for(int i=1; i<N; i++)
        {
            int e1, e2, e3;
            scanf("%d%d%d", &e1, &e2, &e3);
            W[++all_point] = e3;
            addEddge(e1, all_point);
            addEddge(all_point, e1);
            addEddge(all_point, e2);
            addEddge(e2, all_point);
        }
        dfs1(1, 1, 0);
        dfs2(1, 1);
        buildTree(1, 1, all_point);
        while(scanf("%s", op) && op[0] != 'D')
        {
            int e1, e2;
            scanf("%d%d", &e1, &e2);
            if(op[0] == 'C') change_line(e1, e2);
            else if(op[0] == 'N') Negate_line(e1, e2);
            else printf("%d\n", query_range(e1, e2));
        }
    }
    return 0;
}
/*
1
15
7 1 93
6 2 41
6 1 48
14 4 62
12 15 43
7 8 2
2 10 80
7 13 92
13 5 91
13 11 5
15 1 79
2 3 21
9 12 7
15 4 33
Q 13 7
C 6 6
N 13 1
Q 3 15
C 12 11
N 14 11
Q 2 11
N 2 7
Q 6 7
C 11 10
N 3 8
Q 14 8
C 3 4
Q 8 13
C 5 1
N 7 1
Q 1 11
C 11 8
DONE
 ans:
 > 92
 > 79
 > 93
 > -48
 > 93
 > 92
 > 92
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值