SPOJ GSS7 Can you answer these queries VII
2015-10-17 11:01
417 查看
Can you answer these queries VII
Time Limit: 1000msMemory Limit: 262144KB
This problem will be judged on SPOJ. Original ID: GSS7
64-bit integer IO format: %lld Java class name: Main
Given a tree with N ( N<=100000 ) nodes. Each node has a interger value x_i ( |x_i|<=10000 ).
You have to apply Q ( Q<=100000 ) operations:
1. 1 a b : answer the maximum contiguous sum (maybe empty,will always larger than or equal to 0 ) from the path a->b ( inclusive ).
2. 2 a b c : change all value in the path a->b ( inclusive ) to c.
Input
first line consists one interger N.next line consists N interger x_i.
next N-1 line , each consists two interger u,v , means that node u and node v are connected
next line consists 1 interger Q.
next Q line : 1 a b or 2 a b c .
Output
For each query, output one line the maximum contiguous sum.Example
Input: 5
-3 -2 1 2 3
1 2
2 3
1 4
4 5
3
1 2 5
2 3 4 2
1 2 5 Output: 5
9
Source
my own problems解题:LCT
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; struct arc{ int to,next; arc(int x = 0,int y = -1){ to = x; next = y; } }e[maxn<<1]; struct LCT{ int fa[maxn],ch[maxn][2],sz[maxn],parent[maxn]; int key[maxn],ls[maxn],rs[maxn],ms[maxn],sum[maxn]; bool reset[maxn]; inline void pushup(int x){ sz[x] = 1 + sz[ch[x][0]] + sz[ch[x][1]]; sum[x] = key[x] + sum[ch[x][0]] + sum[ch[x][1]]; ls[x] = max(ls[ch[x][0]],sum[ch[x][0]] + key[x] + ls[ch[x][1]]); rs[x] = max(rs[ch[x][1]],sum[ch[x][1]] + key[x] + rs[ch[x][0]]); ms[x] = max(ms[ch[x][0]],ms[ch[x][1]]); ms[x] = max(ms[x],key[x] + rs[ch[x][0]] + ls[ch[x][1]]); } inline void set(int x,int val){ if(!x) return; reset[x] = true; key[x] = val; sum[x] = sz[x]*val; ls[x] = rs[x] = ms[x] = max(0,sum[x]); } inline void pushdown(int x){ if(reset[x]){ set(ch[x][0],key[x]); set(ch[x][1],key[x]); reset[x] = false; } } void rotate(int x,int kd){ int y = fa[x]; pushdown(y); pushdown(x); ch[y][kd^1] = ch[x][kd]; fa[ch[x][kd]] = y; fa[x] = fa[y]; ch[x][kd] = y; fa[y] = x; if(fa[x]) ch[fa[x]][y == ch[fa[x]][1]] = x; pushup(y); } void splay(int x,int goal = 0){ pushdown(x); int y = x; while(fa[y]) y = fa[y]; if(x != y){ parent[x] = parent[y]; parent[y] = 0; while(fa[x] != goal){ pushdown(fa[fa[x]]); pushdown(fa[x]); pushdown(x); if(fa[fa[x]] == goal) rotate(x,x == ch[fa[x]][0]); else{ int y = fa[x],z = fa[y],s = (ch[z][0] == y); if(x == ch[y][s]){ rotate(x,s^1); rotate(x,s); }else{ rotate(y,s); rotate(x,s); } } } pushup(x); } } void access(int x){ for(int y = 0; x; x = parent[x]){ splay(x); fa[ch[x][1]] = 0; parent[ch[x][1]] = x; ch[x][1] = y; fa[y] = x; parent[y] = 0; y = x; pushup(x); } } void update(int x,int y,int z){ access(y); for(y = 0; x; x = parent[x]){ splay(x); if(!parent[x]){ key[x] = z; set(y,z); set(ch[x][1],z); return; } fa[ch[x][1]] = 0; parent[ch[x][1]] = x; ch[x][1] = y; fa[y] = x; parent[y] = 0; y = x; pushup(x); } } int query(int x,int y){ access(y); for(y = 0; x; x = parent[x]){ splay(x); if(!parent[x]){ int ret = max(ms[y],ms[ch[x][1]]); ret = max(ret,key[x] + ls[y] + ls[ch[x][1]]); return ret; } fa[ch[x][1]] = 0; parent[ch[x][1]] = x; ch[x][1] = y; fa[y] = x; parent[y] = 0; y = x; pushup(x); } } void init(){ memset(fa,0,sizeof fa); memset(parent,0,sizeof parent); memset(reset,false,sizeof reset); memset(ch,0,sizeof ch); sz[0] = 0; } }lct; int head[maxn],tot; void add(int u,int v){ e[tot] = arc(v,head[u]); head[u] = tot++; } void dfs(int u,int fa){ for(int i = head[u]; ~i; i = e[i].next){ if(e[i].to == fa) continue; lct.parent[e[i].to] = u; dfs(e[i].to,u); } } int main(){ int n,m,u,v,x,y,z,op; while(~scanf("%d",&n)){ memset(head,-1,sizeof head); tot = 0; lct.init(); for(int i = 1; i <= n; ++i){ lct.sz[i] = 1; scanf("%d",&lct.key[i]); lct.sum[i] = lct.key[i]; lct.ls[i] = lct.rs[i] = lct.ms[i] = max(0,lct.key[i]); } for(int i = 1; i < n; ++i){ scanf("%d%d",&u,&v); add(u,v); add(v,u); } dfs(1,1); scanf("%d",&m); while(m--){ scanf("%d%d%d",&op,&x,&y); if(op == 1) printf("%d\n",lct.query(x,y)); else{ scanf("%d\n",&z); lct.update(x,y,z); } } } return 0; }
View Code
相关文章推荐
- 利用运行时设置UITextField 光标颜色
- iOS界面编程-UIButton
- Difference between Key, Primary Key, Unique Key and Index in MySQL
- JAVA基础学习day23--GUI基础
- 精致的外观Request
- UITableViewCell的背景设置
- 自定义UILable 监听事件
- Displaying Bitmaps Efficiently (二)-----Processing Bitmaps Off the UI Thread
- SQL UNIQUE 约束 高级教程
- juicer语法
- *LeetCode-Unique Word Abbreviation
- iOS开发-UI控件:如何画1像素的线
- 【IOS 开发学习总结-OC-52】UITableViewDelegate详解
- 优化UITableViewCell高度计算
- getGuid()
- *LeetCode-Closest Binary Search Tree Value
- Modern Operating System --- Power Management (OS Issues)
- Modern Operating System --- Power Management (Hardware Issues)
- Ueditor实现自定义conttoller请求或跨域请求
- 利用ueditor的文件上传功能上传文件到外部服务器的地址&&&&&字符统计功能重写