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
    评论
4S店客户管理小程序-毕业设计,基于微信小程序+SSM+MySql开发,源码+数据库+论文答辩+毕业论文+视频演示 社会的发展和科学技术的进步,互联网技术越来越受欢迎。手机也逐渐受到广大人民群众的喜爱,也逐渐进入了每个用户的使用。手机具有便利性,速度快,效率高,成本低等优点。 因此,构建符合自己要求的操作系统是非常有意义的。 本文从管理员、用户的功能要求出发,4S店客户管理系统中的功能模块主要是实现管理员服务端;首页、个人中心、用户管理、门店管理、车展管理、汽车品牌管理、新闻头条管理、预约试驾管理、我的收藏管理、系统管理,用户客户端:首页、车展、新闻头条、我的。门店客户端:首页、车展、新闻头条、我的经过认真细致的研究,精心准备和规划,最后测试成功,系统可以正常使用。分析功能调整与4S店客户管理系统实现的实际需求相结合,讨论了微信开发者技术与后台结合java语言和MySQL数据库开发4S店客户管理系统的使用。 关键字:4S店客户管理系统小程序 微信开发者 Java技术 MySQL数据库 软件的功能: 1、开发实现4S店客户管理系统的整个系统程序; 2、管理员服务端;首页、个人中心、用户管理、门店管理、车展管理、汽车品牌管理、新闻头条管理、预约试驾管理、我的收藏管理、系统管理等。 3、用户客户端:首页、车展、新闻头条、我的 4、门店客户端:首页、车展、新闻头条、我的等相应操作; 5、基础数据管理:实现系统基本信息的添加、修改及删除等操作,并且根据需求进行交流信息的查看及回复相应操作。
现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本微信小程序医院挂号预约系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此微信小程序医院挂号预约系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。微信小程序医院挂号预约系统有管理员,用户两个角色。管理员功能有个人中心,用户管理,医生信息管理,医院信息管理,科室信息管理,预约信息管理,预约取消管理,留言板,系统管理。微信小程序用户可以注册登录,查看医院信息,查看医生信息,查看公告资讯,在科室信息里面进行预约,也可以取消预约。微信小程序医院挂号预约系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值