bzoj 5210(树链刨分下做个dp)
5210: 最大连通子块和
Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 211 Solved: 65
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
3 -2 0 3 -1
1 2
1 3
4 2
2 5
Q 1
M 4 1
Q 1
Q 2
Sample Output
3
1
- #include<bits/stdc++.h>
- #define clr(x) memset(x,0,sizeof(x))
- #define clr_1(x) memset(x,-1,sizeof(x))
- #define INF 0x3f3f3f3f
- #define LL long long
- #define pb push_back
- #define ls(i) (i<<1)
- #define rs(i) (i<<1|1)
- #define mp make_pair
- #define fi first
- #define se second
- using namespace std;
- const int N=2e5+;
- int tsize[N];
- int son[N],down[N],top[N],dep[N],pos[N],pot[N],fat[N];
- int tot;
- int n,q;
- LL v[N],f[N],g[N],maxs[N];
- vector<int> e[N];
- struct heap
- {
- priority_queue<LL> ins,del;
- inline void push(LL x)
- {
- ins.push(x);
- return ;
- }
- inline void pop(LL x)
- {
- del.push(x);
- return ;
- }
- inline LL top()
- {
- while(!del.empty() && ins.top()==del.top())
- ins.pop(),del.pop();
- if(ins.empty()) return 0LL;
- return ins.top();
- }
- void pop()
- {
- top();
- del.push(ins.top());
- return ;
- }
- }lsq[N];
- void dfs1(int u,int d,int fa)
- {
- // cout<<"begin:"<<u<<" "<<son[u]<<endl;
- dep[u]=d;
- tsize[u]=;
- fat[u]=fa;
- int sz=e[u].size(),p;
- for(int i=;i<sz;i++)
- {
- p=e[u][i];
- // cout<<"branch:"<<u<<" "<<p<<" "<<fa<<endl;
- if(p!=fa)
- {
- dfs1(p,d+,u);
- tsize[u]+=tsize[p];
- if(!son[u] || tsize[p]>tsize[son[u]])
- son[u]=p;
- }
- }
- // cout<<"endn:"<<u<<" "<<son[u]<<endl;
- return ;
- }
- void dfs2(int u,int tp)
- {
- // cout<<u<<endl;
- top[u]=tp;
- pos[u]=++tot;
- pot[tot]=u;
- if(son[u]) dfs2(son[u],tp),down[u]=down[son[u]],maxs[u]=maxs[son[u]];
- else down[u]=u,maxs[u]=;
- int sz=e[u].size();
- g[u]=v[u];
- for(int i=;i<sz;i++)
- {
- int p=e[u][i];
- if(p!=fat[u] && p!=son[u])
- {
- dfs2(p,p);
- g[u]+=f[p];
- lsq[u].push(maxs[p]);
- }
- }
- f[u]=max(0LL,g[u]+f[son[u]]),maxs[u]=max(maxs[u],f[u]),maxs[u]=max(maxs[u],lsq[u].top());
- return ;
- }
- struct segt
- {
- int l,r;
- LL all,maxl,maxr,maxn;
- segt operator + (const segt &b)
- {
- segt p;
- p.maxl=max(maxl,all+b.maxl);
- p.maxr=max(b.maxr,maxr+b.all);
- p.all=all+b.all;
- p.maxn=max(max(maxn,b.maxn),maxr+b.maxl);
- p.l=l;
- p.r=b.r;
- return p;
- }
- }seg[N<<];
- void init(int i,int l,int r)
- {
- if(l==r)
- {
- int x=pot[l];
- seg[i]=(segt){l,r,g[x],max(0LL,g[x]),max(0LL,g[x]),max(g[x],lsq[x].top())};
- return ;
- }
- int mid=l+r>>;
- init(i<<,l,mid);
- init(i<<|,mid+,r);
- seg[i]=seg[i<<]+seg[i<<|];
- return ;
- }
- char s[];
- void refresh(int i,int pos)
- {
- if(seg[i].l==seg[i].r)
- {
- int x=pot[seg[i].l];
- segt p=(segt){seg[i].l,seg[i].r,g[x],max(0LL,g[x]),max(0LL,g[x]),max(g[x],lsq[x].top())};
- seg[i]=p;
- return ;
- }
- int mid=seg[i].l+seg[i].r>>;
- if(mid>=pos) refresh(i<<,pos);
- else refresh(i<<|,pos);
- seg[i]=seg[i<<]+seg[i<<|];
- return ;
- }
- segt query(int i,int l,int r)
- {
- if(seg[i].l>=l && seg[i].r<=r) return seg[i];
- int mid=seg[i].l+seg[i].r>>;
- if(r<=mid) return query(i<<,l,r);
- if(l>mid) return query(i<<|,l,r);
- return query(i<<,l,r)+query(i<<|,l,r);
- }
- void update(int u,LL val)
- {
- segt t1,t2,t3;
- bool flag=;
- // cout<<u<<endl;
- while(u)
- {
- // cout<<u<<endl;
- t3=query(,pos[top[u]],pos[down[u]]);
- if(flag) lsq[u].pop(t1.maxn),lsq[u].push(t2.maxn);
- t1=t3;
- flag=;
- g[u]+=val,refresh(,pos[u]);
- t2=query(,pos[top[u]],pos[down[u]]);
- val=t2.maxl-f[top[u]],f[top[u]]=t2.maxl;
- u=fat[top[u]];
- }
- return ;
- }
- int main()
- {
- tot=;
- scanf("%d%d",&n,&q);
- for(int i=;i<=n;i++)
- scanf("%lld",v+i);
- for(int i=;i<=n;i++)
- {
- int u,v;
- scanf("%d%d",&u,&v);
- e[u].pb(v);
- e[v].pb(u);
- }
- dfs1(,,);
- dfs2(,);
- init(,,n);
- for(int i=;i<=q;i++)
- {
- scanf("%s",s);
- if(s[]=='M')
- {
- int u;
- LL num;
- scanf("%d%lld",&u,&num);
- update(u,num-v[u]);
- v[u]=num;
- }
- else
- {
- int u;
- scanf("%d",&u);
- printf("%lld\n",query(,pos[u],pos[down[u]]).maxn);
- }
- }
- return ;
- }
bzoj 5210(树链刨分下做个dp)的更多相关文章
- hdu 5452(树链刨分)
看到题目,想了挺长时间,发现不会,然后看着样子像是树上成段操作,所以查了下树链刨分,结果真的就是这个东西... Minimum Cut Time Limit: 3000/2000 MS (Java/O ...
- xcoj 1103 插线板(树链刨分求最大子段和)
1103: 插线板 时间限制: 1 Sec 内存限制: 128 MB提交: 14 解决: 7 标签提交统计讨论版EditTestData 题目描述 从前有一堆古老的插线板,任意两个插线板之间只有一 ...
- 树链刨分(class版)
class版树链剖(刨)分 感谢沙华大佬的赞助 其实没什么太大变化,就是用了几次一顿乱指... CODE: #include<iostream> #include<cstdio> ...
- HDU - 3966 树链刨分
题目传送门 操作就是询问某个点的值, 然后就是对一条路径上的值全部修改. 最基本的树刨题目了. 树刨的思想: 1. 对于每个点找到他的重儿子. void dfs1(int o, int u){ sz[ ...
- P3565 由简单的树形dp 引入 长链刨分
这道题感觉不太行 因为自己没想出来. 先说一下暴力吧,取三个点 让两两之间的距离相等怎么做呢,看起来是很复杂的样子的,但是仔细观察发现 答案出自一个点的儿子之间 或者儿子和父亲之间. 暴力枚举三个点然 ...
- POJ 2763 Housewife Wind 树链拋分
一.前言 这破题WA了一天,最后重构还是WA,最后通过POJ讨论版得到的数据显示,我看上去是把某个变量写错了..于是,还是低级错误背锅啊....代码能力有待进一步提升2333333 二.题意 某家庭主 ...
- HDU 3966 Aragorn's Story 树链拋分
一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...
- BZOJ 1036 && 树链剖分
还是太弱啊..各种数据结构只听过名字却没有一点概念..树链剖分也在这个范畴..今天来进一步深化一下教育改革推进全民素质提高. 性质 忘了在哪里看到的一篇blog有一句话讲得非常好,树链剖分不是一种数据 ...
- bzoj 4196 树链剖分 模板
[Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2135 Solved: 1232[Submit][Status][D ...
随机推荐
- mogodb的安装与配置
下载:https://www.mongodb.com/https://www.mongodb.com/ 安装:一直next,中间选择custom,选择自己的安装路径,最后安装成功. 配置:打开安装好的 ...
- flask基础之AppContext应用上下文和RequestContext请求上下文(六)
前言 应用上下文和请求上下文存在的目的,官方文档讲的很清楚,可参考: http://www.pythondoc.com/flask/appcontext.html 应用上下文对象在没有请求的时候是可以 ...
- httpd功能配置之虚拟主机【转】
apache默认使用80端口提供服务,使用主服务器配置的话,一台物理机只能提供一个站点服务:可以使用虚拟主机方式提供不同的访问,以实现一台主机提供多站点服务. 虚拟主机的实现方式有三种:基于端口.基于 ...
- 企业日志大数据分析系统ELK+KAFKA实现【转】
背景: 最近线上上了ELK,但是只用了一台Redis在中间作为消息队列,以减轻前端es集群的压力,Redis的集群解决方案暂时没有接触过,并且Redis作为消息队列并不是它的强项:所以最近将Redis ...
- Runtime.getRuntime().exec 类 防止阻塞
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; impor ...
- idea心得
概述 Intellij IDEA真是越用越觉得它强大,它总是在我们写代码的时候,不时给我们来个小惊喜.出于对Intellij IDEA的喜爱,我决定写一个与其相关的专栏或者系列,把一些好用的Intel ...
- pip离线安装
pip freeze > requirements.txt pip download <packages> pip install --no-index --find-links=& ...
- 用js面向对象思想封装插件
js是基于原型的面向对象语言,如果你学过java,c#等正统面向对象语言,你会难以理解js的面向对象,他和普通的面向对象不太一样,今天,我们通过封装一个toast插件,来看看js面向对象是如何运行的. ...
- YUI Compressor 压缩 JavaScript 原理-《转载》
YUI Compressor 压缩 JavaScript 的内容包括: 移除注释 移除额外的空格 细微优化 标识符替换(Identifier Replacement) YUI Compressor包括 ...
- Oracle学习笔记:ORA-22992 cannot use LOB locators selected from remote tables
通过DB_LINK访问远程表的时候出现 ORA-22992: cannot use LOB locators selected from remote tables 错误. 原因:因为表中含有clob ...