HYSBZ1036 树链剖分
这题我建了2棵线段树,这样来处理 最值和和值,简单的题目。
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 99999999
#define ll __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
struct node
{
int to;
int v;
int next;
}edge[MAXN*];
int pre[MAXN],ind,top[MAXN],fa[MAXN],son[MAXN],w[MAXN],deq[MAXN],siz[MAXN],fn;
int tree1[MAXN<<],tree2[MAXN<<];
int n,val[MAXN];
void add(int x,int y)
{
edge[ind].to = y;
edge[ind].next = pre[x];
pre[x] = ind++;
}
void dfs1(int rt,int pa,int d)
{
deq[rt] = d;
son[rt] = ;
fa[rt] = pa;
siz[rt] = ;
for(int i=pre[rt]; i!=-; i=edge[i].next){
int t = edge[i].to;
if(t != fa[rt]){
dfs1(t,rt,d+);
siz[rt] += siz[t];
if(siz[son[rt]] < siz[t]){
son[rt] = t;
}
}
}
}
void dfs2(int rt,int tp)
{
w[rt] = ++ fn;
top[rt] = tp;
if(son[rt] != )
dfs2(son[rt],tp);
for(int i=pre[rt]; i!=-; i=edge[i].next){
int t = edge[i].to;
if(son[rt] != t && t != fa[rt]){
dfs2(t,t);
}
}
}
void pushup1(int rt)
{
tree1[rt] = max(tree1[rt<<],tree1[rt<<|]);
}
void pushup2(int rt)
{
tree2[rt] = tree2[rt<<] + tree2[rt<<|];
}
void Insert(int p,int v,int l,int r,int rt)
{
if(l == r){
tree1[rt] = tree2[rt] = v;
return ;
}
int m = (l+r)/;
if(m >= p){
Insert(p,v,lson);
}
else {
Insert(p,v,rson);
}
pushup1(rt);
pushup2(rt);
}
void updata(int p,int v,int l,int r,int rt)
{
if(l == r){
tree1[rt] = v;
tree2[rt] = v;
return ;
}
int m = (l+r)/;
if(m >= p){
updata(p,v,lson);
}
else {
updata(p,v,rson);
}
pushup1(rt);
pushup2(rt);
}
int query1(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R){
return tree1[rt];
}
int m = (l+r)/;
int ans = -INF;
if(m >= L){
ans = max(ans,query1(L,R,lson));
}
if(m < R){
ans = max(ans,query1(L,R,rson));
}
return ans;
}
int query2(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R){
return tree2[rt];
}
int m = (l+r)/;
int ans = ;
if(m >= L){
ans += query2(L,R,lson);
}
if(m < R){
ans += query2(L,R,rson);
}
return ans;
}
int Getmax(int x,int y)
{
int ans = -INF;
while(top[x] != top[y])
{
if(deq[top[x]] < deq[top[y]]){
swap(x,y);
}
ans = max(ans,query1(w[top[x]],w[x],,fn,));
x = fa[top[x]];
}
if(deq[x] < deq[y]){
swap(x,y);
}
ans = max(ans,query1(w[y],w[x],,fn,));
return ans;
}
int Getsum(int x,int y)
{
int ans = ;
while(top[x] != top[y])
{
if(deq[top[x]] < deq[top[y]]){
swap(x,y);
}
ans += query2(w[top[x]],w[x],,fn,);
x = fa[top[x]];
}
if(deq[x] < deq[y]){
swap(x,y);
}
ans += query2(w[y],w[x],,fn,);
return ans;
}
int main()
{
int i;
while(~scanf("%d",&n))
{
ind = ;
memset(pre,-,sizeof(pre));
memset(tree1,,sizeof(tree1));
memset(tree2,,sizeof(tree2));
for(i=; i<n; i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for(i=; i<=n; i++){
scanf("%d",&val[i]);
}
dfs1(,,); fn = ;
dfs2(,); for(i=; i<=n; i++){
Insert(w[i],val[i],,fn,);
}
int q;
char s[];
scanf("%d",&q);
while(q--)
{
scanf("%s",s);
int x,y;
scanf("%d%d",&x,&y);
if(s[] == 'C'){
updata(w[x],y,,fn,);
}
else if(s[] == 'S'){
printf("%d\n",Getsum(x,y));
}
else {
printf("%d\n",Getmax(x,y));
}
}
}
}
HYSBZ1036 树链剖分的更多相关文章
- HYSBZ1036 [ZJOI2008]树的统计(树链剖分)
将树通过树链剖分转化成线性序列,用线段树维护最值,和值即可. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N ...
- BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2050 Solved: 817[Submit][Status ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
- codevs 1228 苹果树 树链剖分讲解
题目:codevs 1228 苹果树 链接:http://codevs.cn/problem/1228/ 看了这么多树链剖分的解释,几个小时后总算把树链剖分弄懂了. 树链剖分的功能:快速修改,查询树上 ...
- 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)
题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...
- 树链剖分+线段树 CF 593D Happy Tree Party(快乐树聚会)
题目链接 题意: 有n个点的一棵树,两种操作: 1. a到b的路径上,给一个y,对于路径上每一条边,进行操作,问最后的y: 2. 修改某个条边p的值为c 思路: 链上操作的问题,想树链剖分和LCT,对 ...
- 树链剖分+线段树 HDOJ 4897 Little Devil I(小恶魔)
题目链接 题意: 给定一棵树,每条边有黑白两种颜色,初始都是白色,现在有三种操作: 1 u v:u到v路径(最短)上的边都取成相反的颜色 2 u v:u到v路径上相邻的边都取成相反的颜色(相邻即仅有一 ...
- bzoj2243树链剖分+染色段数
终于做了一道不是一眼出思路的代码题(⊙o⊙) 之前没有接触过这种关于染色段数的题目(其实上课好像讲过),于是百度了一下(现在思维能力好弱) 实际上每一段有用的信息就是总共有几段和两段各是什么颜色,在开 ...
- bzoj3631树链剖分
虽然是水题1A的感觉太爽了O(∩_∩)O~ 题意相当于n-1次树上路径上每个点权值+1,最后问每个点的权值 本来想写线段树,写好了change打算框架打完了再来补,结果打完发现只是区间加和单点查 前缀 ...
随机推荐
- 51nod-1661 1661 黑板上的游戏(组合游戏)
题目链接: 1661 黑板上的游戏 Alice和Bob在黑板上玩一个游戏,黑板上写了n个正整数a1, a2, ..., an,游戏的规则是这样的:1. Alice占有先手主动权.2. 每个人可以选取一 ...
- UESTC 1227 & POJ 3667 Hotel
非常细腻的线段树题目啊,后来还是有个细节写错了,查了一个晚上..就不分析了. 代码: #include <iostream> #include <cstdio> #includ ...
- POJ 2318 TOYS【叉积+二分】
今天开始学习计算几何,百度了两篇文章,与君共勉! 计算几何入门题推荐 计算几何基础知识 题意:有一个盒子,被n块木板分成n+1个区域,每个木板从左到右出现,并且不交叉. 有m个玩具(可以看成点)放在这 ...
- 最严谨的校验email地址的正则表达式
通用 (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0 ...
- Eclipse c++头文件问题(未完)
http://stackoverflow.com/questions/7905025/string-could-not-resolved-error-in-eclipse-for-c-eclipse- ...
- PNG文件
png格式主要由六大块组成:文件头.IHDR块.PLTE块.tRNS块.IDAT块.文件尾文件头一般是 8950 4E47 0D0A 1A0A而本题提示中的IHDR块是png中用来描述图片的基本信息, ...
- js常用宽高属性
document.body.clientWidth //body对象的宽度 document.body.clientHeight //body对象的高度 document.documentElemen ...
- Struts登录
- 4201 TortoiseSVN常用配置
在Windows下推荐使用乌龟(Tortoise)SVN客户端. TortoiseSVN 是 Subversion 版本控制系统的一个免费开源客户端,可以超越时间的管理文件和目录.文件保存在中央版本库 ...
- C语言 百炼成钢8
//题目22:两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人.已抽签决定 //比赛名单.有人向队员打听比赛的名单.a说他不和x比,c说他不和x, z比,请编程序找出 //三 ...