【ZJOI2008】树的统计
题目
一棵树上有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本身
分析
只用单点修改的树链剖分模板题。
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
const int maxlongint=2147483647;
using namespace std;
struct trees
{
int v,mx,l,r;
}tree[90000];
int d[90000],dson[90000],deep[90000],size[90000],fa[90000],top[90000],n,m,v[90000],bef[90000],tot,mi2[30];
int next[90000],last[90000],to[90000],po;
int ans;
int bj(int x,int y)
{
next[++tot]=last[x];
last[x]=tot;
to[tot]=y;
}
int dg(int x)
{
size[x]=1;
int mx=0;
for(int i=last[x];i;i=next[i])
{
if(to[i]!=fa[x])
{
fa[to[i]]=x;
deep[to[i]]=deep[x]+1;
dg(to[i]);
size[x]+=size[to[i]];
if(size[to[i]]>mx)
{
mx=size[to[i]];
dson[x]=to[i];
}
}
}
}
int dg1(int x)
{
bef[x]=++tot;
d[tot]=x;
if(top[x]==0)
{
top[x]=x;
}
if(dson[x])
{
top[dson[x]]=top[x];
dg1(dson[x]);
}
for(int i=last[x];i;i=next[i])
{
if(to[i]!=fa[x] && to[i]!=dson[x])
{
dg1(to[i]);
}
}
}
int treechange(int l,int r,int pos,int aim,int value)
{
int mid=(l+r)/2;
tree[pos].l=l;
tree[pos].r=r;
if(l==r)
{
tree[pos].v=value;
tree[pos].mx=value;
return 0;
}
if(aim<=mid)
{
treechange(l,mid,pos*2,aim,value);
}
else
{
treechange(mid+1,r,pos*2+1,aim,value);
}
tree[pos].v=tree[pos*2].v+tree[pos*2+1].v;
tree[pos].mx=max(tree[pos*2].mx,tree[pos*2+1].mx);
}
int findmx(int l,int r,int pos,int aiml,int aimr)
{
int mid=(l+r)/2;
if(l==aiml && r==aimr)
{
ans=max(ans,tree[pos].mx);
return 0;
}
if(aimr<=mid)
{
findmx(l,mid,pos*2,aiml,aimr);
}
else
if(aiml>mid)
{
findmx(mid+1,r,pos*2+1,aiml,aimr);
}
else
{
findmx(l,mid,pos*2,aiml,mid);
findmx(mid+1,r,pos*2+1,mid+1,aimr);
}
}
int findmax(int x,int y)
{
ans=-maxlongint;
while(top[x]!=top[y])
{
if(deep[top[x]]>=deep[top[y]])
{
findmx(1,n,1,bef[top[x]],bef[x]);
x=fa[top[x]];
}
else
{
findmx(1,n,1,bef[top[y]],bef[y]);
y=fa[top[y]];
}
}
if(deep[x]>=deep[y])
{
findmx(1,n,1,bef[y],bef[x]);
}
else
{
findmx(1,n,1,bef[x],bef[y]);
}
printf("%d\n",ans);
}
int findsm(int l,int r,int pos,int aiml,int aimr)
{
int mid=(l+r)/2;
if(l==aiml && r==aimr)
{
ans+=tree[pos].v;
return 0;
}
if(aimr<=mid)
{
findsm(l,mid,pos*2,aiml,aimr);
}
else
if(aiml>mid)
{
findsm(mid+1,r,pos*2+1,aiml,aimr);
}
else
{
findsm(l,mid,pos*2,aiml,mid);
findsm(mid+1,r,pos*2+1,mid+1,aimr);
}
}
int findsum(int x,int y)
{
ans=0;
while(top[x]!=top[y])
{
if(deep[top[x]]>=deep[top[y]])
{
findsm(1,n,1,bef[top[x]],bef[x]);
x=fa[top[x]];
}
else
{
findsm(1,n,1,bef[top[y]],bef[y]);
y=fa[top[y]];
}
}
if(deep[x]>=deep[y])
{
findsm(1,n,1,bef[y],bef[x]);
}
else
{
findsm(1,n,1,bef[x],bef[y]);
}
printf("%d\n",ans);
}
int main()
{
mi2[1]=1;
for(int i=2;i<=25;i++)
mi2[i]=mi2[i-1]*2;
scanf("%d",&n);
for(int i=1;i<=n-1;i++)
{
int x,y;
scanf("%d%d",&x,&y);
bj(x,y);
bj(y,x);
}
deep[1]=1;
dg(1);
tot=0;
dg1(1);
for(int i=1;i<=90000-1;i++)
tree[i].mx=-maxlongint;
for(int i=1;i<=25;i++)
{
if(mi2[i]*2>=tot)
{
po=mi2[i]*2-1;
break;
}
}
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
treechange(1,n,1,bef[i],x);
}
scanf("%d\n",&m);
char c;
for(int i=1;i<=m;i++)
{
c=getchar();
if(c=='C')
{
int x,y;
scanf("HANGE %d %d\n",&x,&y);
treechange(1,n,1,bef[x],y);
}
else
{
c=getchar();
if(c=='M')
{
int x,y;
scanf("AX %d %d\n",&x,&y);
findmax(x,y);
}
else
{
int x,y;
scanf("UM %d %d\n",&x,&y);
findsum(x,y);
}
}
}
}
【ZJOI2008】树的统计的更多相关文章
- BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14302 Solved: 5779[Submit ...
- bzoj1036 [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MB Submit: 12646 Solved: 5085 [Subm ...
- BZOJ 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MB Submit: 14354 Solved: 5802 [Subm ...
- 【BZOJ1036】[ZJOI2008]树的统计Count 树链剖分
[BZOJ1036][ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. ...
- bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 10677 Solved: 4313[Submit ...
- Bzoj 1036: [ZJOI2008]树的统计Count 树链剖分,LCT
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 11102 Solved: 4490[Submit ...
- 数据结构(LCT动态树):BZOJ 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 12266 Solved: 4945[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count( 树链剖分 )
树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...
- 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7496 Solved: 3078[Submit] ...
- bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题
[ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...
随机推荐
- cannot assign to struct field xxx in map
golang 中对 map 类型中的 struct 赋值报错 type s struct{ name string age int}func main(){ a := map[string]s{ &q ...
- 什么是SLF,PSL,MLF,SLO?
受国际经济金融形势不确定性增强以及各种影响流动性的因素波动较大影响,近年来我国银行体系短期流动性供求的波动性有所加大,尤其是当多个因素相互叠加或市场预期发生变化时,有可能出现市场短期资金供求缺口难以通 ...
- python启动线程查看线程的名称和id;python杀掉进程的方法
def cpu_app(): print("CPU") #启动一个线程t=threading.Thread(target=cpu_app,args=()) t.daemon = T ...
- 前端 CSS 盒子模型 目录
CSS盒子模型介绍 padding border属性
- python 开启进程两种方法 multiprocessing模块 介绍
一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu\_count\(\)查看),在python中大部分情况需要使用多进 ...
- Bubble Sort(冒泡排序)
冒泡排序(英语:Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行 ...
- Python模块logging
基本用法: import logging import sys # 获取logger实例,如果参数为空则返回root logger logger = logging.getLogger("A ...
- python之optparse
Python有两个内建的模块用来处理命令行参数 一个是getopt只能简单处理命令行参数 一个是optparse,功能更强大,而且易于使用,可以方便地生成标准的,符合Unix/Posix规范的命令行说 ...
- C函数调用过程原理及函数栈帧分析(转)
在x86的计算机系统中,内存空间中的栈主要用于保存函数的参数,返回值,返回地址,本地变量等.一切的函数调用都要将不同的数据.地址压入或者弹出栈.因此,为了更好地理解函数的调用,我们需要先来看看栈是怎么 ...
- Mybatis-学习笔记(8)常用的注解
1.常用的注解. 2.@insert.@delete.@update.@select完成常见的CRUD操作. import java.util.List; import org.apache.ibat ...