【题目描述】

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

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. 判断本地是否存在Jquery文件,如果不存在则使用CDN加速的Jquery文件

    <script>//判断是否成功将Jquery库引入,如果没有成功引入则引入本地Jquery库if (typeof jQuery == 'undefined') {document.wri ...

  2. sqlite3使用备忘

    执行sqlite3进入sqlite3环境: $ sqlite3 SQLite version -- :: Enter ".help" for usage hints. Connec ...

  3. UI界面相关

    在开发中有些控件或者控件显示的属性需要经常设置,但是又是万变不离其中,经常写着一样的代码会显得冗余,不利于阅读.这里做了简化. 1.UI控件 2.颜色管理 3.图片管理 4.字体选择

  4. Berkeley DB 使用经验总结

    作者:陈磊 NoSQL是现在互联网Web2.0时代备受关注的技术之一,被用来存储大量的非关系型的数据.Berkeley DB作为一款优秀的Key/Value存储引擎自然也在讨论之列.最近使用BDB来发 ...

  5. P1979 华容道

    题意:$n*m$棋盘上$n*m-1$颗棋子,有且只有一个格子为空白格子,每个棋子大小$1*1$ 有些棋子可以移动,而有些棋子固定,任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上 ...

  6. 最短路【bzoj2464】: 中山市选[2009]小明的游戏

    2464: 中山市选[2009]小明的游戏 Description 小明最近喜欢玩一个游戏.给定一个n * m的棋盘,上面有两种格子#和@.游戏的规则很简单:给定一个起始位置和一个目标位置,小明每一步 ...

  7. IE兼容css3圆角的htc解决方法

    IE兼容css教程3圆角的htc解决方法 现在css3的border-radius属性可以很方便的实现圆角功能,对网站前台人员无疑是一件喜事,但悲剧的是IE6/7/8并不支持,让我们弃新技术不用,是不 ...

  8. 74th LeetCode Weekly Contest Preimage Size of Factorial Zeroes Function

    Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...

  9. Codeforces - 625B 贪心

    求最小不重复匹配次数 改最后一个字符最划算 我当时怎么就没看出来.. #include<bits/stdc++.h> using namespace std; string S,T; in ...

  10. day22 正则表达式 re

    1. 正则表达式 正则表达式是对字符串操作的一种逻辑公式. 我们一般使用正则表达式对字符串进行匹配和过滤. 工具: 各大文本编辑器⼀般都有正则匹配功能. 我们也可以去http://tool.china ...