BNUOJ39566 Do use segment tree (树链剖分+维护区间最大连续和)

Do use segment tree

Given a tree with n (1 ≤ n ≤ 200,000) nodes and a list of q (1 ≤ q ≤ 100,000) queries, process the queries in order and output a value for each output query. The given tree is connected and each node on the tree has a weight wi (-10,000 ≤ wi ≤ 10,000).

Each query consists of a number ti (ti = 1, 2), which indicates the type of the query , and three numbers ai, bi and ci (1 ≤ ai, bi ≤ n, -10,000 ≤ ci ≤ 10,000). Depending on the query type, process one of the followings:

(ti = 1: modification query) Change the weights of all nodes on the shortest path between ai and bi (both inclusive) to ci.

(ti = 2: output query) First, create a list of weights on the shortest path between ai and bi (both inclusive) in order. After that, output the maximum sum of a non-empty continuous subsequence of the weights on the list. ci is ignored for output queries.

Input

The first line contains two integers n and q. On the second line, there are n integers which indicate w1, w2, … , wn.

Each of the following n - 1 lines consists of two integers si and ei (1 ≤ si, ei ≤ n), which means that there is an edge between si and ei.

Finally the following q lines give the list of queries, each of which contains four integers in the format described above. Queries must be processed one by one from top to bottom.

Output

For each output query, output the maximum sum in one line.

Sample Input 1

3 4
1 2 3
1 2
2 3
2 1 3 0
1 2 2 -4
2 1 3 0
2 2 2 0

Output for the Sample Input 1

6
3
-4

Sample Input 2

7 5
-8 5 5 5 5 5 5
1 2
2 3
1 4
4 5
1 6
6 7
2 3 7 0
2 5 2 0
2 4 3 0
1 1 1 -1
2 3 7 0

Output for the Sample Input 2

12
10
10
19

Sample Input 3

21 30
10 0 -10 -8 5 -5 -4 -3 1 -2 8 -1 -7 2 7 6 -9 -6 3 4 9
10 3
3 2
3 12
12 4
4 13
4 9
10 21
21 1
1 11
11 14
1 15
10 6
6 17
6 16
6 5
5 18
5 19
10 7
10 8
8 20
1 1 21 -10
1 3 19 10
2 1 13 0
1 4 18 8
1 5 17 -5
2 16 7 0
1 6 16 5
1 7 15 4
2 4 20 0
1 8 14 3
1 9 13 -1
2 9 18 0
1 10 12 2
1 11 11 -8
2 21 15 0
1 12 10 1
1 13 9 7
2 6 14 0
1 14 8 -2
1 15 7 -7
2 10 2 0
1 16 6 -6
1 17 5 9
2 12 17 0
1 18 4 6
1 19 3 -3
2 11 8 0
1 20 2 -4
1 21 1 -9
2 5 19 0

Output for the Sample Input 3

20
9
29
27
10
12
1
18
-2
-3

  • 树链剖分转化为线段树;
  • 线段树上维护最大前缀和,最大后缀和,最大区间连续和,区间和。

    剩下部分就随便搞搞。

  • 代码 36 ms 31040 KB 4521 B 2015-10-11 00:45:56

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 200000 + 10;

//tree
int head[maxn], to[maxn<<1], nxt[maxn<<1], tot = 0, sz = 0;

void addedge(int _u, int _v){
    to[++tot] = _v; nxt[tot] = head[_u]; head[_u] = tot;
}

//divide tree
int siz[maxn], fa[maxn], dep[maxn];

void dfs1(int u){
    siz[u] = 1;
    for(int i = head[u]; i; i = nxt[i]){
        int v = to[i];
        if (v == fa[u]) continue;
        dep[v] = dep[u] + 1;
        fa[v] = u;
        dfs1(v);
        siz[u] += siz[v];
    }
}
int pos[maxn], belong[maxn], szu[maxn];
void dfs2(int u, int f){
    int k = 0;
    pos[u] = ++sz; szu[sz] = u; belong[u]= f;
    for(int i = head[u]; i; i = nxt[i]){
        int v = to[i];
        if (dep[v] > dep[u] && siz[v] > siz[k]) k = v;
    }
    if (!k) return;
    dfs2(k, f);
    for(int i = head[u]; i; i = nxt[i]){
        int v = to[i];
        if (dep[v] > dep[u] && v != k) dfs2(v, v);
    }
}

#define Lson (k<<1)
#define Rson ((k<<1)|1)

//segment tree
struct Seg{
    int l, r, prefix, suffix, maxseq, sum, setv;
    void setVal(int val){
        int tmp = val * (r - l + 1);
        sum = tmp;
        prefix = suffix = maxseq = max(tmp, val);
        setv = val;
    }
}t[maxn<<2];

Seg operator + (Seg a, Seg b){
    if (a.maxseq == -inf) return b;
    if (b.maxseq == -inf) return a;

    Seg re;

    re.sum = a.sum + b.sum;

    re.prefix = max(a.prefix, a.sum + b.prefix);

    re.suffix = max(b.suffix, a.suffix + b.sum);

    re.maxseq = max(max(a.maxseq, b.maxseq), a.suffix + b.prefix);

    return re;
}

int w[maxn];

void pushup(int k){
    t[k].sum = t[Lson].sum + t[Rson].sum;

    t[k].prefix = max(t[Lson].prefix, t[Lson].sum + t[Rson].prefix);

    t[k].suffix = max(t[Rson].suffix, t[Lson].suffix + t[Rson].sum);

    t[k].maxseq = max(max(t[Lson].maxseq, t[Rson].maxseq), t[Lson].suffix + t[Rson].prefix);
}

void build(int k, int l, int r){
    t[k].setv = inf;
    t[k].l = l; t[k].r = r;
    if (l==r){
        t[k].prefix = t[k].suffix = t[k].maxseq = t[k].sum = w[szu[l]];
        return;
    }
    int mid = (l+r)>>1;
    build(Lson, l, mid);
    build(Rson, mid+1, r);
    pushup(k);
}

void pushdown(int k){
    if (t[k].setv != inf){
        t[Lson].setVal(t[k].setv);
        t[Rson].setVal(t[k].setv);

        t[k].setv = inf;
    }
}

void change(int k, int x, int y, int val){
    int l = t[k].l, r = t[k].r, mid = (l+r)>>1;
    if (x <= l && r <= y){
        t[k].setVal(val);
        return;
    }
    pushdown(k);
    if (y <= mid) change(Lson, x, y, val);
    else if (x > mid) change(Rson, x, y, val);
    else change(Lson, x, mid, val), change(Rson, mid+1, y, val);
    pushup(k);
}

void solveChange(int x, int y, int k){
    while(belong[x] != belong[y]){
        if (dep[belong[x]] < dep[belong[y]]) swap(x, y);
        change(1, pos[belong[x]], pos[x], k);
        x = fa[belong[x]];
    }
    if (dep[x] < dep[y]) swap(x, y);
    change(1, pos[y], pos[x], k);
}

Seg query(int k, int x, int y){
    int l = t[k].l, r = t[k].r, mid = (l+r)>>1;
    if (x <= l && r <= y){
        return t[k];
    }
    pushdown(k);
    if (y <= mid) return query(Lson, x, y);
    else if (x > mid) return query(Rson, x, y);
    else return query(Lson, x, mid) + query(Rson, mid+1, y);
}

void solveMax(int x, int y){
    Seg tx, ty;
    tx.maxseq = -inf;
    ty.maxseq = -inf;
    while(belong[x] != belong[y]){
        if (dep[belong[x]] > dep[belong[y]]){
            tx = query(1, pos[belong[x]], pos[x]) + tx;
            x = fa[belong[x]];
        }
        else{
            ty = query(1, pos[belong[y]], pos[y]) + ty;
            y = fa[belong[y]];
        }
    }
    if (dep[x] > dep[y]){
        tx = query(1, pos[y], pos[x]) + tx;
    }
    else{
        ty = query(1, pos[x], pos[y]) + ty;
    }
    swap(tx.prefix, tx.suffix);
    tx = tx + ty;
    printf("%d\n", tx.maxseq);
}

int main()
{
    int n, Q, op, x, y, z;
    scanf("%d%d", &n, &Q);
    for(int i = 1; i <= n; i++){
        scanf("%d", &w[i]);
    }
    for(int _i = 1; _i < n; _i++){
        scanf("%d%d", &x, &y);
        addedge(x, y);
        addedge(y, x);
    }
    dfs1((1+n)/2);
    dfs2((1+n)/2, (1+n)/2);
    build(1, 1, n);

    while(Q--){
        scanf("%d%d%d%d", &op, &x, &y, &z);
        if (op == 1){
            solveChange(x, y, z);
        }
        else{
            solveMax(x, y);
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值