HYSBZ 1036(树的统计Count)
题目链接:传送门
题目大意:中文题,略
题目思路:树链剖分裸题。
闲谈:树链越练越熟练了
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 30005
#define maxn 30010
typedef pair<int,int> PII;
typedef long long LL;
LL read(){
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,flag,L,R;
int son[N],siz[N],top[N],id[N],tid,a[N];
int dep[N],posi[N],fa[N],head[N],hcnt;
struct Node{int to,nxt,v;}node[maxn<<];
struct Seg{int sum,v;}seg[N<<];
inline void init(){
mst(head,-);tid=hcnt=;mst(siz,);mst(son,);mst(id,);
}
void dfs1(int u,int f,int deep){
dep[u]=deep,fa[u]=f,siz[u]++;
for(int i=head[u];~i;i=node[i].nxt){
int e=node[i].to;
if(e==f)continue;
dfs1(e,u,deep+);
siz[u]+=siz[e];
if(!son[u]||siz[son[u]]<siz[e])
son[u]=e;
}
}
void dfs2(int u,int tp){
top[u]=tp,id[u]=++tid,posi[tid]=u;
if(!son[u])return;dfs2(son[u],tp);
for(int i=head[u];~i;i=node[i].nxt){
int e=node[i].to;
if(!id[e])dfs2(e,e);
}
}
inline void pushup(int rt){
seg[rt].sum=seg[rt<<].sum+seg[rt<<|].sum;
seg[rt].v=max(seg[rt<<].v,seg[rt<<|].v);
}
void build(int rt,int l,int r){
if(l==r){seg[rt].v=seg[rt].sum=a[posi[l]];return;}
int mid=l+r>>;
build(lson);build(rson);
pushup(rt);
}
void update(int rt,int l,int r,int v){
if(l==r){seg[rt].v=seg[rt].sum=v;return;}
int mid=l+r>>;
if(L<=mid)update(lson,v);
else update(rson,v);
pushup(rt);
}
int query(int rt,int l,int r){
if(L<=l&&r<=R){if(flag==)return seg[rt].v;return seg[rt].sum;}
int mid=l+r>>;
int temp;
if(flag){
temp=;
if(L<=mid)temp+=query(lson);
if(R>mid) temp+=query(rson);
}
else{
temp=-inf;
if(L<=mid)temp=max(temp,query(lson));
if(R>mid) temp=max(temp,query(rson));
}
return temp;
}
void lca(int x,int y,int fl){
flag=fl;
if(fl){
int res=;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
L=id[top[x]],R=id[x];
res+=query(,,n);
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
L=id[x],R=id[y];
res+=query(,,n);
printf("%d\n",res);
}
else{
int res=-inf;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
L=id[top[x]],R=id[x];
res=max(res,query(,,n));
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
L=id[x],R=id[y];
res=max(res,query(,,n));
printf("%d\n",res);
}
}
int main(){
int i,j,group,x,y,v;
while(scanf("%d",&n)!=EOF){
init();
for(i=;i<n;++i){
x=read(),y=read();
node[hcnt].to=y,node[hcnt].nxt=head[x],head[x]=hcnt++;
node[hcnt].to=x,node[hcnt].nxt=head[y],head[y]=hcnt++;
}
for(i=;i<=n;++i)a[i]=read();
dfs1(,,);dfs2(,);build(,,n);
m=read();
char str[];
while(m--){
scanf("%s",str);x=read(),y=read();
if(strcmp(str,"QMAX")==) lca(x,y,);
else if(strcmp(str,"QSUM")==) lca(x,y,);
else L=id[x],update(,,n,y);
}
}
return ;
}
HYSBZ 1036(树的统计Count)的更多相关文章
- HYSBZ 1036 树的统计Count(树链剖分)题解
思路: 树链剖分,不知道说什么...我连模板都不会用 代码: #include<map> #include<ctime> #include<cmath> #incl ...
- HYSBZ 1036 树的统计Count (水题树链剖分)
题意:中文题. 析:就是直接维护一个最大值和一个和,用线段树维护即可,这个题很简单,但是我卡了一晚上,就是在定位的时候,位置直接反过来了,但是样例全过了...真是... 代码如下: #pragma c ...
- BZOJ 1036 树的统计Count 树链剖分模板题
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1036 题目大意: 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将 ...
- [置顶] bzoj 1036 树的统计Count 点权值模板
树链剖分 点权型可做模板,链路剖分的思想把点hash到线段树的上,然后可通过n*(log(n)*log(n))的复杂度在树上操作,在线段树上能操作的在链路上都能操作. #include<cstd ...
- bzoj 1036 树的统计Count
题目大意: 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u ...
- BZOJ - 1036 树的统计Count (LCT)
LCT试炼题(代码量居然完爆树剖?) #include<bits/stdc++.h> using namespace std; ,inf=0x3f3f3f3f; ],flp[N],n,m, ...
- BZOJ - 1036 树的统计Count (树链剖分+线段树)
题目链接 #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f; ],mx[ ...
- Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)
Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树) Description 一棵树上有n个节点,编号分别 ...
- BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14302 Solved: 5779[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MB Submit: 14354 Solved: 5802 [Subm ...
随机推荐
- 使用Open Live Writer写博客
1. 下载安装软件 安装包路径http://openlivewriter.org/ 2.配置 打开软件后会提示你配置博客账号地址 3.安装代码高亮插件 下载插件源代码https://pan.baidu ...
- bootstrap-fileinput文件上传组件和laravel引用(未完)
前言:之前的三篇介绍了下bootstrap table的一些常见用法,发现博主对这种扁平化的风格有点着迷了.前两天做一个excel导入的功能,前端使用原始的input type='file'这种标签, ...
- (Xilinx)FPGA中LVDS差分高速传输的实现
https://wenku.baidu.com/view/24e8bad86f1aff00bed51ef8.html
- RIP动态路由的配置
RIP其实相对比会比静态路由会简单的多,只需要使用rip命令添加邻居的网络号即可. 命令: Router(config)#ip route rip Router(config-router)#netw ...
- Eclipse4.4 安装java反编译插件Eclipse Class Decompiler
一.在线安装方式: Eclipse Class Decompiler整合了眼下最好的2个Java反编译工具Jad和JD-Core,而且和Eclipse Class Viewer无缝集成.可以非常方便的 ...
- Unix系统编程()改变信号处置:signal
Unix系统提供了两种方法来改变信号处置:signal和sigaction.这篇描述的是signal系统调用,是设置信号处理的原始API,所提供的接口比sigaction简单.另一方面,sigacti ...
- groupBox和panel
private void Form1_Load(object sender, EventArgs e) { groupBox1.Text = "信息表"; panel1.Borde ...
- HTML5标准最终来了,看什么书学习最好??????
近期看了一本书<HTML5网页开发实例具体解释>,是大众点评的攻城狮写的,认为非常有收获.看样子眼下大多数的国内网页都支持HTML5了,全栈project师是不是必须得会HTML5? 有兴 ...
- 抹掉Scala的糖衣(14) -- Update Method
欢迎关注我的新博客地址:http://cuipengfei.me/ 在Scala中,名字叫做update的方法是有特殊作用的. 比如: 1 2 3 val scores = new scala.col ...
- html 处理
近期做了一个后台管理网站,后台页面都是Html页面,里面再通过ajax访问后台服务.要做到比较好的用户体验,即:如果用户没有登录或没有权限马上调到登录页面,而不是等到页面加载后再ajax时判断是否登录 ...