bzoj1036 [ZJOI2008]树的统计Count(树链剖分)
Description
一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w。我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节点的权值和 注意:从点u到点v的路径上的节点包括u和v本身
Input
输入的第一行为一个整数n,表示节点的个数。接下来n – 1行,每行2个整数a和b,表示节点a和节点b之间有一条边相连。接下来n行,每行一个整数,第i行的整数wi表示节点i的权值。接下来1行,为一个整数q,表示操作的总数。接下来q行,每行一个操作,以“CHANGE u t”或者“QMAX u v”或者“QSUM u v”的形式给出。
对于100%的数据,保证1<=n<=30000,0<=q<=200000;中途操作中保证每个节点的权值w在-30000到30000之间。
Output
对于每个“QMAX”或者“QSUM”的操作,每行输出一个整数表示要求输出的结果。
Sample Input
1 2
2 3
4 1
4 2 1 3
12
QMAX 3 4
QMAX 3 3
QMAX 3 2
QMAX 2 3
QSUM 3 4
QSUM 2 1
CHANGE 1 5
QMAX 3 4
CHANGE 3 6
QMAX 3 4
QMAX 2 4
QSUM 3 4
Sample Output
1
2
2
10
6
5
6
5
16
program rrr(input,output);
const
inf=;
type
treetype=record
l,r,max,sum:longint;
end;
etype=record
t,next:longint;
end;
var
a:array[..]of treetype;
b,c,father,siz,son,ss,idx,head,q,dep:array[..]of longint;
e:array[..]of etype;
n,m,i,j,x,y,cnt,h,t,ans:longint;
s:string;
function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end;
procedure add(x,y:longint);
begin
inc(cnt);e[cnt].t:=y;e[cnt].next:=c[x];c[x]:=cnt;
end;
procedure work;
begin
j:=pos(' ',s);val(copy(s,,j-),x);delete(s,,j);val(s,y);
end;
procedure prepare;
begin
h:=;t:=;q[]:=;father[]:=;dep[]:=;
while h<t do
begin
inc(h);
i:=c[q[h]];
while i<> do
begin
if e[i].t<>father[q[h]] then
begin
father[e[i].t]:=q[h];dep[e[i].t]:=dep[q[h]]+;
inc(t);q[t]:=e[i].t;
end;
i:=e[i].next;
end;
end;
fillchar(son,sizeof(son),);fillchar(ss,sizeof(ss),);
for i:= to n do siz[i]:=;
for i:=n downto do
begin
inc(siz[father[q[i]]],siz[q[i]]);
if siz[q[i]]>ss[father[q[i]]] then
begin ss[father[q[i]]]:=siz[q[i]];son[father[q[i]]]:=q[i]; end;
end;
cnt:=;fillchar(idx,sizeof(idx),);
for i:= to n do
if idx[q[i]]= then
begin
j:=q[i];
while j<> do begin inc(cnt);idx[j]:=cnt;head[j]:=q[i];j:=son[j]; end;
end;
end;
procedure build(k,l,r:longint);
var
mid,i:longint;
begin
a[k].l:=l;a[k].r:=r;
if l=r then begin a[k].max:=b[l];a[k].sum:=b[l];exit; end;
mid:=(l+r)>>;i:=k+k;
build(i,l,mid);
build(i+,mid+,r);
a[k].max:=max(a[i].max,a[i+].max);
a[k].sum:=a[i].sum+a[i+].sum;
end;
procedure change(k,x,y:longint);
var
mid,i:longint;
begin
if a[k].l=a[k].r then begin a[k].sum:=y;a[k].max:=y;exit end;
mid:=(a[k].l+a[k].r)>>;i:=k+k;
if x<=mid then change(i,x,y) else change(i+,x,y);
a[k].max:=max(a[i].max,a[i+].max);
a[k].sum:=a[i].sum+a[i+].sum;
end;
function askmax(k,x,y:longint):longint;
var
mid,ans:longint;
begin
if (x<=a[k].l) and (a[k].r<=y) then exit(a[k].max);
mid:=(a[k].l+a[k].r)>>;
ans:=-inf;
if x<=mid then ans:=askmax(k+k,x,y);
if mid<y then ans:=max(ans,askmax(k+k+,x,y));
exit(ans);
end;
function asksum(k,x,y:longint):longint;
var
mid,ans:longint;
begin
if (x<=a[k].l) and (a[k].r<=y) then exit(a[k].sum);
mid:=(a[k].l+a[k].r)>>;
ans:=;
if x<=mid then ans:=asksum(k+k,x,y);
if mid<y then ans:=ans+asksum(k+k+,x,y);
exit(ans);
end;
procedure qmax;
begin
ans:=-inf;
while head[x]<>head[y] do
if dep[head[x]]>dep[head[y]] then
begin
ans:=max(ans,askmax(,idx[head[x]],idx[x]));
x:=father[head[x]];
end
else begin
ans:=max(ans,askmax(,idx[head[y]],idx[y]));
y:=father[head[y]];
end;
if dep[x]>dep[y] then ans:=max(ans,askmax(,idx[y],idx[x]))
else ans:=max(ans,askmax(,idx[x],idx[y]));
end;
procedure qsum;
begin
ans:=;
while head[x]<>head[y] do
if dep[head[x]]>dep[head[y]] then
begin
ans:=ans+asksum(,idx[head[x]],idx[x]);
x:=father[head[x]];
end
else begin
ans:=ans+asksum(,idx[head[y]],idx[y]);
y:=father[head[y]];
end;
if dep[x]>dep[y] then ans:=ans+asksum(,idx[y],idx[x])
else ans:=ans+asksum(,idx[x],idx[y]);
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(n);
fillchar(c,sizeof(c),);cnt:=;
for i:= to n- do begin readln(x,y);add(x,y);add(y,x); end;
prepare;
for i:= to n do read(b[idx[i]]);
build(,,n);
readln(m);
for i:= to m do
begin
readln(s);
if s[]='C' then begin delete(s,,);work;change(,idx[x],y); end
else if s[]='M' then begin delete(s,,);work;qmax;writeln(ans); end
else begin delete(s,,);work;qsum;writeln(ans); end;
end;
close(input);close(output);
end.
然后是是子树也在一起的代码:
program rrr(input,output);
const
inf=;
type
treetype=record
l,r,max,sum:longint;
end;
etype=record
t,next:longint;
end;
var
a:array[..]of treetype;
b,c,father,siz,son,ss,idx,head,q,dep,ot:array[..]of longint;
e:array[..]of etype;
n,m,i,j,x,y,cnt,h,t,ans:longint;
s:string;
function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end;
procedure add(x,y:longint);
begin
inc(cnt);e[cnt].t:=y;e[cnt].next:=c[x];c[x]:=cnt;
end;
procedure work;
begin
j:=pos(' ',s);val(copy(s,,j-),x);delete(s,,j);val(s,y);
end;
procedure prepare;
begin
h:=;t:=;q[]:=;father[]:=;dep[]:=;
while h<t do
begin
inc(h);
i:=c[q[h]];
while i<> do
begin
if e[i].t<>father[q[h]] then
begin
father[e[i].t]:=q[h];dep[e[i].t]:=dep[q[h]]+;
inc(t);q[t]:=e[i].t;
end;
i:=e[i].next;
end;
end;
fillchar(son,sizeof(son),);fillchar(ss,sizeof(ss),);
for i:= to n do siz[i]:=;
for i:=n downto do
begin
inc(siz[father[q[i]]],siz[q[i]]);
if siz[q[i]]>ss[father[q[i]]] then
begin ss[father[q[i]]]:=siz[q[i]];son[father[q[i]]]:=q[i]; end;
end;
fillchar(idx,sizeof(idx),);ot[]:=;
for i:= to n do
if idx[q[i]]= then
begin
cnt:=ot[father[q[i]]];j:=q[i];
while j<> do
begin
head[j]:=q[i];
inc(cnt);idx[j]:=cnt;
ot[j]:=cnt;inc(ot[father[j]],siz[j]);
j:=son[j];
end;
end;
end;
procedure build(k,l,r:longint);
var
mid,i:longint;
begin
a[k].l:=l;a[k].r:=r;
if l=r then begin a[k].max:=b[l];a[k].sum:=b[l];exit; end;
mid:=(l+r)>>;i:=k+k;
build(i,l,mid);
build(i+,mid+,r);
a[k].max:=max(a[i].max,a[i+].max);
a[k].sum:=a[i].sum+a[i+].sum;
end;
procedure change(k,x,y:longint);
var
mid,i:longint;
begin
if a[k].l=a[k].r then begin a[k].sum:=y;a[k].max:=y;exit end;
mid:=(a[k].l+a[k].r)>>;i:=k+k;
if x<=mid then change(i,x,y) else change(i+,x,y);
a[k].max:=max(a[i].max,a[i+].max);
a[k].sum:=a[i].sum+a[i+].sum;
end;
function askmax(k,x,y:longint):longint;
var
mid,ans:longint;
begin
if (x<=a[k].l) and (a[k].r<=y) then exit(a[k].max);
mid:=(a[k].l+a[k].r)>>;
ans:=-inf;
if x<=mid then ans:=askmax(k+k,x,y);
if mid<y then ans:=max(ans,askmax(k+k+,x,y));
exit(ans);
end;
function asksum(k,x,y:longint):longint;
var
mid,ans:longint;
begin
if (x<=a[k].l) and (a[k].r<=y) then exit(a[k].sum);
mid:=(a[k].l+a[k].r)>>;
ans:=;
if x<=mid then ans:=asksum(k+k,x,y);
if mid<y then ans:=ans+asksum(k+k+,x,y);
exit(ans);
end;
procedure qmax;
begin
ans:=-inf;
while head[x]<>head[y] do
if dep[head[x]]>dep[head[y]] then
begin
ans:=max(ans,askmax(,idx[head[x]],idx[x]));
x:=father[head[x]];
end
else begin
ans:=max(ans,askmax(,idx[head[y]],idx[y]));
y:=father[head[y]];
end;
if dep[x]>dep[y] then ans:=max(ans,askmax(,idx[y],idx[x]))
else ans:=max(ans,askmax(,idx[x],idx[y]));
end;
procedure qsum;
begin
ans:=;
while head[x]<>head[y] do
if dep[head[x]]>dep[head[y]] then
begin
ans:=ans+asksum(,idx[head[x]],idx[x]);
x:=father[head[x]];
end
else begin
ans:=ans+asksum(,idx[head[y]],idx[y]);
y:=father[head[y]];
end;
if dep[x]>dep[y] then ans:=ans+asksum(,idx[y],idx[x])
else ans:=ans+asksum(,idx[x],idx[y]);
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(n);
fillchar(c,sizeof(c),);cnt:=;
for i:= to n- do begin readln(x,y);add(x,y);add(y,x); end;
prepare;
for i:= to n do read(b[idx[i]]);
build(,,n);
readln(m);
for i:= to m do
begin
readln(s);
if s[]='C' then begin delete(s,,);work;change(,idx[x],y); end
else if s[]='M' then begin delete(s,,);work;qmax;writeln(ans); end
else begin delete(s,,);work;qsum;writeln(ans); end;
end;
close(input);close(output);
end.
其实大部分都是一模一样的,只是预处理不一样。
bzoj1036 [ZJOI2008]树的统计Count(树链剖分)的更多相关文章
- 【BZOJ1036】[ZJOI2008]树的统计Count 树链剖分
[BZOJ1036][ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. ...
- bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题
[ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...
- BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14302 Solved: 5779[Submit ...
- Bzoj 1036: [ZJOI2008]树的统计Count 树链剖分,LCT
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 11102 Solved: 4490[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count( 树链剖分 )
树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...
- bzoj 1036: [ZJOI2008]树的统计Count 树链剖分+线段树
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 16294 Solved: 6645[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14982 Solved: 6081[Submit ...
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分)(线段树单点修改)
[ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14968 Solved: 6079[Submit][Stat ...
- Cogs 1688. [ZJOI2008]树的统计Count(树链剖分+线段树||LCT)
[ZJOI2008]树的统计Count ★★★ 输入文件:bzoj_1036.in 输出文件:bzoj_1036.out 简单对比 时间限制:5 s 内存限制:162 MB [题目描述] 一棵树上有n ...
- BZOJ1036 [ZJOI2008]树的统计Count 树链剖分
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1036 题意概括 一个树,每个节点有一个权值.3种操作. 1:修改某一个节点的权值. 2:询问某两个 ...
随机推荐
- font:12px/1.5 tahoma, arial, \5b8b\4f53, sans-serif详解
在phpcms v9的样式表文件reset.css中有如下一段样式,具体什么意思?代码如下:body,html,input{font:12px/1.5 tahoma,arial,\5b8b\4f53, ...
- 【日常训练】Help Victoria the Wise(Codeforces 99C)
题意与分析 这题意思是这样的:在正方体的六面镶嵌给定颜色的宝石(相同颜色不区分),然后问最多有几种彼此不等价(即各种旋转过后看起来一致)的方案. 其实可以乱搞,因为范围只有720.求出全排列,然后每个 ...
- linux 查询管道过滤,带上标题字段
linux查询过滤, 带上标题字段例: 一个简单的查询 ps -e | grep httpd 上面经过grep 过滤后, 标题没了, 但是为了看上去更方便,有标题字段看起来更方便一些, 那么可以按下面 ...
- Java+Netty实现的RESTful框架--netty-rest-server
在工作中用Netty做了几个服务,感觉Netty做出来的程序性能好,资源占用少,但是实现Http服务比较麻烦,于是就参考Spring MVC的注解基于Netty实现了一个轻量级的RESTful框架. ...
- 什么是mvc模式
MVC是一个架构,或者说是一个设计模式,它就是强制性使应用程序的输入,处理和输出分开.将一个应用程序分为三个部分:Model,View,Controller. 原理图: 分析: Model 模型(完成 ...
- Bitcoin Core P2P网络层
目录 数据结构 节点发现和节点连接 地址管理 节点发现 节点连接 插口(Sockets)和消息 Socket线程 (net.cpp) 消息线程 ProcessMessages (net_process ...
- XAMPP安装PHP_GMP
CentOS 6.4 Xampp 7.1.12 下载PHP7.1.12的源码包 yum install gmp-devel yum install m4 .tar.xz cd /root/php-/e ...
- [SimHash] the Hash-based Similarity Detection Algorithm
The current information explosion has resulted in an increasing number of applications that need to ...
- Appengine直接下载文件并保存到google drive
一直对下载文件比较感兴趣.前些日子无意搜到google 推出一项服务,可以直接将文件下载到google drive中,原型猛戳这里,但有限额限制.一时脑洞大开,可不可以在appengine 上架设服务 ...
- paste命令详解
基础命令学习目录首页 原文链接:https://blog.csdn.net/u011341352/article/details/52806312 个人分类: linux paste命令和cut命 ...