HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)
The devil likes to make thing in chaos. This kingdom’s road system is like simply a tree(connected graph without cycle). A road has a color of black or white. The devil often wants to make some change of this system.
In details, we call a path on the tree from a to b consists of vertices lie on the shortest simple path between a and b. And we say an edge is on the path if both its two endpoints is in the path, and an edge is adjacent to the path if exactly one endpoint of it is in the path.
Sometimes the devil will ask you to reverse every edge’s color on a path or adjacent to a path.
The king’s daughter, WJMZBMR, is also a cute loli, she is surprised by her father’s lolicon-like behavior. As she is concerned about the road-system’s status, sometimes she will ask you to tell there is how many black edge on a path.
Initially, every edges is white.
For each test case, the first line contains an integer n, which is the size of the tree. The vertices be indexed from 1.
On the next n-1 lines, each line contains two integers a,b, denoting there is an edge between a and b.
The next line contains an integer Q, denoting the number of the operations.
On the next Q lines, each line contains three integers t,a,b. t=1 means we reverse every edge’s color on path a to b. t=2 means we reverse every edge’s color adjacent to path a to b. t=3 means we query about the number of black edge on path a to b.
T<=5.
n,Q<=10^5.
Please use scanf,printf instead of cin,cout,because of huge input.
定位:中等题 LCT和树链剖分都可以做,简单起见就说说树链剖分,LCT类似。
首先注意到一条路径上是log n条重链和log n条轻边。
当我们给一个点的所有边上的邻居边打标记时,如果该点在重链上,会影响他的轻孩子们。(要特殊考虑链头和链尾之类的)。
那么我们可以给重链上的点打标记,表示这个点的轻孩子们被翻转了没。
那么一个轻边就可以直接询问出来。复杂度:O(n(logn)^2)。
这个官方题解不怎么详细,我说一下我的解法:
首先跟普通的树链剖分一样,先给每个结点编上一个编号,每条重链用一个线段树来维护(其实都放到同一个线段树中了,占用不同的位置)
现在用线段树维护3个值:sum代表这个区间的黑边总数,flip代表这个区间的边是否被翻转颜色,light代表这个区间是否被打上了翻转标记(后面再说这个是啥)
其中所有边的编号为其靠下方的结点的编号,也就是根的编号在边的维护中没有意义
其中light代表这个点作为操作②所影响的次数,即它的轻边(不考虑与父节点的边)被翻转次数的奇偶。
维护这些值,考虑重链上的重边,某条边是黑色当且仅当它在线段树中的flip是1(被翻转了奇数次)。而对于不在重链上的边,它是黑色当且仅当它在线段树中的flip(被翻转的次数奇偶性)和它的父亲被打上翻转标记的次数light的和是奇数(异或值为1)。
那么,对于操作①,在树链往上走的时候,把所有边都翻转一次即可,没有什么难度。
对于操作②,在树链往上走的时候,除了要给所有路径上的点打上翻转标记以外,还要:翻转重链上的孩纸边,因为它要翻转而不受翻转标记的影响;翻转重链的父边,它是轻边,会受到重链的父亲的翻转标记的影响,所以直接给翻转一次抵消这种影响。
对于操作③,在树链往上走的时候,统计重链上的黑边和sum,和不在重链上的黑边(上面有讲)。
然后这样就差写代码了。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL; const int MAXV = ;
const int MAXE = MAXV << ;
const int MAXT = MAXV << ; int head[MAXV], ecnt;
int to[MAXE], next[MAXE];
int n, m, T;
//Graph
void initGraph() {
memset(head + , -, n * sizeof(int));
ecnt = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
}
//Segment Tree
#define ll (x << 1)
#define rr (ll | 1)
#define mid ((l + r) >> 1)
int sum[MAXT], flip[MAXT];
int light[MAXT];//轻边是否翻转 void initSegmentTree() {
memset(sum + , , * n * sizeof(int));
memset(flip + , , * n * sizeof(int));
memset(light + , , * n * sizeof(int));
} void pushdown(int x, int l, int r) {
if(flip[x]) {
flip[ll] ^= ;
sum[ll] = (mid - l + ) - sum[ll];
flip[rr] ^= ;
sum[rr] = (r - mid) - sum[rr];
flip[x] = ;
}
if(light[x]) {
light[ll] ^= ;
light[rr] ^= ;
light[x] = ;
}
} void maintain(int x) {
sum[x] = sum[ll] + sum[rr];
} void modifyFlip(int x, int l, int r, int a, int b) {
if(a <= l && r <= b) {
flip[x] ^= ;
sum[x] = (r - l + ) - sum[x];
} else {
pushdown(x, l, r);
if(a <= mid) modifyFlip(ll, l, mid, a, b);
if(mid < b) modifyFlip(rr, mid + , r, a, b);
maintain(x);
}
} void modifyLight(int x, int l, int r, int a, int b) {
if(a <= l && r <= b) {
light[x] ^= ;
} else {
pushdown(x, l, r);
if(a <= mid) modifyLight(ll, l, mid, a, b);
if(mid < b) modifyLight(rr, mid + , r, a, b);
}
} int queryFlip(int x, int l, int r, int a, int b) {
if(a <= l && r <= b) {
return sum[x];
} else {
int res = ;
pushdown(x, l, r);
if(a <= mid) res += queryFlip(ll, l, mid, a, b);
if(mid < b) res += queryFlip(rr, mid + , r, a, b);
return res;
}
} int queryLight(int x, int l, int r, int a, int b) {
if(a <= l && r <= b) {
return light[x];
} else {
int res = ;
pushdown(x, l, r);
if(a <= mid) res += queryLight(ll, l, mid, a, b);
if(mid < b) res += queryLight(rr, mid + , r, a, b);
return res;
}
}
//树链剖分
int fa[MAXV], size[MAXV], son[MAXV], top[MAXV], tid[MAXV], dep[MAXV];
int dfs_clock; void dfs_size(int u, int f, int depth) {
fa[u] = f; dep[u] = depth;
size[u] = ; son[u] = ;
int maxsize = ;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == f) continue;
dfs_size(v, u, depth + );
size[u] += size[v];
if(size[v] > maxsize) {
son[u] = v;
maxsize = size[v];
}
}
} void dfs_heavy_edge(int u, int ancestor) {
tid[u] = ++dfs_clock; top[u] = ancestor;
if(son[u]) dfs_heavy_edge(son[u], ancestor);
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == fa[u] || v == son[u]) continue;
dfs_heavy_edge(v, v);
}
} void modifyFlip(int a, int b) {
while(top[a] != top[b]) {
if(dep[top[a]] < dep[top[b]]) swap(a, b);
modifyFlip(, , n, tid[top[a]], tid[a]);
a = fa[top[a]];
}
if(a != b) {
if(dep[a] < dep[b]) swap(a, b);
modifyFlip(, , n, tid[son[b]], tid[a]);
}
} void modifyLight(int a, int b) {
while(top[a] != top[b]) {
if(dep[top[a]] < dep[top[b]]) swap(a, b);
modifyLight(, , n, tid[top[a]], tid[a]);
if(son[a]) modifyFlip(, , n, tid[son[a]], tid[son[a]]);
modifyFlip(, , n, tid[top[a]], tid[top[a]]);
a = fa[top[a]];
}
if(dep[a] < dep[b]) swap(a, b);
modifyLight(, , n, tid[b], tid[a]);
if(fa[b]) modifyFlip(, , n, tid[b], tid[b]);
if(son[a]) modifyFlip(, , n, tid[son[a]], tid[son[a]]);
} int query(int a, int b) {
int res = ;
while(top[a] != top[b]) {
if(dep[top[a]] < dep[top[b]]) swap(a, b);
if(a != top[a]) res += queryFlip(, , n, tid[son[top[a]]], tid[a]);
res += queryFlip(, , n, tid[top[a]], tid[top[a]]) ^ queryLight(, , n, tid[fa[top[a]]], tid[fa[top[a]]]);
a = fa[top[a]];
}
if(a != b) {
if(dep[a] < dep[b]) swap(a, b);
res += queryFlip(, , n, tid[son[b]], tid[a]);
}
return res;
} void ask() {
int a, b;
while(cin>>a>>b && a + b) {
cout<<query(a, b)<<endl;
}
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
initGraph();
for(int i = , a, b; i < n; ++i) {
scanf("%d%d", &a, &b);
add_edge(a, b);
}
initSegmentTree();
dfs_clock = ;
dfs_size(, , );
dfs_heavy_edge(, );
scanf("%d", &m);
for(int i = , op, a, b; i < m; ++i) {
scanf("%d%d%d", &op, &a, &b);
if(op == ) modifyFlip(a, b);
if(op == ) modifyLight(a, b);
if(op == ) printf("%d\n", query(a, b));
//if(i == 1) ask();
}
}
}
HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)的更多相关文章
- hdu 4897 Little Devil I (树链剖分+线段树)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意: 给你一棵树,一开始每条边都是白色,有三种操作: 1.将 u - v路径上的边转换颜色 ...
- HDU 4897 Little Devil I 树链剖分+线段树
Little Devil I Problem Description There is an old country and the king fell in love with a devil. T ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- hdu_4897_Little Devil I(树链剖分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意:有三种操作,1是在树上的两个节点之间的路径改变当前的颜色,2是改变树上有且只有一个端点在u ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)
题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...
- BZOJ3862Little Devil I——树链剖分+线段树
题目大意: 给一棵树,每条边可能是黑色或白色(起始都是白色),有三种操作: 1.将u到v路径上所有边颜色翻转(黑->白,白->黑) 2.将只有一个点在u到v路径上的边颜色翻转 3.查询u到 ...
- HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 5452——Minimum Cut——————【树链剖分+差分前缀和】ACdream 1429——Diversion——————【树链剖分】
Minimum Cut Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Tota ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
随机推荐
- PHP 依赖注入 (转)
说这个话题之前先讲一个比较高端的思想--'依赖倒置原则' "依赖倒置是一种软件设计思想,在传统软件中,上层代码依赖于下层代码,当下层代码有所改动时,上层代码也要相应进行改动,因此维护成本较高 ...
- Fatal error: Cannot redeclare tran() (previously declared in
解决方法如下: 1.你该段源码里面是不是有include 或者require 之类的包含其他文件函数 包含的文件里已经有 函数 nw() 的定义 而这段代码里又有nv()的定义 所以出现redecla ...
- SQl中Left Join 、Right Join 、Inner Join与Ful Join
1 left join 左外连接:查询结果以左表数据为准.假如左表有四条数据,右表有三条数据,则查询结果为四条,且都是左表中有的数据. 例如: EMP表: SAL表: 左连接 左连接,表EMP是主表, ...
- thinkphp接手机网站接口
首先说下,支付宝提供的demo不一定完全正确,可能回缺少些步骤,所以,最好按照规则文档的步骤参照demo写. 先说遇到的问题: 1.错误0001,缺少partner.service等必参数 最初选择的 ...
- COLUMN_VALUE Pseudocolumn
With below three situation, we can use the pseudocolumn column_value to refer the column value. an X ...
- HTML: 仿写一个财经类静态的网页
要求:仿写一个静态的网页,主要采用HTML+CSS+DIV的布局方式, 新建两个文件:demo.html.demo.css 图片素材:image.zip demo.html代码如下: <!doc ...
- linux上配置java环境
四.安装JDKsudo rpm -ivh jdk-7u75-linux-x64.rpmsudo rpm -qd jdk //查看jdk安装路径:/usr/java/jdk1.7.0_75/ 五.编辑环 ...
- ava获得当前文件路径
第一种: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); ...
- js 如何清除setinterval
var id=window.setInterval(function(){ },1000);window.clearInterval(id);
- def文件格式
模块定义 (.def) 文件 模块定义 (.def) 文件为链接器提供有关被链接程序的导出.属性及其他方面的 信息.生成 DLL 时,.def 文件最有用.由于存在 ...