[HZOI 2015]树黑白
【题目描述】
给定一棵树,要求维护以下操作:
1、M u 将u节点反色
2、Q u 查询u到所有黑色节点距离和
【输入格式】
第一行n,m 表示节点总数和操作次数
之后n-1行,每行u,v表示两个端点,w表示边权
之后m行,操作如题意
n,m<=200000,边权<=1000
一开始所有点均为白色
【输出格式】
输出每次Q的答案
【样例输入】
7 5
2 1 144
3 2 361
4 3 236
5 3 697
6 2 140
7 5 718
Q 4
M 4
Q 5
Q 1
Q 7
【样例输出】
0
933
741
1651
题解:
和上一题差不多,多了一个修改操作。
还是动态点分,每个节点储存两个数,分别表示整棵子树到根和到根节点的父节点的距离和。
容斥一下就好。
//Never forget why you start
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf (2147483647)
using namespace std;
int n,m,a[],lim;
struct node{
int next,to,dis;
}edge[];
int head[],size;
void putin(int from,int to,int dis){
size++;
edge[size].next=head[from];
edge[size].to=to;
edge[size].dis=dis;
head[from]=size;
}
int fa[][],dis[],depth[];
void dfs1(int r,int father){
int i;
fa[r][]=father;
depth[r]=depth[father]+;
for(i=head[r];i!=-;i=edge[i].next){
int y=edge[i].to;
if(y!=father){
dis[y]=dis[r]+edge[i].dis;
dfs1(y,r);
}
}
}
void make(){
lim=log(n)/log();
for(int i=;i<=lim;i++)
for(int j=;j<=n;j++)
fa[j][i]=fa[fa[j][i-]][i-];
}
int LCA(int x,int y){
if(depth[x]<depth[y])swap(x,y);
for(int i=lim;i>=;i--)
if(depth[fa[x][i]]>=depth[y])
x=fa[x][i];
if(x!=y){
for(int i=lim;i>=;i--)
if(fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
x=fa[x][];
y=fa[y][];
}
return x;
}
int dist(int x,int y){
int lca=LCA(x,y);
return dis[x]+dis[y]-dis[lca]*;
}
int tot,root,vis[],d[],ff[],cnt[];
void getroot(int r,int father){
int i;
cnt[r]=;d[r]=;
for(i=head[r];i!=-;i=edge[i].next){
int y=edge[i].to;
if(y!=father&&!vis[y]){
getroot(y,r);
cnt[r]+=cnt[y];
d[r]=max(d[r],cnt[y]);
}
}
d[r]=max(d[r],tot-cnt[r]);
if(d[root]>d[r])root=r;
}
void buildtree(int r,int father){
int i,all=tot;
ff[r]=father;vis[r]=;
for(i=head[r];i!=-;i=edge[i].next){
int y=edge[i].to;
if(!vis[y]){
if(cnt[y]>cnt[r])cnt[y]=all-cnt[r];tot=cnt[y];
root=;getroot(y,r);buildtree(root,r);
}
}
}
int p[][];
void insert(int x,int v){
int i;
for(i=x;ff[i];i=ff[i]){
int len=dist(x,ff[i]);
p[i][]+=len*v;
p[ff[i]][]+=len*v;
}
for(i=x;i;i=ff[i])cnt[i]+=v;
}
int find(int x){
int i,ans=p[x][];
for(i=x;ff[i];i=ff[i]){
int len=dist(x,ff[i]);
ans+=p[ff[i]][];
ans-=p[i][];
ans+=(cnt[ff[i]]-cnt[i])*len;
}
return ans;
}
void clean(){
memset(head,-,sizeof(head));
size=;
}
int main(){
freopen("A_Tree.in","r",stdin);
freopen("A_Tree.out","w",stdout);
int i,j;
clean();
scanf("%d%d",&n,&m);
for(i=;i<n;i++){
int u,v,l;
scanf("%d%d%d",&u,&v,&l);
putin(u,v,l);
putin(v,u,l);
}
dfs1(,);make();
tot=n;root=;d[]=inf;
getroot(,);buildtree(root,);
for(i=;i<=n;i++)if(!ff[i]){root=i;break;}
char s[];
memset(cnt,,sizeof(cnt));
while(m--){
scanf("%s",s);
if(s[]=='Q'){
int x;
scanf("%d",&x);
printf("%d\n",find(x));
}
else{
int x;
scanf("%d",&x);
a[x]^=;
if(a[x])insert(x,);
else insert(x,-);
}
}
return ;
}
[HZOI 2015]树黑白的更多相关文章
- COGS 2280. [HZOI 2015]树白黑
★★ 输入文件:B_Tree.in 输出文件:B_Tree.out 简单对比时间限制:2 s 内存限制:512 MB [题目描述] 给定一棵有根树,树根为1,一开始这棵树所有节点均为白 ...
- Bitset([HZOI 2015]偏序++)
Bitset简介 下面介绍C++ STL 中一个非常有用的东西: Bitset 类似于二进制状压,它可以把信息转化成一个01串存储起来 定义方法: 首先要#include<bitset>或 ...
- cogs 2320. [HZOI 2015]聪聪的世界题解
2320. [HZOI 2015]聪聪的世界 时间限制:6 s 内存限制:512 MB [题目描述] 背景: 聪聪的性取向有问题. 题目描述: 聪聪遇到了一个难题: 给出一个序列a1…an,完成以 ...
- BZOJ 2287. [HZOI 2015]疯狂的机器人 [FFT 组合计数]
2287. [HZOI 2015]疯狂的机器人 题意:从原点出发,走n次,每次上下左右不动,只能在第一象限,最后回到原点方案数 这不煞笔提,组合数写出来发现卷积NTT,然后没考虑第一象限gg 其实就是 ...
- 【COGS】2287:[HZOI 2015]疯狂的机器人 FFT+卡特兰数+排列组合
[题意][COGS 2287][HZOI 2015]疯狂的机器人 [算法]FFT+卡特兰数+排列组合 [题解]先考虑一维的情况,支持+1和-1,前缀和不能为负数,就是卡特兰数的形式. 设C(n)表示第 ...
- [cogs2314][HZOI 2015] Persistable Editor - 可持久化平衡树
[cogs2314][HZOI 2015]Persistable Editor - 可持久化平衡树 题目链接 首先吐槽扯淡几句 [题目描述] 维护一种可持久化的文本编辑器,支持下列操作: 1 p st ...
- cogs 2123. [HZOI 2015] Glass Beads
2123. [HZOI 2015] Glass Beads ★★★ 输入文件:MinRepresentations.in 输出文件:MinRepresentations.out 简单对比时 ...
- COGS 2580. [HZOI 2015]偏序 II
COGS 2580. [HZOI 2015]偏序 II 题目传送门 题目大意:给n个元素,每个元素有具有4个属性a,b,c,d,求i<j并且ai<aj,bi<bj,ci<cj, ...
- 【CJOJ2375】 【HZOI 2015】偏序 II(cdq分治,树状数组)
传送门 CJOJ Solution 具体实现参考上一篇Blog(四维偏序) 代码实现1(cdq+cdq+cdq+BIT) /* mail: mleautomaton@foxmail.com autho ...
随机推荐
- MVVM模式下WPF动态绑定展示图片
MVVM模式下WPF动态展示图片,界面选择图标,复制到项目中固定目录下面,保存到数据库的是相对路径,再次读取的时候是根据数据库的相对路径去获取项目中绝对路径的图片展示. 首先在ViewModel中 / ...
- 清理前一天log日志shell
清理前一天log日志shell #!/bin/bashlogPathList=`cat <<STD/data/logs/aiclass/backcms/data/logs/aiclass/ ...
- Django之博客系统:用户登陆
使用django有一个好处就是有各种各样的框架可以拿来直接使用.相比flask,django自带的框架确实要多很多.比如这一章就要介绍的用户登录.Django拥有一个内置的认证(authenticat ...
- (Delphi)第一个Windows 32 API的窗口程序
program Project1; uses Winapi.Windows, Winapi.messages; {$R *.res} const className = 'MyDelphiWindow ...
- 深入浅出git
图文 http://www.cnblogs.com/syp172654682/p/7689328.html 廖雪峰 https://www.liaoxuefeng.com/wiki/001373951 ...
- 【bzoj1853】: [Scoi2010]幸运数字 数论-容斥原理
[bzoj1853]: [Scoi2010]幸运数字 预处理出所有幸运数字然后容斥原理 但是幸运数字是2logn个数的 直接搞会炸 所以把成倍数的处理掉 然后发现还是会T 所以数字要从大到小处理会快很 ...
- ios swift 打造自己的http请求工具
在ios开发中,网络请求是不可以少的,说到网络请求可能用的最多的就是第三方的比人比较有名的AFNetworking.Alamofire等,原生的用的少.今天就用ios提供的原生方法来打造属于自己的一个 ...
- iOS端VR视频播放(转自简书http://www.jianshu.com/p/1ee1a0d1d320)
下面是我看了谷歌的一个VR在iOS端开发的文档写的一个demo. 第一步是需要用cocoaPods导入谷歌开发的一个第三方:CardboardSDK,怎么导入就不多说了,这里需要注意的一点是谷歌方面的 ...
- kvm重新命名
1.停止虚拟机 virsh shutdown wcltest3 2.导出虚拟机的配置文件 cd /etc/libvirt/qemu virsh dumpxml wcltest3 > vnc.xm ...
- Mybatis学习笔记(五) —— Mapper.xml(输入映射和输出映射)
一.parameterType(输入类型) 1.1 传递简单类型 <!-- 根据用户id查询用户 --> <select id="queryUserById" p ...