Aizu 2450 Do use segment tree LCT
2015-10-03 09:51
489 查看
Do use segment tree
Time Limit : 2 sec, Memory Limit : 256000 KB
Do use segment tree
Given a tree with n (1≤n≤200,000) nodesand 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 numbersai, 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
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> using namespace std; #define maxn 500007 #define inf 1000000000 #define ll int struct Node{ Node *fa,*ch[2]; bool rev,root; int val,w,size; int ls,rs,al,ans; }; Node pool[maxn]; Node *nil,*tree[maxn]; int cnt = 0; void init(){ cnt = 1; nil = tree[0] = pool; nil->ch[0] = nil->ch[1] = nil; nil->val = -inf; nil->size = 0; nil->ls=nil->rs=nil->al=nil->ans=-inf; } Node *newnode(int w,Node *f){ pool[cnt].fa = f; pool[cnt].ch[0]=pool[cnt].ch[1]=nil; pool[cnt].rev = false; pool[cnt].root = true; pool[cnt].val = -inf; pool[cnt].ls=pool[cnt].rs=pool[cnt].al=pool[cnt].ans=w; pool[cnt].w = w; pool[cnt].size=1; return &pool[cnt++]; } //左右子树反转******真正把结点变为根 void update_rev(Node *x){ if(x == nil) return ; x->rev = !x->rev; swap(x->ch[0],x->ch[1]); } void update(Node*x); //splay下推信息****** void pushdown(Node *x){ if(x == nil) return ; if(x->rev != false){ update_rev(x->ch[0]); update_rev(x->ch[1]); x->rev = false; update(x); } if(x->val != -inf){ x->w = x->val; x->ch[0]->val = x->val; x->ch[1]->val = x->val; update(x->ch[0]); update(x->ch[1]); x->val = -inf; } } //splay向上更新信息****** void update(Node *x){ if(x == nil) return ; x->size = x->ch[0]->size + x->ch[1]->size + 1; if(x->val != -inf){ if(x->val >= 0){ x->ans = x->al = x->rs = x->ls = x->size * x->val; } else { x->ans = x->rs = x->ls = x->val; x->al = x->size*x->val; } return ; } Node lc=*x->ch[0],rc=*x->ch[1]; if(lc.rev) swap(lc.ls,lc.rs); if(rc.rev) swap(rc.ls,rc.rs); x->ans = x->al = x-> ls = x->rs = x->w; if(x->ch[0] != nil){ x->ans = max(x->ans,lc.ans); x->ans = max(x->ans,x->ls+lc.rs); x->rs = max(x->rs,x->al+lc.rs); x->ls = max(lc.ls,lc.al+x->ls); x->al += lc.al; } if(x->ch[1] != nil){ x->ans = max(x->ans,rc.ans); x->ans = max(x->ans,x->rs+rc.ls); x->rs = max(rc.rs,rc.al+x->rs); x->ls = max(x->ls,x->al+rc.ls); x->al += rc.al; } } //splay在root-->x的路径下推信息****** void push(Node *x){ if(!x->root) push(x->fa); pushdown(x); } //将结点x旋转至splay中父亲的位置****** void rotate(Node *x){ Node *f = x->fa, *ff = f->fa; int t = (f->ch[1] == x); if(f->root) x->root = true, f->root = false; else ff->ch[ff->ch[1] == f] = x; x->fa = ff; f->ch[t] = x->ch[t^1]; x->ch[t^1]->fa = f; x->ch[t^1] = f; f->fa = x; update(f); } //将结点x旋转至x所在splay的根位置****** void splay(Node *x){ push(x); Node *f, *ff; while(!x->root){ f = x->fa,ff = f->fa; if(!f->root) if((ff->ch[1]==f)&&(f->ch[1] == x)) rotate(f); else rotate(x); rotate(x); } update(x); } //将x到树根的路径并成一条path****** Node *access(Node *x){ Node *y = nil,*z; while(x != nil){ splay(x); x->ch[1]->root = true; (x->ch[1] = y)->root = false; update(x); y = x; x = x->fa; } return y; } //将结点x变成树根****** void be_root(Node *x){ access(x); splay(x); update_rev(x); } //将x连接到结点f上****** void link(Node *x, Node *f){ be_root(x); x->fa = f; } int main(){ int n,q,s,t,l,r; Node*x,*y,*z; while(scanf("%d%d",&n,&q)!=EOF){ init(); for(int i =1;i <= n; i++){ scanf("%d",&s); tree[i] = newnode(s,nil); } for(int i = 1;i < n; i++){ scanf("%d%d",&l,&r); link(tree[l],tree[r]); } while(q--){ scanf("%d%d%d%d",&t,&l,&r,&s); if(t==1){ be_root(tree[l]); x = access(tree[r]); x->val = s; update(x); } else { be_root(tree[l]); x = access(tree[r]); printf("%d\n",x->ans); } } } return 0; }
相关文章推荐
- [BZOJ2594][WC2006][LCT][MST]水管局长数据加强版
- SPOJ QTREE LCT
- BZOJ1180 [CROATIAN2009]OTOCI
- BZOJ 4202: 石子游戏 SG定理+LCT
- [BZOJ2959]长跑(LCT)
- [BZOJ2555]SubString(后缀自动机+LCT)
- [BZOJ3282][tree][LCT]
- [BZOJ4573][[Zjoi2016]大森林][LCT建虚点]
- [BZOJ2631][Tree][LCT]
- [BZOJ2002][[Hnoi2010]Bounce 弹飞绵羊]
- [BZOJ2049][[Sdoi2008]Cave 洞穴勘测][LCT]
- LCT模板
- Jzoj4731 游戏(待填)
- Jzoj3898 树的连通性
- 【最小生成树】【LCT】【bzoj2594】水管局长数据加强版
- 【bzoj2002】【LCT】弹飞绵羊
- 【bzoj2049】【LCT】洞穴勘测
- bzoj2049 LCT
- HDU5333 Undirected Graph
- NOI2014补全+解题报告