[BZOJ2157] 旅游 边权树链剖分

对于边权,我们将其赋值到树边的子节点上就好

查询,修改时,只需要在两点都在一条重链时,序号小的点+1即可

(x,y,min,max写混WA了一发,气,另外需要改一改线段树的写法了,又臭又长)

/**************************************************************
    Problem: 2157
    User: cabinfever
    Language: C++
    Result: Accepted
    Time:584 ms
    Memory:3592 kb
****************************************************************/
 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
using namespace std;
 
const int maxn = 20100;
 
int epos[maxn];
int ee[maxn][3];
struct edge
{
    int v,next;
}e[maxn << 1];
int h[maxn],num = 0;
struct node
{
    int l,r;
    int minn,summ,maxx,mark;
}t[maxn << 1];
int tot = 0;
int a[maxn];
bool vis[maxn];
int n,m,u,v,x,y,d,q;
int cnt = 0;
int fa[maxn];
int dep[maxn];
int son[maxn];
int pos[maxn],ppos[maxn];
int top[maxn];
int size[maxn];
 
void build_edge(int u,int v)
{
    num++;
    e[num].v = v;
    e[num].next = h[u];
    h[u] = num;
}
 
void dfs1(int x)
{
    dep[x] = dep[fa[x]] + 1;
    vis[x] = true;
    size[x] = 1;
    son[x] = 0;
    for(int i = h[x]; i; i = e[i].next)
    {
        if(vis[e[i].v])
            continue;
        fa[e[i].v] = x;
        dfs1(e[i].v);
        if(size[e[i].v] > size[son[x]])
            son[x] = e[i].v;
        size[x] += size[e[i].v];
    }
}
 
void dfs2(int x)
{
    vis[x] = true;
    if(son[fa[x]] == x)
        top[x] = top[fa[x]];
    else
    {
        top[x] = x;
        for(int i = x; i; i = son[i])
        {
            pos[i] = ++cnt;
            ppos[cnt] = i;
        }
    }
    for(int i = h[x]; i; i = e[i].next)
        if(!vis[e[i].v])
            dfs2(e[i].v);
}
 
void build(int p,int l,int r)
{
    int mid = l + r >> 1;
    if(l == r)
        return;
    t[p].l = ++tot;
    t[p].r = ++tot;
    build(t[p].l,l,mid);
    build(t[p].r,mid+1,r);
}
 
void change1(int p,int l,int r,int q,int c)
{
    int mid = l + r >> 1;
    if(l == r)
    {
        t[p].maxx = c;
        t[p].minn = c;
        t[p].summ = c;
        return;
    }
    if(t[p].mark)
    {
        t[t[p].l].summ = -t[t[p].l].summ;
        t[t[p].r].summ = -t[t[p].r].summ;
        swap(t[t[p].l].minn,t[t[p].l].maxx);
        swap(t[t[p].r].minn,t[t[p].r].maxx);
        t[t[p].l].minn = -t[t[p].l].minn;
        t[t[p].r].minn = -t[t[p].r].minn;
        t[t[p].l].maxx = -t[t[p].l].maxx;
        t[t[p].r].maxx = -t[t[p].r].maxx;
        t[p].mark ^= 1;
        t[t[p].l].mark ^= 1;
        t[t[p].r].mark ^= 1;
    }
    if(q <= mid)
    {
        change1(t[p].l,l,mid,q,c);
        t[p].maxx = max(t[t[p].l].maxx,t[t[p].r].maxx);
        t[p].minn = min(t[t[p].l].minn,t[t[p].r].minn);
        t[p].summ = t[t[p].r].summ + t[t[p].l].summ;
    }
    else
    {
        change1(t[p].r,mid+1,r,q,c);
        t[p].maxx = max(t[t[p].l].maxx,t[t[p].r].maxx);
        t[p].minn = min(t[t[p].l].minn,t[t[p].r].minn);
        t[p].summ = t[t[p].r].summ + t[t[p].l].summ;
    }
}
 
void change2(int p,int l,int r,int L,int R)
{
    int mid = l + r >> 1;
    if(l == L && r == R)
    {
        t[p].summ = -t[p].summ;
        swap(t[p].minn,t[p].maxx);
        t[p].minn = -t[p].minn;
        t[p].maxx = -t[p].maxx;
        t[p].mark ^= 1;
        return;
    }
    if(t[p].mark)
    {
        t[t[p].l].summ = -t[t[p].l].summ;
        t[t[p].r].summ = -t[t[p].r].summ;
        swap(t[t[p].l].minn,t[t[p].l].maxx);
        swap(t[t[p].r].minn,t[t[p].r].maxx);
        t[t[p].l].minn = -t[t[p].l].minn;
        t[t[p].r].minn = -t[t[p].r].minn;
        t[t[p].l].maxx = -t[t[p].l].maxx;
        t[t[p].r].maxx = -t[t[p].r].maxx;
        t[p].mark ^= 1;
        t[t[p].l].mark ^= 1;
        t[t[p].r].mark ^= 1;
    }
    if(R <= mid)
    {
        change2(t[p].l,l,mid,L,R);
        t[p].minn = min(t[t[p].r].minn,t[t[p].l].minn);
        t[p].maxx = max(t[t[p].r].maxx,t[t[p].l].maxx);
        t[p].summ = t[t[p].r].summ + t[t[p].l].summ;
    }
    else if(L > mid)
    {
        change2(t[p].r,mid+1,r,L,R);
        t[p].minn = min(t[t[p].r].minn,t[t[p].l].minn);
        t[p].maxx = max(t[t[p].r].maxx,t[t[p].l].maxx);
        t[p].summ = t[t[p].r].summ + t[t[p].l].summ;
    }
    else
    {
        change2(t[p].l,l,mid,L,mid);
        change2(t[p].r,mid+1,r,mid+1,R);
        t[p].minn = min(t[t[p].r].minn,t[t[p].l].minn);
        t[p].maxx = max(t[t[p].r].maxx,t[t[p].l].maxx);
        t[p].summ = t[t[p].r].summ + t[t[p].l].summ;
    }
}
 
int getMax(int p,int l,int r,int L,int R)
{
    int mid = l + r >> 1;
    if(l == L && r == R)
        return t[p].maxx;
    if(t[p].mark)
    {
        t[t[p].l].summ = -t[t[p].l].summ;
        t[t[p].r].summ = -t[t[p].r].summ;
        swap(t[t[p].l].minn,t[t[p].l].maxx);
        swap(t[t[p].r].minn,t[t[p].r].maxx);
        t[t[p].l].minn = -t[t[p].l].minn;
        t[t[p].r].minn = -t[t[p].r].minn;
        t[t[p].l].maxx = -t[t[p].l].maxx;
        t[t[p].r].maxx = -t[t[p].r].maxx;
        t[p].mark ^= 1;
        t[t[p].l].mark ^= 1;
        t[t[p].r].mark ^= 1;
    }
    if(R <= mid)
        return getMax(t[p].l,l,mid,L,R);
    else if(L > mid)
        return getMax(t[p].r,mid+1,r,L,R);
    return max(getMax(t[p].l,l,mid,L,mid),getMax(t[p].r,mid+1,r,mid+1,R));
}
 
int getMin(int p,int l,int r,int L,int R)
{
    int mid = l + r >> 1;
    if(l == L && r == R)
        return t[p].minn;
    if(t[p].mark)
    {
        t[t[p].l].summ = -t[t[p].l].summ;
        t[t[p].r].summ = -t[t[p].r].summ;
        swap(t[t[p].l].minn,t[t[p].l].maxx);
        swap(t[t[p].r].minn,t[t[p].r].maxx);
        t[t[p].l].minn = -t[t[p].l].minn;
        t[t[p].r].minn = -t[t[p].r].minn;
        t[t[p].l].maxx = -t[t[p].l].maxx;
        t[t[p].r].maxx = -t[t[p].r].maxx;
        t[p].mark ^= 1;
        t[t[p].l].mark ^= 1;
        t[t[p].r].mark ^= 1;
    }
    if(R <= mid)
        return getMin(t[p].l,l,mid,L,R);
    else if(L > mid)
        return getMin(t[p].r,mid+1,r,L,R);
    return min(getMin(t[p].l,l,mid,L,mid),getMin(t[p].r,mid+1,r,mid+1,R));
}
 
int getSum(int p,int l,int r,int L,int R)
{
    int mid = l + r >> 1;
    if(l == L && r ==  R)
        return t[p].summ;
    if(t[p].mark)
    {
        t[t[p].l].summ = -t[t[p].l].summ;
        t[t[p].r].summ = -t[t[p].r].summ;
        swap(t[t[p].l].minn,t[t[p].l].maxx);
        swap(t[t[p].r].minn,t[t[p].r].maxx);
        t[t[p].l].minn = -t[t[p].l].minn;
        t[t[p].r].minn = -t[t[p].r].minn;
        t[t[p].l].maxx = -t[t[p].l].maxx;
        t[t[p].r].maxx = -t[t[p].r].maxx;
        t[p].mark ^= 1;
        t[t[p].l].mark ^= 1;
        t[t[p].r].mark ^= 1;
    }
    if(R <= mid)
        return getSum(t[p].l,l,mid,L,R);
    else if(L > mid)
        return getSum(t[p].r,mid+1,r,L,R);
    return getSum(t[p].l,l,mid,L,mid) + getSum(t[p].r,mid+1,r,mid+1,R);
}
 
void updata(int x,int y)
{
    int fx = top[x];
    int fy = top[y];
    int l,r;
    while(fx != fy)
    {
        if(dep[fx] > dep[fy])
        {
            change2(0,1,n,pos[fx],pos[x]);
            x = fa[fx];
            fx = top[x];
        }
        else
        {
            change2(0,1,n,pos[fy],pos[y]);
            y = fa[fy];
            fy = top[y];
        }
    }
    if(x == y)
        return;
    if(pos[x] > pos[y])
        l = pos[y]+1, r = pos[x];
    else
        l = pos[x]+1, r = pos[y];
    change2(0,1,n,l,r);
    return;
}
 
int query_max(int x,int y)
{
    int ans = -1000000000;
    int fx = top[x];
    int fy = top[y];
    int l,r;
    while(fx != fy)
    {
        if(dep[fx] > dep[fy])
        {
            ans = max(ans,getMax(0,1,n,pos[fx],pos[x]));
            x = fa[fx];
            fx = top[x];
        }
        else
        {
            ans = max(ans,getMax(0,1,n,pos[fy],pos[y]));
            y = fa[fy];
            fy = top[y];
        }
    }
    if(x == y)
        return ans;
    if(pos[x] > pos[y])
        l = pos[y]+1, r = pos[x];
    else
        l = pos[x]+1, r = pos[y];
    ans = max(ans,getMax(0,1,n,l,r));
    return ans;
}
 
int query_min(int x,int y)
{
    int ans = 1000000000;
    int fx = top[x];
    int fy = top[y];
    int l,r;
    while(fx != fy)
    {
        if(dep[fx] > dep[fy])
        {
            ans = min(ans,getMin(0,1,n,pos[fx],pos[x]));
            x = fa[fx];
            fx = top[x];
        }
        else
        {
            ans = min(ans,getMin(0,1,n,pos[fy],pos[y]));
            y = fa[fy];
            fy = top[y];
        }
    }
    if(x == y)
        return ans;
    if(pos[x] > pos[y])
        l = pos[y]+1, r = pos[x];
    else
        l = pos[x]+1, r = pos[y];
    ans = min(ans,getMin(0,1,n,l,r));
    return ans;
}
 
int query_sum(int x,int y)
{
    int ans = 0;
    int fx = top[x];
    int fy = top[y];
    int l,r;
    while(fx != fy)
    {
        if(dep[fx] > dep[fy])
        {
            ans += getSum(0,1,n,pos[fx],pos[x]);
            x = fa[fx];
            fx = top[x];
        }
        else
        {
            ans += getSum(0,1,n,pos[fy],pos[y]);
            y = fa[fy];
            fy = top[y];
        }
    }
    if(x == y)
        return ans;
    if(pos[x] > pos[y])
        l = pos[y]+1, r = pos[x];
    else
        l = pos[x]+1, r = pos[y];
    ans += getSum(0,1,n,l,r);
    return ans;
}
 
int main()
{
    scanf("%d",&n);
    for(int i = 0; i < n - 1; i++)
    {
        scanf("%d%d%d",&ee[i][0],&ee[i][1],&ee[i][2]);
        ee[i][0]++;
        ee[i][1]++;
        build_edge(ee[i][0],ee[i][1]);
        build_edge(ee[i][1],ee[i][0]);
    }
    memset(vis,0,sizeof(vis));
    dfs1(1);
    memset(vis,0,sizeof(vis));
    dfs2(1);
    for(int i = 0; i < n - 1; i++)
        if(fa[ee[i][0]] == ee[i][1])
            epos[i+1] = ee[i][0];
        else
            epos[i+1] = ee[i][1];
    build(0,1,n);
    for(int i = 1; i < n; i++)
        change1(0,1,n,pos[epos[i]],ee[i-1][2]);
    char q[10];
    scanf("%d",&m);
    while(m--)
    {
        scanf("%s",q);
        scanf("%d%d",&x,&y);
        x++,y++;
        if(!strcmp(q,"C"))
        {
            y--;
            x--;
            change1(0,1,n,pos[epos[x]],y);
        }
        else if(!strcmp(q,"MAX"))
        {
            printf("%d\n",query_max(x,y));
        }
        else if(!strcmp(q,"N"))
        {
            updata(x,y);
        }
        else if(!strcmp(q,"MIN"))
        {
            printf("%d\n",query_min(x,y));
        }
        else
        {
            printf("%d\n",query_sum(x,y));
        }
    }
    return 0;
}

你可能感兴趣的:(bzoj,数据结构——树链剖分)