[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 ...
随机推荐
- 开启VS的JavaScript调试
前提条件,设置我们的IE 去掉勾,即启用调试然后再JS代码片段中输入debugger.注意:如果IE开启了F12调试面板, VS是不会捕获JS代码片段中的debugger断点.
- c#桌面应用程序开发--登陆窗口
一.显示登陆窗口 应用程序入口点为Main方法,因此在Main方法中创建登陆窗体. 1)创建登陆窗体(登陆窗体UI已提前创建好); 2)显示窗体,以模式对话框的形式显示,并赋值给result; 3)判 ...
- EasyUi取消选中表格的所有行
- ASPxGridView 下拉框不让输入
DropDownStyle="DropDownList"该属性使combox控件不能手动输入数据,只能在下拉列表中选择
- BZOJ 2725 [Violet 6]故乡的梦 线段树+最短路树
\(\color{#0066ff}{ 题目描述 }\) \(\color{#0066ff}{输入格式}\) \(\color{#0066ff}{输出格式}\) \(\color{#0066ff}{输入 ...
- pytorch批训练数据构造
这是对莫凡python的学习笔记. 1.创建数据 import torch import torch.utils.data as Data BATCH_SIZE = 8 x = torch.linsp ...
- JS执行顺序-函数声明提升、匿名函数、函数表达式
大方向上: JS 是按照 代码块 进行 编译.执行 的. 学习至: 1.变量声明提升 2.新唐的博客 3.js中匿名函数的创建与调用方法分析 4.前端圣经 - <高程三> 5.深入理解变量 ...
- void类型指针的基本用法
void作为指针时可以用任意类型的的指针值都可以给它进行赋值和传递,但是输出时必须时显性输出 代码如下: #include<cstdio> #include<iostream> ...
- Oracle table-lock的5种模式
Oracle中的锁定可以分为几类: 1.DML lock(data lock), 2.DDL lock(dictionary lock) 3.internal lock/latch. DML lock ...
- zabbix 安装使用
zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位/解决 ...