【题目描述】

给定一棵树,要求维护以下操作:

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]树黑白的更多相关文章

  1. COGS 2280. [HZOI 2015]树白黑

    ★★   输入文件:B_Tree.in   输出文件:B_Tree.out   简单对比时间限制:2 s   内存限制:512 MB [题目描述] 给定一棵有根树,树根为1,一开始这棵树所有节点均为白 ...

  2. Bitset([HZOI 2015]偏序++)

    Bitset简介 下面介绍C++ STL 中一个非常有用的东西: Bitset 类似于二进制状压,它可以把信息转化成一个01串存储起来 定义方法: 首先要#include<bitset>或 ...

  3. cogs 2320. [HZOI 2015]聪聪的世界题解

    2320. [HZOI 2015]聪聪的世界 时间限制:6 s   内存限制:512 MB [题目描述] 背景: 聪聪的性取向有问题. 题目描述: 聪聪遇到了一个难题: 给出一个序列a1…an,完成以 ...

  4. BZOJ 2287. [HZOI 2015]疯狂的机器人 [FFT 组合计数]

    2287. [HZOI 2015]疯狂的机器人 题意:从原点出发,走n次,每次上下左右不动,只能在第一象限,最后回到原点方案数 这不煞笔提,组合数写出来发现卷积NTT,然后没考虑第一象限gg 其实就是 ...

  5. 【COGS】2287:[HZOI 2015]疯狂的机器人 FFT+卡特兰数+排列组合

    [题意][COGS 2287][HZOI 2015]疯狂的机器人 [算法]FFT+卡特兰数+排列组合 [题解]先考虑一维的情况,支持+1和-1,前缀和不能为负数,就是卡特兰数的形式. 设C(n)表示第 ...

  6. [cogs2314][HZOI 2015] Persistable Editor - 可持久化平衡树

    [cogs2314][HZOI 2015]Persistable Editor - 可持久化平衡树 题目链接 首先吐槽扯淡几句 [题目描述] 维护一种可持久化的文本编辑器,支持下列操作: 1 p st ...

  7. cogs 2123. [HZOI 2015] Glass Beads

    2123. [HZOI 2015] Glass Beads ★★★   输入文件:MinRepresentations.in   输出文件:MinRepresentations.out   简单对比时 ...

  8. COGS 2580. [HZOI 2015]偏序 II

    COGS 2580. [HZOI 2015]偏序 II 题目传送门 题目大意:给n个元素,每个元素有具有4个属性a,b,c,d,求i<j并且ai<aj,bi<bj,ci<cj, ...

  9. 【CJOJ2375】 【HZOI 2015】偏序 II(cdq分治,树状数组)

    传送门 CJOJ Solution 具体实现参考上一篇Blog(四维偏序) 代码实现1(cdq+cdq+cdq+BIT) /* mail: mleautomaton@foxmail.com autho ...

随机推荐

  1. ubuntu - 官方简体中文wiki,有你想要的ubuntu的一切

    ubuntu官方简体中文wiki:http://wiki.ubuntu.org.cn/%E9%A6%96%E9%A1%B5

  2. [转]10分钟梳理MySQL知识点:揭秘亿级高并发数据库调优与最佳实践法则

    转:https://mp.weixin.qq.com/s/RYIiHAHHStIMftQT6lQSgA 做业务,要懂基本的SQL语句: 做性能优化,要懂索引,懂引擎: 做分库分表,要懂主从,懂读写分离 ...

  3. mysql 表压缩

    mysql中经常出现历史表,这些表不会进行修改数据的操作,只有读操作.那么我们可以对其进行压缩处理,缩减磁盘空间.Innodb表和MyISAM表的压缩指令不一样.下面分别来讨论: 一.InnoDB表 ...

  4. loj #547. 「LibreOJ β Round #7」匹配字符串

    #547. 「LibreOJ β Round #7」匹配字符串   题目描述 对于一个 01 串(即由字符 0 和 1 组成的字符串)sss,我们称 sss 合法,当且仅当串 sss 的任意一个长度为 ...

  5. Python Flask模块

    模块是一个包含响应文本的文件,其中包含占用位变量表示的动态部分,其具体值只在请求的上下文中才知道.使用真实值替换变量,再返回最终得到的响应字符串,这一过程称为渲染.为了渲染模块,Flask使用一个名为 ...

  6. C语言数据结构-顺序线性表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作

    1.数据结构-顺序线性表的实现-C语言 #define MAXSIZE 100 //结构体定义 typedef struct { int *elem; //基地址 int length; //结构体当 ...

  7. BFS【bzoj1667】: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛

    1667: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛 Description 经过跟Farmer John长达数年的谈判,奶牛们终于如愿以偿地得到了想要的旱冰鞋.农场上大 ...

  8. 10大Python开源项目推荐(Github平均star2135)

    翻译 | suisui 来源 | 人工智能头条(AI_Thinker) 继续假日充电系列~本文是 Mybridge 挑选的 10 个 Python 开源项目,Github 平均star 2135,希望 ...

  9. Hibernate常见报错

    1.A different object with the same identifier value was already associated with the session(使用Hibern ...

  10. Linux 磁盘 分区 挂载点的理解

    在Linux中一切皆文件: 虚拟文件系统(Virtual File System, 简称 VFS), 是 Linux 内核中的一个软件层,用于给用户空间的程序提供文件系统接口:同时,它也提供了内核中的 ...