Aizu 2450 Do use segment tree LCT
来源:互联网 发布:常州骏飞网络 编辑:程序博客网 时间:2024/05/16 14:44
Do use segment tree
Time Limit : 2 sec, Memory Limit : 256000 KBDo 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 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 41 2 31 22 32 1 3 01 2 2 -42 1 3 02 2 2 0
Output for the Sample Input 1
63-4
Sample Input 2
7 5-8 5 5 5 5 5 51 22 31 44 51 66 72 3 7 02 5 2 02 4 3 01 1 1 -12 3 7 0
Output for the Sample Input 2
12101019
Sample Input 3
21 3010 0 -10 -8 5 -5 -4 -3 1 -2 8 -1 -7 2 7 6 -9 -6 3 4 910 33 23 1212 44 134 910 2121 11 1111 141 1510 66 176 166 55 185 1910 710 88 201 1 21 -101 3 19 102 1 13 01 4 18 81 5 17 -52 16 7 01 6 16 51 7 15 42 4 20 01 8 14 31 9 13 -12 9 18 01 10 12 21 11 11 -82 21 15 01 12 10 11 13 9 72 6 14 01 14 8 -21 15 7 -72 10 2 01 16 6 -61 17 5 92 12 17 01 18 4 61 19 3 -32 11 8 01 20 2 -41 21 1 -92 5 19 0
Output for the Sample Input 3
20929271012118-2-3
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<vector>using namespace std;#define maxn 500007#define inf 1000000000#define ll intstruct 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;}
- Aizu 2450 Do use segment tree LCT
- Aizu 2450 Do use segment tree (树链剖分)
- Aoj 2450 Do use segment tree【树链剖分】
- aoj2450 Do use segment tree 树链剖分
- AOJ 2450 Do use segment tree (树链剖分 + 线段树区间合并)
- Do use segment tree 树链剖分,按顺序剖分
- bnuoj 39566 Do use segment tree(树链剖分)
- 树链剖分 JAG Summer 2012 Day 4 D Do use segment tree
- BNUOJ 39566 Do use segment tree(树链剖分——点权)
- BNUOJ39566 Do use segment tree (树链剖分+维护区间最大连续和)
- Aizu ALDS1_7_C Tree Walk
- Segment tree
- Segment Tree
- Segment Tree
- segment Tree
- Segment Tree
- Segment Tree
- hdu 5002 Tree (LCT)
- c++ primer 练习5.20
- 1037. Magic Coupon (25)
- Linux基本命令(二)
- DirectX11 聚光灯
- 马克飞象-图文教程
- Aizu 2450 Do use segment tree LCT
- AI---个人主页
- HDU 2500 做一个正气的杭电人(水~)
- 13种排序算法详解(相当清楚,还附有flash动画)
- QT5 学习之路10---Qt 模块简介
- Scala之旅-函数详解
- HDU 2502 月之数(水~)
- 【BZOJ】【P4264】【小C找朋友】【题解】【hash】
- LeetCode 066 Plus One