Do use segment tree

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://www.bnuoj.com/v3/problem_show.php?pid=39566

Description

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 aibi 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 w1w2, ... , 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

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

Sample Output

6
3
-4

HINT

题意

给你一棵树,然后查询一条链上,区间连续最大和

然后区间更新两个操作

题解:

树链剖分+线段树

1.树链剖分 要bfs,不然会爆栈

2.如果wa了,可以尝试开ll试试

3.线段树,注意保存从左边开始的最大值,从右边开始最大值,这个区间的最大值,这个区间和

4.建议用lca写

//////////////////////////////

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 2e5 + ;
const long long inf = 1LL << ;
typedef pair<long long,long long>dl;
const long long check = -(1LL << ); typedef long long SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum , Lv , Rv , setv , maxv;
void updata(SgTreeDataType v)
{
setv = v ;
sum = (R-L+)*v;
maxv = (v > ) ? (R-L+)*v : v;
Rv = Lv = maxv;
}
}; struct QueryData
{
long long MaxValue , Left , Right ,sum;
QueryData(long long MaxValue = ,long long Left = ,long long Right = ,long long sum = ) :MaxValue(MaxValue) , Left(Left) , Right(Right) , sum(sum){}
}; treenode tree[maxn * ]; inline void push_down(int o)
{
if(tree[o].setv == inf) return ;
SgTreeDataType lazyval = tree[o].setv;
tree[*o].updata(lazyval) ; tree[*o+].updata(lazyval);
tree[o].setv = inf;
} inline void push_up(int o)
{
tree[o].sum = tree[*o].sum + tree[*o+].sum;
tree[o].Rv = max(tree[o*+].Rv,tree[o*+].sum + tree[o*].Rv);
tree[o].Lv = max(tree[o*].Lv,tree[o*].sum + tree[o*+].Lv);
tree[o].maxv = max( max(tree[o*].maxv , tree[o*+].maxv) , max( tree[o*].Rv + tree[o*+].Lv ,max(tree[o*].sum + tree[o*+].Lv , tree[o*+].sum + tree[o*].Rv)) );
} inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = , tree[o].setv = inf , tree[o].maxv = tree[o].Lv = tree[o].Rv = ;
if (R > L)
{
int mid = (L+R) >> ;
build_tree(L,mid,o*);
build_tree(mid+,R,o*+);
}
} inline void updata(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].updata(v);
else
{
push_down(o);
int mid = (L+R)>>;
if (QL <= mid) updata(QL,QR,v,o*);
if (QR > mid) updata(QL,QR,v,o*+);
push_up(o);
}
} inline QueryData QueryMax(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return QueryData(tree[o].maxv,tree[o].Lv,tree[o].Rv,tree[o].sum);
else
{
int mid = (L+R)>>;
push_down(o);
QueryData res;
if(QL<=mid && QR <= mid) res = QueryMax(QL,QR,*o);
else if(QL>mid&&QR>mid) res = QueryMax(QL,QR,*o+);
else
{
QueryData Lv = QueryMax(QL,QR,*o);
QueryData Rv = QueryMax(QL,QR,*o+);
res.MaxValue = max( max(Lv.MaxValue,Rv.MaxValue) , Lv.Right+Rv.Left );
res.Left=Lv.Left,res.Right=Rv.Right;res.sum=Lv.sum+Rv.sum;
res.Left=max(res.Left,Lv.sum+Rv.Left);
res.Right=max(res.Right,Rv.sum+Lv.Right);
}
push_up(o);
return res;
}
} inline dl QueryLeftMax(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if(QL<=L && R <= QR) return make_pair(tree[o].Lv,tree[o].sum);
else
{
push_down(o);
int mid = (L+R) >> ;
dl result;
if(QL > mid) result = QueryLeftMax(QL,QR,o*+);
else if(QR <= mid) result = QueryLeftMax(QL,QR,o*);
else
{
dl LL = QueryLeftMax(QL,QR,o*);
dl RR = QueryLeftMax(QL,QR,o*+);
long long sum = LL.second + RR.second;
long long Lval = max(LL.first , LL.second + RR.first);
result = make_pair(Lval,sum);
}
push_up(o);
return result;
}
} inline dl QueryRightMax(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if(QL<=L && R <= QR) return make_pair(tree[o].Rv,tree[o].sum);
else
{
push_down(o);
int mid = (L+R) >> ;
dl result;
if(QL > mid) result = QueryRightMax(QL,QR,o*+);
else if(QR <= mid) result = QueryRightMax(QL,QR,o*);
else
{
dl LL = QueryRightMax(QL,QR,o*);
dl RR = QueryRightMax(QL,QR,o*+);
long long sum = LL.second + RR.second;
long long Rval = max(RR.first , RR.second + LL.first);
result = make_pair(Rval,sum);
}
push_up(o);
return result;
}
} long long QuerySum(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if(QL <= L && R <= QR) return tree[o].sum;
else
{
int mid = (L+R) >> ;
long long res = ;
push_down(o);
if(QL <= mid) res += QuerySum(QL,QR,*o);
if(QR > mid) res += QuerySum(QL,QR,*o+);
push_up(o);
return res;
}
} vector<int>G[maxn];
int n , q ,val[maxn] , son[maxn] , idx[maxn] , top[maxn] , deep[maxn], fa[maxn] , head[maxn],T=; void test()
{
n = ;
build_tree( , n , );
for(int i = ; i <= n ; ++ i) updata( i , i , i , );
updata(,,-,);
QueryData res;
dl BB;
res = QueryMax(,n,);
BB = QueryLeftMax(,n,);
cout << res.MaxValue << endl;
cout << BB.first << endl;
} //******** int dfs_clock;
int que[maxn*],num[maxn],iii[maxn],b[maxn]; void build_List()
{
int ft = , rear = ;
que[rear++] = ;
fa[] = ;
deep[] = ;
while(ft < rear)
{
int u = que[ft++];
for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if(v == fa[u]) continue;
fa[v] = u;
que[rear++] = v;
deep[v] = deep[u]+;
}
}
memset(num, , sizeof (num));
for(int i = n-; i >= ; i--)
{
int u = que[i];
num[u]++;
num[fa[u]] += num[u];
}
for(int i = ; i <= n; i++)
{
for(int j = ; j < G[i].size(); j++) if(G[i][j] != fa[i])
if(G[i][] == fa[i] || num[G[i][j]] > num[G[i][]])
swap(G[i][], G[i][j]);
}
top[] = ;
for(int i = ; i < n; i++)
{
int u = que[i];
if(G[fa[u]][] == u) top[u] = top[fa[u]];
else top[u] = u;
}
memset(iii, , sizeof (iii));
ft = ;
dfs_clock = ;
que[++ft] = ;
idx[] = ++dfs_clock;
b[] = val[];
while(ft)
{
int u = que[ft];
if(iii[u] >= G[u].size()) ft--;
else if(G[u][iii[u]] == fa[u]) iii[u]++;
else
{
int v = G[u][iii[u]];
que[++ft] = v;
idx[v] = ++dfs_clock;
b[idx[v]] = val[v];
iii[u]++;
}
}
for(int i = ; i <= n ; ++ i) updata(i , i , b[i] , );
} //********* void my_updata(int u , int v , int c)
{
int f1 = top[u] , f2 = top[v];
while(f1 != f2)
{
if(deep[f1] < deep[f2]) swap(f1,f2) , swap(u,v);
updata(idx[top[u]],idx[u],c,);
u = top[u] , u = fa[u] , f1 = top[u];
}
if(deep[u] > deep[v]) swap(u,v);
updata(idx[u] , idx[v] , c , );
} long long solve(int u ,int v)
{
int f1 = top[u] , f2 = top[v];
if(u == v) return QueryMax(idx[u],idx[u],).MaxValue;
long long s[];s[] = s[] = check;
int cur = ;
long long ans=check;
while(f1 != f2)
{
if(deep[f1] < deep[f2]) swap(f1,f2) , swap(u,v) , cur ^= ;
long long sum = QuerySum(idx[top[u]],idx[u],);
long long tt = QueryMax(idx[top[u]],idx[u],).MaxValue;
ans = max(ans , tt);
tt = QueryRightMax(idx[top[u]],idx[u],).first;
ans = max(ans ,tt);
ans = max(ans ,s[cur] + tt);
tt = QueryLeftMax(idx[top[u]],idx[u],).first;
s[cur] = max(sum + s[cur],tt);
ans = max(ans , s[cur]);
u = top[u] , u = fa[u] , f1 = top[u];
}
if(deep[u] > deep[v]) swap(u,v) , cur ^= ;
ans = max(ans , QueryMax(idx[u],idx[v],).MaxValue);
if(s[cur^] != (check)) ans = max( ans , s[cur^] + QueryRightMax(idx[u],idx[v],).first);
if(s[cur] != (check)) ans = max( ans , s[cur] + QueryLeftMax(idx[u],idx[v],).first);
if(s[cur] != (check) && s[cur^] != (check)) ans = max( ans , QuerySum(idx[u],idx[v],) + s[] + s[]);
return ans;
} inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} int main(int argc,char * argv[])
{
scanf("%d%d",&n,&q);
for(int i = ; i <= n ; ++ i) scanf("%d",val+i);
for(int i = ; i < n ; ++ i)
{
int u , v;scanf("%d%d",&u,&v);
G[u].push_back(v);G[v].push_back(u);
}
build_tree(,n,);
build_List();
while(q--)
{
int x,y,z,w;scanf("%d%d%d%d",&x,&y,&z,&w);
if(x == ) my_updata(y,z,w);
else printf("%lld\n",solve(y,z));
}
return ;
}

Aizu 2450 Do use segment tree 树链剖分+线段树的更多相关文章

  1. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  2. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

  3. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  4. Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  5. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  6. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  7. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  8. B20J_3231_[SDOI2014]旅行_树链剖分+线段树

    B20J_3231_[SDOI2014]旅行_树链剖分+线段树 题意: S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,城市信仰不同的宗教,为了方便,我们用不同的正整数代表各种宗教. S国 ...

  9. BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )

    BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...

随机推荐

  1. 正确认识Android的内存管理机制,合理关闭进程 (一)

    随着大家收货后会有很多乐粉晒内存,为啦方便大家,在网上搜集了一些相关Andriod管理的相关机制合理管理内存,整理下发个贴. 首先要知道Android系统是基于Linux 2.6内核开发的开源操作系统 ...

  2. eclipse 点击 open Implementation就退出eclipse

    输入法不对.. 切换到纯英文的输入法. 微软自带的那个..  我电脑上也这样. 现在好了 (安装谷歌输入法貌似存在这个问题)

  3. 反编译.net dll

    自己公司的程序,年代久了,没有源代码,修改一些小地方,只能反编译,还好当时没有混淆. 先ildasm 反编译. 删除   .publickey = ( ) 这段,去原来签名. 然后再用找要改的IL,这 ...

  4. 逆序对的相关问题:bzoj1831,bzoj2431

    先从简单一点的bzoj2431入手: n个数1~n已经限定了,所以 对于1~i-1,新加入i,最多可以增加i-1个逆序对,最少增加0个逆序对 f[i,j]表示1~i形成的序列逆序对为j的方案数 比较容 ...

  5. UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.

    题意: 这道题和POJ 3090很相似,求|x|≤a,|y|≤b 中站在原点可见的整点的个数K,所有的整点个数为N(除去原点),求K/N 分析: 坐标轴上有四个可见的点,因为每个象限可见的点数都是一样 ...

  6. Android样式——Styles

    说明 样式(style)是属性的集合,用来指定View或者Window的外观和格式. 这些属性可以是height(高度).padding(内边距).font size(字体颜色)等. 样式定义在另一个 ...

  7. MFC中状态栏显示鼠标坐标位置

    原文:MFC中状态栏显示鼠标坐标位置,蝈蝈 1,利用MFC向导创建一个应用工程ewq. 2,打开ResourceView,右击Menu菜单,插入Menu,在空白处双击,Caption中填入Point. ...

  8. HDU 1251-统计难题(Trie)

    题意: 给一组单词 开始提问每次给一个串求该串是上面几个单词的前缀 分析: 没给数据规模,但用链表写ME好几次,又用数组写开小RE了,试了几次才过了,真是醉了... #include <map& ...

  9. iOS7程序后台运行

    介绍 这次 iOS7 对程序后台运行进行了加强,但是仅仅是加强而已,要想像 Android 程序那样自由当然就别想了,苹果这么做主要还是出于电池使用时间考虑,但是这次的加强对大部分程序基本够用. 在介 ...

  10. 关于在MDK中使用 printf 函数

    microlib 提供了一个有限的 stdio 子系统,它仅支持未缓冲的 stdin.stdout 和 stderr. 这样,即可使用 printf() 来显示应用程序中的诊断消息. 要使用高级 I/ ...