Little Devil I

Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

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.

 
Input
The first line contains an integer T, denoting the number of the test cases.
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.

 
Output
For each t=3 operation, output the answer in one line.
 
Sample Input
1
10
2 1
3 1
4 1
5 1
6 5
7 4
8 3
9 5
10 6

10
2 1 6
1 3 8
3 8 10
2 3 4
2 10 8
2 4 10
1 7 6
2 7 3
2 1 4
2 10 10

 
Sample Output
3

Hint

reverse color means change from white to black or vice virsa.

 
Author
WJMZBMR
 

题意:

  给定一棵树,每条边有黑白两种颜色,初始都是白色,现在有三种操作:

     1 u v:u到v路径上的边都取成相反的颜色

     2 u v:u到v路径上相邻的边都取成相反的颜色(相邻即仅有一个节点在路径上)

         3 u v:查询u到v路径上有多少个黑色边

题解:

  这题卡了我一天

  首先树剖一下,

  操作1,对于一条重链,我们直接在线段树W上修改即可,这就是个简单的区间线段树,

      轻边的话,我们观察到它是由2个点决定的,那么考虑到操作2,我们可以思考到,假设一条的一个端点别进行了操作2,那么这条边就被修改了,两个端点同时被操作,那么我们不修改,这样的话此时我们将这里的轻边的两个端点都异或1就好了,这里需要用到线段树L

  操作2,对于重链,我们直接在线段树L上进行修改,但是我们要考虑到途中的重边或许会与改路径相连但又不在此路径上,

      熟悉树剖过程的话,其实也就在端点两端会有重边相接,和向上跳的过程中,轻边旁会有相交的重边,只要对跳点的重儿子进行线段树W上的修改即可

  操作3,重链,我们直接查询线段树W就好了,轻边的话我们查询L进行异或就行

  

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 5e5+, M = 1e3+, mod = 1e9+, inf = 2e9; int dep[N],head[N],t=,sz[N],fa[N],indexS,top[N],pos[N],son[N];
struct ss{int to,next;}e[N*];
int n;
void add(int u,int v)
{e[t].to = v;e[t].next = head[u];head[u] = t++;}
void dfs(int u) {
int k = ;
sz[u] = ;
dep[u] = dep[fa[u]] + ;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(to == fa[u]) continue;
fa[to] = u;
dfs(to);
sz[u] += sz[to];
if(sz[to] > sz[k]) k = to;
}
if(k) son[u] = k;
}
void dfs(int u,int chain) {
int k = ;
pos[u] = ++indexS;
top[u] = chain;
if(son[u] > )
dfs(son[u],chain);
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(dep[to] > dep[u] && son[u] != to)
dfs(to,to);
}
}
int lazy[N],sum[N],L[N],lazyw[N],color[N];
void init() {
indexS = ;
t = ;
memset(sz,,sizeof(sz));
memset(head,,sizeof(head));
memset(fa,,sizeof(fa));
memset(son,,sizeof(son));
memset(L,,sizeof(L));
memset(sum,,sizeof(sum));
memset(lazy,,sizeof(lazy));
memset(lazyw,,sizeof(lazyw));
memset(color,,sizeof(color));
memset(L,,sizeof(L));
}
void push_down(int i,int ll,int rr) {
if(lazyw[i] && ll != rr) {
lazyw[ls] ^= ;
if(ll!=)sum[ls] = (mid-ll+) - sum[ls];
else sum[ls] = (mid-ll) - sum[ls];
lazyw[rs] ^= ;
sum[rs] = (rr - mid) - sum[rs];
}
lazyw[i] = ;
}
void push_up(int i,int ll,int rr) {
sum[i] = sum[ls] + sum[rs];
}
int queryW(int i,int ll,int rr,int x,int y) {
int res = ;
push_down(i,ll,rr);
if(ll == x && rr == y)
return sum[i];
if(y <= mid) res += queryW(ls,ll,mid,x,y);
else if(x > mid) res += queryW(rs,mid+,rr,x,y);
else {
res += queryW(ls,ll,mid,x,mid);
res += queryW(rs,mid+,rr,mid+,y);
}
push_up(i,ll,rr);
return res;
}
int queryL(int i,int ll,int rr,int x) {
if(lazy[i]) {
if(ll == rr) L[i] ^= ;
else {
lazy[ls] ^= ;
lazy[rs] ^= ;
}
lazy[i] = ;
}
if(ll == rr) return L[i];
if(x <= mid) return queryL(ls,ll,mid,x);
else return queryL(rs,mid+,rr,x);
}
void updateL(int i,int ll,int rr,int x,int y) {
if(ll == x && rr == y) {
lazy[i] ^= ;
return ;
}
if(y <= mid) updateL(ls,ll,mid,x,y);
else if(x > mid) updateL(rs,mid+,rr,x,y);
else {
updateL(ls,ll,mid,x,mid);
updateL(rs,mid+,rr,mid+,y);
}
}
void updateW(int i,int ll,int rr,int x,int y) {
push_down(i,ll,rr);
if(ll == x && rr == y) {
lazyw[i] ^= ;
if(ll != )sum[i] = rr-ll+ - sum[i];
else sum[i] = rr-ll+- - sum[i];
return ;
}
if(y <= mid) updateW(ls,ll,mid,x,y);
else if(x > mid) updateW(rs,mid+,rr,x,y);
else {
updateW(ls,ll,mid,x,mid);
updateW(rs,mid+,rr,mid+,y);
}
push_up(i,ll,rr);
}
void updateL(int x,int y) {
int firx = , firy = ;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x,y),swap(firx,firy);
if(son[x])updateW(,,n,pos[son[x]],pos[son[x]]);
updateL(,,n,pos[top[x]],pos[x]);
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x,y);
if(son[x] && x==y)
updateW(,,n,pos[son[x]],pos[son[x]]);
if(x!=y && son[y])updateW(,,n,pos[son[y]],pos[son[y]]);
if(x != && son[fa[x]] == x) updateW(,,n,pos[x],pos[x]);
updateL(,,n,pos[x],pos[y]);
}
void updateW(int x,int y) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x,y);
if(top[x] != x) updateW(,,n,pos[son[top[x]]],pos[x]);
color[top[x]]^=;
x = fa[top[x]];
}
if(x == y) {}
else {
if(dep[x] < dep[y] && son[x]) updateW(,,n,pos[son[x]],pos[y]);
else if(son[y])updateW(,,n,pos[son[y]],pos[x]);
}
}
int solve(int x,int y) {
int res = ;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x,y);
if(top[x] != x) res += queryW(,,n,pos[son[top[x]]],pos[x]);
int fx = queryL(,,n,pos[top[x]])^queryL(,,n,pos[fa[top[x]]]);
res += fx^color[top[x]];
x = fa[top[x]];
}
if(x == y) {
}
else {
if(dep[x] < dep[y] && son[x])res += queryW(,,n,pos[son[x]],pos[y]);
else if(son[y])res += queryW(,,n,pos[son[y]],pos[x]);
}
return res;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
init();
for(int i = ; i < n; ++i) {
int x,y;
scanf("%d%d",&x,&y);
add(x,y),add(y,x);
}
dfs();
dfs(,);
int Q;
scanf("%d",&Q);
while(Q--) {
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op == ) {
updateW(x,y);
}
else if(op == )
{
updateL(x,y);
}
else
{
if(x == y) puts("");
else
printf("%d\n",solve(x,y));
}
}
}
return ;
}

HDU 4897 Little Devil I 树链剖分+线段树的更多相关文章

  1. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  2. BZOJ3862Little Devil I——树链剖分+线段树

    题目大意: 给一棵树,每条边可能是黑色或白色(起始都是白色),有三种操作: 1.将u到v路径上所有边颜色翻转(黑->白,白->黑) 2.将只有一个点在u到v路径上的边颜色翻转 3.查询u到 ...

  3. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  4. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

  5. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  6. 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  7. BZOJ2243 (树链剖分+线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

  8. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  9. bzoj4034 (树链剖分+线段树)

    Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...

  10. 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 ...

随机推荐

  1. 网易技术分享:Nginx缓存引发的跨域惨案

    推荐:更多技术团队分享文章 关注:MAYOU18技术专栏 1. 前言 贵金属wap版直播间上线后,偶尔有用户反馈,在进入wap直播间的时候,出现空白页面,但是重新刷新又可以正常显示了.我们曾一度认为是 ...

  2. RabbitMQ 关键词解释

    源地址: https://www.cnblogs.com/hz04022016/p/6518138.html RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQ ...

  3. 我们需要了解的五个ERP趋势

    企业的全面现代化进程伴随着ERP的发展.在2019年,预计规模较小的供应商将加大力度,双层ERP(Two-Tier ERP)将开始占据市场份额,企业也将要求更加灵活的ERP的实施方案. 该预测基于咨询 ...

  4. Python中的类(2)

    一.使用类和实例 我们先编写一个学生的类,它存储了有关学生的信息,还有一个整合学生信息的方法: student.py class Student(): def __init__(self,name,a ...

  5. 分享下自己的EmpireofCode进攻策略 https://empireofcode.com/ https://empireofcode.com/game/#

    # 没什么用,该游戏的模块调用不友好,取数据难import queue from battle import commander # import math unit_client = command ...

  6. mybatis完整sql调试

    问题描述 在使用mybatis进行开发的时候,由于可以动态拼接sql,这样大大方便了我们.但是也有一定的问题,当我们动态sql拼接的块很多的时候,我们要想从*mapper.xml中直接找出完整的sql ...

  7. 单链表 C语言 学习记录

    概念 链接方式存储 链接方式存储的线性表简称为链表(Linked List). 链表的具体存储表示为: 用一组任意的存储单元来存放线性表的结点(这组存储单元既可以是连续的,也可以是不连续的). 链表中 ...

  8. ASP.NET MVC______VS2012

    1,在VS2010中,选中指定的代码段,可以拖拽到工具箱中,形成标签,以后还想书写类似的代码,双击鼠标即可. 2,在VS2012中,可以在注释上标注//TODO:我是注释 这样,注释就会出现在任务列表 ...

  9. jquery怎么获得ul中li的个数

  10. 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 236A,虽然很水,但有一个很简单的函数用起来方便

    A. Boy or Girl time limit per test 1 second memory limit per test 256 megabytes input standard input ...