树剖裸题

#include <iostream>
#include <cstdio>
using namespace std;
int n, uu, vv, hea[30005], ecnt, w[30005], ww[30005], dep[30005], cnt;
int idx[30005], son[30005], fa[30005], top[30005], q, wt[30005], siz[30005];
char ss[15];
struct Edge{
int too, nxt;
}edge[60005];
void add_edge(int fro, int too){
edge[++ecnt].nxt = hea[fro];
edge[ecnt].too = too;
hea[fro] = ecnt;
}
struct SGT{
int sum[120005];
int zdz[120005];
void build(int o, int l, int r){
if(l==r) zdz[o] = sum[o] = wt[l];
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
zdz[lson] = zdz[rson] = -0x3f3f3f3f;
if(l<=mid) build(lson, l, mid);
if(mid<r) build(rson, mid+1, r);
sum[o] = sum[lson] + sum[rson];
zdz[o] = max(zdz[lson], zdz[rson]);
}
}
void change(int o, int l, int r, int x, int k){
if(l>=x && r<=x) sum[o] = zdz[o] = k;
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(x<=mid) change(lson, l, mid, x, k);
if(mid<x) change(rson, mid+1, r, x, k);
sum[o] = sum[lson] + sum[rson];
zdz[o] = max(zdz[lson], zdz[rson]);
}
}
int querySum(int o, int l, int r, int x, int y){
if(l>=x && r<=y) return sum[o];
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
int ans=0;
if(x<=mid) ans += querySum(lson, l, mid, x, y);
if(mid<y) ans += querySum(rson, mid+1, r, x, y);
return ans;
}
}
int queryZdz(int o, int l, int r, int x, int y){
if(l>=x && r<=y) return zdz[o];
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
int ans=-0x3f3f3f3f;
if(x<=mid) ans = max(ans, queryZdz(lson, l, mid, x, y));
if(mid<y) ans = max(ans, queryZdz(rson, mid+1, r, x, y));
return ans;
}
}
}sgt;
void dfs1(int x, int f){
fa[x] = f;
dep[x] = dep[f] + 1;
siz[x] = 1;
int maxSon=-1;
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t==f) continue;
dfs1(t, x);
siz[x] += siz[t];
if(maxSon<siz[t]){
maxSon = siz[t];
son[x] = t;
}
}
}
void dfs2(int x, int topf){
top[x] = topf;
idx[x] = ++cnt;
wt[cnt] = w[x];
if(!son[x]) return ;
dfs2(son[x], topf);
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t==fa[x] || t==son[x]) continue;
dfs2(t, t);
}
}
int queryRangeSum(int uu, int vv){
int ans=0;
while(top[uu]!=top[vv]){
if(dep[top[uu]]<dep[top[vv]]) swap(uu, vv);
ans += sgt.querySum(1, 1, n, idx[top[uu]], idx[uu]);
uu = fa[top[uu]];
}
if(dep[uu]>dep[vv]) swap(uu, vv);
ans += sgt.querySum(1, 1, n, idx[uu], idx[vv]);
return ans;
}
int queryRangeZdz(int uu, int vv){
int ans=-0x3f3f3f3f;
while(top[uu]!=top[vv]){
if(dep[top[uu]]<dep[top[vv]]) swap(uu, vv);
ans = max(ans, sgt.queryZdz(1, 1, n, idx[top[uu]], idx[uu]));
uu = fa[top[uu]];
}
if(dep[uu]>dep[vv]) swap(uu, vv);
ans = max(ans, sgt.queryZdz(1, 1, n, idx[uu], idx[vv]));
return ans;
}
int main(){
cin>>n;
for(int i=1; i<n; i++){
scanf("%d %d", &uu, &vv);
add_edge(uu, vv);
add_edge(vv, uu);
}
for(int i=1; i<=n; i++)
scanf("%d", &w[i]);
dep[1] = 1;
dfs1(1, 0);
dfs2(1, 1);
sgt.build(1, 1, n);
cin>>q;
while(q--){
scanf("%s %d %d", ss, &uu, &vv);
if(ss[1]=='H') sgt.change(1, 1, n, idx[uu], vv);
if(ss[1]=='M') printf("%d\n", queryRangeZdz(uu, vv));
if(ss[1]=='S') printf("%d\n", queryRangeSum(uu, vv));
}
return 0;
}

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

  1. BZOJ1036:[ZJOI2008]树的统计——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1036 题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来 ...

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

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

  3. bzoj1036 [ZJOI2008]树的统计Count

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 公司项目git开发流程规范

    手动修改冲突之后,git add . git commit ,git push

  2. css继承性

    不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...

  3. 在使用seek()函数时,有时候会报错为 “io.UnsupportedOperation: can't do nonzero cur-relative seeks”,代码如下:

    __author__ = 'ZHHT' #!/usr/bin/env python # -*- coding:utf-8 -*- import os f = open("test1" ...

  4. 分享eclipse自动生成java注释方法

    设置方法介绍: eclipse中:Windows->Preferences->Java->Code Style->Code Template->Comments,然后对应 ...

  5. $'\r': command not found 或者 syntax error: unexpected end of file 或者 syntax error near unexpected token `$'\r''

    执行shell脚本如果报如下错误: syntax error near unexpected token `$'\r'' syntax error: unexpected end of file $' ...

  6. Python3+Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )

    !/usr/bin/env python -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )'''from selenium im ...

  7. 如何给SAP Cloud Connector Region列表中添加新的Region

    SAP help里提供了CloudFoundry和Neo环境下可用的Region和API endpoint: 当我们期望用SAP Cloud Connector连接某个SAP云平台Region时,一般 ...

  8. 第008课_第1个ARM落版程序及引申

    form:第008课_第1个ARM裸板程序及引申 第001节_辅线1_硬件知识_LED原理图 当我们学习C语言的时候,我们会写个Hello程序.那当我们下ARM程序,也该有一个简单的程序引领我们入门, ...

  9. 类库日期和jsp导包

    一.日期类库 1.1. Date Date类创建一个时间,或者是创建一个与你计算机当前的时间:精确到毫秒. //实例化时间类 Date date = new Date(); 1.2.格式转换类 1.2 ...

  10. 【转】EM算法原理

    EM是我一直想深入学习的算法之一,第一次听说是在NLP课中的HMM那一节,为了解决HMM的参数估计问题,使用了EM算法.在之后的MT中的词对齐中也用到了.在Mitchell的书中也提到EM可以用于贝叶 ...