[Codechef - ADITREE] Adi and the Tree - 树链剖分,线段树
[Codechef - ADITREE] Adi and the Tree
Description
树上每个节点有一个灯泡,开始所有灯泡都是熄灭的。每次操作给定两个数 \(a,b\) ,将 \(a,b\) 这两个节点的灯的状态改变。定义某个状态的权值为,将树上所有亮点两两配对,每个对的权值的总和最小值。其中一个配对的权值定义为这两个点之间的距离。求出每次操作后的权值。
Solution
很容易发现如果我们将每个亮点到树根的路径染色,那么染色次数为奇数的路径就会被统计入答案。
所以只需要维护布尔值就可以,这样每次操作就转化为对点到根的路径异或,询问就是整棵树的权和。树链剖分一下就可以。
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
namespace seg
{
int val[N],tag[N];
void pushup(int p)
{
val[p]=val[p*2]+val[p*2+1];
}
void pushdown(int p,int l,int r)
{
if(tag[p])
{
tag[p*2]^=1;
tag[p*2+1]^=1;
val[p*2]=((l+r)/2-l+1)-val[p*2];
val[p*2+1]=(r-(l+r)/2)-val[p*2+1];
tag[p]^=1;
}
}
void modify(int p,int l,int r,int ql,int qr)
{
if(l>qr || r<ql)
return ;
if(l>=ql && r<=qr)
{
val[p]=(r-l+1)-val[p];
tag[p]^=1;
}
else
{
pushdown(p,l,r);
modify(p*2,l,(l+r)/2,ql,qr);
modify(p*2+1,(l+r)/2+1,r,ql,qr);
pushup(p);
}
}
int query()
{
return val[1];
}
} // seg
namespace tree
{
vector <int> g[N];
int n,top[N],wson[N],siz[N],dep[N],vis[N],tid[N],did[N],fa[N],cnt=0;
void link(int p,int q)
{
g[p].push_back(q);
g[q].push_back(p);
}
void dfs1(int p)
{
vis[p]=siz[p]=1;
for(int i=0; i<g[p].size(); i++)
{
if(vis[g[p][i]]==0)
{
dep[g[p][i]]=dep[p]+1;
fa[g[p][i]]=p;
dfs1(g[p][i]);
siz[p]+=siz[g[p][i]];
if(wson[p]==0 || siz[g[p][i]]>siz[wson[p]])
wson[p]=g[p][i];
}
}
}
void dfs2(int p)
{
vis[p]=1;
did[p]=++cnt;
tid[cnt]=p;
if(wson[p])
{
top[wson[p]]=top[p];
dfs2(wson[p]);
}
for(int i=0; i<g[p].size(); i++)
{
if(vis[g[p][i]]==0)
{
top[g[p][i]]=g[p][i];
dfs2(g[p][i]);
}
}
}
void presolve()
{
dep[1]=1;
dfs1(1);
memset(vis,0,sizeof vis);
top[1]=1;
dfs2(1);
}
void link_modify(int p,int q)
{
while(top[p]-top[q])
{
if(dep[top[p]]>dep[top[q]])
swap(p,q);
seg::modify(1,1,n,did[top[q]],did[q]);
q=fa[top[q]];
}
if(dep[p]>dep[q])
swap(p,q);
seg::modify(1,1,n,did[p],did[q]);
}
}
int n,m,t1,t2,t3;
int main()
{
scanf("%d",&n);
for(int i=1; i<n; i++)
{
scanf("%d%d",&t1,&t2);
tree::link(t1,t2);
}
tree::n=n;
tree::presolve();
scanf("%d",&m);
for(int i=1; i<=m; i++)
{
scanf("%d%d",&t1,&t2);
tree::link_modify(1,t1);
tree::link_modify(1,t2);
printf("%d\n",seg::query());
}
}
[Codechef - ADITREE] Adi and the Tree - 树链剖分,线段树的更多相关文章
- Aizu 2450 Do use segment tree 树链剖分+线段树
Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- POJ3237 Tree 树链剖分 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...
- 【CF725G】Messages on a Tree 树链剖分+线段树
[CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...
- Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- POJ3237 (树链剖分+线段树)
Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...
- B20J_3231_[SDOI2014]旅行_树链剖分+线段树
B20J_3231_[SDOI2014]旅行_树链剖分+线段树 题意: S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,城市信仰不同的宗教,为了方便,我们用不同的正整数代表各种宗教. S国 ...
- BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )
BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...
随机推荐
- 使用VConsole调试代码
在真实手机上运行H5页面时,无法看到控制台.为了能在真实手机上使用控制台,可以加入如下代码实现控制台: //引入vconsole var isTestEnvironment =true if(isTe ...
- DotnetCore 单文件发布
NETCORE3.0开始,可以发布单文件,参考https://www.cnblogs.com/ZaraNet/p/11790645.html 发布后(config目录 是手工复制进去的) 运行时, ...
- PTA 邻接表存储图的广度优先遍历
试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ) 其中LGraph是邻接表存储的 ...
- 封装的Redis队列
封装的Redis队列 MyRedisQueue.py #!usr/bin/env python2.7 # -*- coding: utf-8 -*- import redis class RedisQ ...
- 组件使用v-model、$listeners、.sync(区别于v-model的双向数据绑定)
自定义组件 自定义组件的v-model 首先我们先说一下在自定义组件中使用v-model的必要条件 在自定义的组件中要有input(这里我们先不讨论单选复选框) 在自定义组件的模板对象中要有props ...
- day6 基础总结和编码方式
# = 赋值 == 比较值是否相等 is 比较内存地址 li1 = [1, 2, 3] li2 = li1 print(li1 is li2) print(id(li1), id(li2)) #数字, ...
- python list comprehensions
list comprehensions 列表解释 You now have all the knowledge necessary to begin writing list comprehensio ...
- 技术之心 | 云信和TA们携手打响防疫战
1月27日,教育部发布<关于2020年春季学期延期开学的通知>,各地高等院校.中小学.幼儿园纷纷推迟开学.疫情当前,学生们的鼠年寒假变得无比漫长. 网易云信众多教育客户以行动践行教育的 ...
- EOFError: Compressed file ended before the end-of-stream marker was reached解决办法(在Windows下查看已下载的MNIST数据文件)
出现这个问题的原因是因为文件下载到一半就中断了,解决办法是删除datasets中下载到一半的数据包. 下面以我遇到的问题为例: 我下载数据下载到最后一个包就没有反应了,于是我强制终止了运行,可能是因为 ...
- 问题 D: 八皇后
#include <cstdio> #include <vector> #include <algorithm> using namespace std; cons ...