题目链接:戳我

树链剖分。

注意一点就是维护最大值的时候最好写成下面代码里那个样子,要不然会因为可能左右区间没有的问题有奇奇怪怪的锅。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100010
using namespace std;
int n,m,t,tot;
int a[MAXN],wt[MAXN],head[MAXN],sum[MAXN],maxx[MAXN];
int son[MAXN],siz[MAXN],fa[MAXN],id[MAXN],dep[MAXN],top[MAXN];
char cur[10];
struct Edge{int nxt,to;}edge[MAXN<<1];
inline void add(int from,int to){edge[++t].nxt=head[from];edge[t].to=to;head[from]=t;}
inline void dfs1(int now,int pre)
{
siz[now]=1;
fa[now]=pre;
dep[now]=dep[pre]+1;
int maxx=-1;
for(int i=head[now];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(v==pre) continue;
dfs1(v,now);
siz[now]+=siz[v];
if(siz[v]>maxx) maxx=siz[v],son[now]=v;
}
}
inline void dfs2(int now,int topf)
{
id[now]=++tot;
top[now]=topf;
wt[tot]=a[now];
if(son[now]) dfs2(son[now],topf);
for(int i=head[now];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(v==fa[now]||v==son[now]) continue;
dfs2(v,v);
}
}
inline int ls(int x){return x<<1;}
inline int rs(int x){return x<<1|1;}
inline void push_up(int x)
{
sum[x]=sum[ls(x)]+sum[rs(x)];
maxx[x]=max(maxx[ls(x)],maxx[rs(x)]);
}
inline void build(int x,int l,int r)
{
if(l==r) {sum[x]=wt[l],maxx[x]=wt[l];return;}
int mid=(l+r)>>1;
build(ls(x),l,mid);
build(rs(x),mid+1,r);
push_up(x);
}
inline void update(int x,int l,int r,int pos,int k)
{
if(l==r) {sum[x]=k,maxx[x]=k;return;}
int mid=(l+r)>>1;
if(pos<=mid) update(ls(x),l,mid,pos,k);
else update(rs(x),mid+1,r,pos,k);
push_up(x);
}
inline int query_sum(int x,int l,int r,int ll,int rr)
{
if(ll<=l&&r<=rr) return sum[x];
int mid=(l+r)>>1;
int cur_ans=0;
if(ll<=mid) cur_ans+=query_sum(ls(x),l,mid,ll,rr);
if(mid<rr) cur_ans+=query_sum(rs(x),mid+1,r,ll,rr);
return cur_ans;
}
inline int query_max(int x,int l,int r,int ll,int rr)
{
if(ll<=l&&r<=rr) return maxx[x];
int mid=(l+r)>>1;
int ans=-2147483647;
if(ll<=mid) ans=max(ans,query_max(ls(x),l,mid,ll,rr));
if(mid<rr) ans=max(ans,query_max(rs(x),mid+1,r,ll,rr));
return ans;
}
inline int solve_max(int x,int y)
{
int maxx=(int)-1e9;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
maxx=max(maxx,query_max(1,1,n,id[top[x]],id[x]));
x=fa[top[x]];
}
if(dep[x]<dep[y]) swap(x,y);
maxx=max(maxx,query_max(1,1,n,id[y],id[x]));
return maxx;
}
inline int solve_sum(int x,int y)
{
int cur_ans=0;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
cur_ans+=query_sum(1,1,n,id[top[x]],id[x]);
x=fa[top[x]];
}
if(dep[x]<dep[y]) swap(x,y);
cur_ans+=query_sum(1,1,n,id[y],id[x]);
return cur_ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
dfs1(1,1);
dfs2(1,1);
build(1,1,n);
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%s%d%d",cur,&x,&y);
if(cur[1]=='M') printf("%d\n",solve_max(x,y));
else if(cur[1]=='S') printf("%d\n",solve_sum(x,y));
else update(1,1,n,id[x],y);
}
return 0;
}

[ZJOI2008] 树的统计Count的更多相关文章

  1. BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14302  Solved: 5779[Submit ...

  2. bzoj1036 [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 12646  Solved: 5085 [Subm ...

  3. BZOJ 1036: [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 14354  Solved: 5802 [Subm ...

  4. 【BZOJ1036】[ZJOI2008]树的统计Count 树链剖分

    [BZOJ1036][ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. ...

  5. bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 10677  Solved: 4313[Submit ...

  6. Bzoj 1036: [ZJOI2008]树的统计Count 树链剖分,LCT

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 11102  Solved: 4490[Submit ...

  7. 数据结构(LCT动态树):BZOJ 1036: [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 12266  Solved: 4945[Submit ...

  8. BZOJ 1036: [ZJOI2008]树的统计Count( 树链剖分 )

    树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...

  9. 1036: [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7496  Solved: 3078[Submit] ...

  10. bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题

    [ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...

随机推荐

  1. Unity 单例

    1. 继承于MonoBehaviour(不随着场景切换而销毁) 基类代码: using System.Collections; using System.Collections.Generic; us ...

  2. IO操作中的建议

    程序输出信息使用PrintStream(或者PrintWriter),程序输入信息使用Scaner

  3. Cookie&Session会话跟踪技术

    今日内容学习目标 可以响应给浏览器Cookie信息[response.addCookie()] 可以接受浏览器Cookie信息[request.getCookies()] [创建cookie,设置pa ...

  4. 服务器意外重启导致storm报错的问题处理

    解决方法 cat /opt/storm-0.8.2/conf/storm.yaml中找到storm.local.dir设定的目录,备份supervisor和workers两个文件夹,#nohup su ...

  5. 【UVA11212 算法竞赛入门经典】 Editing a Book 【IDA*】

    题意 你有一篇由n(2<=n<=9)个自然段组成的文章,希望将它们排列成1,2,···,n.可以用剪切和粘贴来完成任务.每次可以剪切一段连续的自然段,粘贴时按照顺序粘贴.注意剪贴板只有一个 ...

  6. ubuntu18 tensorflow cpu fast_rcnn

    (flappbird) luo@luo-All-Series:~/MyFile/TensorflowProject/tf-faster-rcnn/lib$ makepython setup.py bu ...

  7. Selenium自动化测试WebDriver下载

    1. 所有版本chrome下载 是不是很难找到老版本的chrome?博主收集了几个下载chrome老版本的网站,其中哪个下载的是原版的就不得而知了. http://www.slimjet.com/ch ...

  8. 基于weui的一个小插件

    移动端项目当中大量的使用了weui,为了减少工作量,方便修改,自己写了个小插件,暂时只有toast和dialog部分,可能会更新actionSheet等其他部分 更新一个手机端预览的二维码,就直接放项 ...

  9. Mysql中自定义函数编程

    1.语法 1.1 新建函数 Create function function_name(参数列表) returns 返回值类型 函数体 (1)函数名,应该合法的标识符,并且不应该与已有的关键字冲突. ...

  10. Spring思维导图(IOC篇)

    写在前面 写过java的都知道:所有的对象都必须创建:或者说:使用对象之前必须先创建.而使用ioc之后,你就可以不再手动创建对象,而是从ioc容器中直接获取对象. 就好像我们无需考虑对象的销毁回收一样 ...