HDU 4897 Little Devil I 树链剖分+线段树

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+10, M = 1e3+20, mod = 1e9+7, inf = 2e9;

int dep[N],head[N],t=1,sz[N],fa[N],indexS,top[N],pos[N],son[N];
struct ss{int to,next;}e[N*2];
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 = 0;
    sz[u] = 1;
    dep[u] = dep[fa[u]] + 1;
    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 = 0;
    pos[u] = ++indexS;
    top[u] = chain;
    if(son[u] > 0)
        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 = 0;
    t = 1;
    memset(sz,0,sizeof(sz));
    memset(head,0,sizeof(head));
    memset(fa,0,sizeof(fa));
    memset(son,0,sizeof(son));
    memset(L,0,sizeof(L));
    memset(sum,0,sizeof(sum));
    memset(lazy,0,sizeof(lazy));
    memset(lazyw,0,sizeof(lazyw));
    memset(color,0,sizeof(color));
    memset(L,0,sizeof(L));
}
void push_down(int i,int ll,int rr) {
    if(lazyw[i] && ll != rr) {
        lazyw[ls] ^= 1;
        if(ll!=1)sum[ls] = (mid-ll+1) - sum[ls];
        else sum[ls] = (mid-ll) - sum[ls];
        lazyw[rs] ^= 1;
        sum[rs] = (rr - mid) - sum[rs];
    }
    lazyw[i] = 0;
}
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 = 0;
    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+1,rr,x,y);
    else {
        res += queryW(ls,ll,mid,x,mid);
        res += queryW(rs,mid+1,rr,mid+1,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] ^= 1;
        else {
            lazy[ls] ^= 1;
            lazy[rs] ^= 1;
        }
        lazy[i] = 0;
    }
    if(ll == rr) return L[i];
    if(x <= mid) return queryL(ls,ll,mid,x);
    else return queryL(rs,mid+1,rr,x);
}
void updateL(int i,int ll,int rr,int x,int y) {
    if(ll == x && rr == y) {
        lazy[i] ^= 1;
        return ;
    }
    if(y <= mid) updateL(ls,ll,mid,x,y);
    else if(x > mid) updateL(rs,mid+1,rr,x,y);
    else {
        updateL(ls,ll,mid,x,mid);
        updateL(rs,mid+1,rr,mid+1,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] ^= 1;
        if(ll != 1)sum[i] = rr-ll+1 - sum[i];
        else sum[i] = rr-ll+1-1 - sum[i];
        return ;
    }
    if(y <= mid) updateW(ls,ll,mid,x,y);
    else if(x > mid) updateW(rs,mid+1,rr,x,y);
    else {
        updateW(ls,ll,mid,x,mid);
        updateW(rs,mid+1,rr,mid+1,y);
    }
    push_up(i,ll,rr);
}
void updateL(int x,int y) {
    int firx = 0, firy = 0;
    while(top[x] != top[y]) {
        if(dep[top[x]] < dep[top[y]]) swap(x,y),swap(firx,firy);
        if(son[x])updateW(1,1,n,pos[son[x]],pos[son[x]]);
        updateL(1,1,n,pos[top[x]],pos[x]);
        x = fa[top[x]];
    }
    if(dep[x] > dep[y])  swap(x,y);
    if(son[x] && x==y)
        updateW(1,1,n,pos[son[x]],pos[son[x]]);
    if(x!=y && son[y])updateW(1,1,n,pos[son[y]],pos[son[y]]);
    if(x != 1 && son[fa[x]] == x) updateW(1,1,n,pos[x],pos[x]);
    updateL(1,1,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(1,1,n,pos[son[top[x]]],pos[x]);
        color[top[x]]^=1;
        x = fa[top[x]];
    }
    if(x == y) {}
    else {
        if(dep[x] < dep[y]  && son[x]) updateW(1,1,n,pos[son[x]],pos[y]);
        else if(son[y])updateW(1,1,n,pos[son[y]],pos[x]);
    }
}
int solve(int x,int y) {
    int res = 0;
    while(top[x] != top[y]) {
        if(dep[top[x]] < dep[top[y]]) swap(x,y);
        if(top[x] != x) res += queryW(1,1,n,pos[son[top[x]]],pos[x]);
        int fx = queryL(1,1,n,pos[top[x]])^queryL(1,1,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(1,1,n,pos[son[x]],pos[y]);
        else if(son[y])res += queryW(1,1,n,pos[son[y]],pos[x]);
    }
    return res;
}
int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        init();
        for(int i = 1; i < n; ++i) {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y),add(y,x);
        }
        dfs(1);
        dfs(1,1);
        int Q;
        scanf("%d",&Q);
        while(Q--) {
            int op,x,y;
            scanf("%d%d%d",&op,&x,&y);
            if(op == 1) {
                updateW(x,y);
            }
            else if(op == 2)
            {
                updateL(x,y);
            }
            else
            {
                if(x == y) puts("0");
                else
                printf("%d\n",solve(x,y));
            }
        }
    }
    return 0;
}

 

posted @ 2017-04-13 17:05  meekyan  阅读(274)  评论(0编辑  收藏  举报