SPOJ 16580 QTREE7 - Query on a tree VII

终于是完成了QTREE系列。

我还是想说 , 这貌似链分治总是可以搞定的 , 如果不行 , 就加一个线段树 , 如果还是不行 , 就加一个堆。

由于QTREE6苦逼地在 findPath() 里多了一个 lg(n) 这一次 , 我在线段树里加了两个量来维护一个结点在重链左右能够到达的区域长度。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>

using namespace std;
const int maxn = 1e5+1e2;

int n , dfsCnt;
int fa[maxn] , id[maxn] , reid[maxn] , bl[maxn] , Size[maxn] , s[maxn] , c[maxn] , w[maxn];
vector<int> g[maxn];

void dfs(int u)
{
    Size[u] = 1;
    for(int i=0;i<g[u].size();i++)
    {
        int t = g[u][i];
        if(t == fa[u]) continue;

        fa[t] = u;
        dfs(t);
        Size[u] += Size[t];
    }
}

void dfs(int u , int num)
{
    reid[id[u] = ++dfsCnt] = u;
    bl[u] = num;
    s[num]++;

    int mx = 0 , w;
    for(int i=0;i<g[u].size();i++)
    {
        int t = g[u][i];
        if(t == fa[u]) continue;

        if(mx < Size[t]) mx = Size[t] , w = t;
    }

    if(mx) dfs(w, num);

    for(int i=0;i<g[u].size();i++)
    {
        int t = g[u][i];
        if(t == fa[u] || t == w) continue;
        dfs(t, t);      
    }
}

struct node 
{ 
    int l , r , lc , rc;
    node(){} 
    node(int l , int r , int lc , int rc):l(l) , r(r) , lc(lc) , rc(rc){} 
};

template <typename T>
struct discendingOrder
{
    bool operator ()(const T& l , const T& r)const { return l>r; }
};

int cnt;
node seg[maxn*4];
int ls[maxn*4] , rs[maxn*4] , root[maxn];
set<int , discendingOrder<int> > bw[maxn][2];

node merge(node& a , node& b , int l1 , int l2 , bool same)
{
    node res = node(a.l, b.r, a.lc, b.rc);
    if(a.lc == l1 && same) res.l = max(res.l, b.l) , res.lc += b.lc;
    if(b.rc == l2 && same) res.r = max(res.r, a.r) , res.rc += a.rc;
    return res;
}

void maintain(int o , int x)
{
    int hea = w[x];
    if(bw[x][c[x]].size()) hea = max(hea , *bw[x][c[x]].begin());
    seg[o].l = seg[o].r = hea;
    seg[o].lc = seg[o].rc = 1;
}

void build(int o , int l , int r)
{
    if(l==r)
    {
        int x = reid[l];
        for(int i=0;i<g[x].size();i++)
        {
            int t = g[x][i];
            if(t == fa[x] || bl[t] == bl[x]) continue;
            build(root[t] = ++cnt, id[t], id[t]+s[t]-1);
            bw[x][c[t]].insert(seg[root[t]].l);
        }

        maintain(o, x);
    }
    else 
    {
        int mid = (l+r)/2;
        build(ls[o] = ++cnt, l, mid);
        build(rs[o] = ++cnt, mid+1, r);
        seg[o] = merge(seg[ls[o]], seg[rs[o]], mid-l+1, r-mid, c[reid[mid]]==c[reid[mid+1]]);
    }
}

deque<int> pat;
void findPath(int x , bool noted = false)
{
    pat.clear();
    while(x)
    {
        int f = bl[x];
        if(noted)
        {
            if(id[f] + seg[root[f]].lc - 1 < id[x] || !fa[f] || c[fa[f]]!=c[f])
            {
                pat.push_front(x);
                pat.push_front(f);
                break;
            }
        }
        else 
        {
            pat.push_front(x);
            pat.push_front(f);
        }
        x = fa[f];
    }
}

void modify(int o , int l , int r , int i , bool w = true)
{
    if(l==r)
    {
        int x = reid[l];
        if(i+2 < pat.size())
        {
            int t = pat[i+1];
            bw[x][c[t]].erase(bw[x][c[t]].find(seg[root[t]].l));
            modify(root[t], id[t], id[t]+s[t]-1, i+2 , w);
            bw[x][c[t]].insert(seg[root[t]].l);
        }
        else if(w) c[x] = 1-c[x];
        maintain(o, x);
    }
    else
    {
        int mid = (l+r)/2;
        if(id[pat[i]] <= mid) modify(ls[o], l, mid, i , w);
        else modify(rs[o], mid+1, r, i , w);
        seg[o] = merge(seg[ls[o]], seg[rs[o]], mid-l+1, r-mid, c[reid[mid]]==c[reid[mid+1]]);
    }
}

int query(int o , int l , int r , int i)
{
    if(l==r) return seg[o].l; 
    else 
    {
        int mid = (l+r)/2 , t = id[pat[i]] , res = 0;
        if(t <= mid)
        {
            res = query(ls[o], l, mid, i);
            if(mid - seg[ls[o]].rc + 1 <= t && c[reid[mid]]==c[reid[mid+1]]) res = max(res , seg[rs[o]].l);
        }
        else 
        {
            res = query(rs[o], mid+1, r, i);
            if(mid + seg[rs[o]].lc >= t && c[reid[mid]]==c[reid[mid+1]]) res = max(res , seg[ls[o]].r);
        }
        return res;
    }
}


int main(int argc, char *argv[]) {

    cin>>n;
    for(int i=1;i<n;i++)
    {
        int a ,b;
        scanf("%d%d" , &a , &b);
        g[a].push_back(b);
        g[b].push_back(a);
    }

    for(int i=1;i<=n;i++) scanf("%d" , &c[i]);
    for(int i=1;i<=n;i++) scanf("%d" , &w[i]);


    dfs(1);
    dfs(1, 1);
    build(root[1] = ++cnt, 1, s[1]);
    int t;
    scanf("%d" , &t);

    while(t--)
    {
        int ty , x , y;
        scanf("%d%d" , &ty , &x);
        if(ty==2) scanf("%d" , &y);
        if(!ty) 
        {
            findPath(x , 1);
            int now = pat[0];
            printf("%d\n" , query(root[now], id[now], id[now]+s[now]-1, 1));
        }
        else if(ty==1)
        {
            findPath(x);
            modify(1, 1, s[1], 1);
        }
        else 
        {
            findPath(x);
            w[x] = y;
            modify(1, 1, s[1], 1 , false);
        }
    }

    return 0;
}

你可能感兴趣的:(树的分治)