HDU - 3966 Aragorn's Story(树链剖分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cherish0222/article/details/77506566

点我看题

题意:n个营地,m条边连接他们,可以对这n个营地进行三种操作,I C1 C2 val,在C1~C2(包含C1C2)增援val人,D C1 C2 val,在C1,~C2减少val人,Q C,看C营地还有多少人(可以为负的)。

分析:直接树链剖分,然后查询,其实树链剖分的两个搜索基本固定,主要还是线段树的问题。(个人感觉这个题有点奇怪,用树链剖分解的话,这个m应该是恒等于n-1的

结构体存线段树的内容会超时QAQ

参考代码:

/*树链剖分*/
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>

using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson rt<<1
#define rson rt<<1|1
const int maxn = 5e4+10;
int n,m,q;
int a[maxn];
int head[maxn],tot;
struct Edge{
    int to,next;
}edge[maxn<<1];
int siz[maxn],fa[maxn],son[maxn],dep[maxn];
int p[maxn],fp[maxn],top[maxn],pos;
//线段树
int num[maxn<<2],add[maxn<<2];

void Init()
{
    tot = pos = 0;
    mem(head,-1);
    mem(son,-1);
}

void AddEdge( int u, int v)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}

void dfs1( int u, int pre, int d)
{
    siz[u] = 1;
    fa[u] = pre;
    dep[u] = d;
    for( int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if( v != pre)
        {
            dfs1(v,u,d+1);
            siz[u] == siz[v];
            if( son[u] == -1 || siz[v] > siz[son[u]])
                son[u] = v;
        }
    }
}

void dfs2( int u, int tp)
{
    top[u] = tp;
    p[u] = ++pos;
    fp[p[u]] = u;
    if( son[u] == -1)
        return;
    dfs2(son[u],tp);
    for( int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if( v != son[u] && v != fa[u])
            dfs2(v,v);
    }
}

void PushUp( int rt)
{
    num[rt] = num[lson]+num[rson];
}

void Build( int l, int r, int rt)
{
    add[rt] = 0;
    if( l == r)
    {
        num[rt] = a[fp[l]];
        return;
    }
    int mid = (l+r)>>1;
    Build(l,mid,lson);
    Build(mid+1,r,rson);
    PushUp(rt);
}

void PushDown( int rt, int m)
{
    if( add[rt])
    {
        add[lson] += add[rt];
        add[rson] += add[rt];
        num[lson] += (m-(m>>1))*add[rt];
        num[rson] += (m>>1)*add[rt];
        add[rt] = 0;
    }
}

void Update( int l, int r, int rt, int L, int R, int val)
{
    if( L <= l && R >= r)
    {
        num[rt] += val*(r-l+1);
        add[rt] += val;
        return;
    }
    PushDown(rt,r-l+1);
    int mid = (l+r)>>1;
    if( R <= mid)
        Update(l,mid,lson,L,R,val);
    else if( L > mid)
        Update(mid+1,r,rson,L,R,val);
    else
    {
        Update(l,mid,lson,L,mid,val);
        Update(mid+1,r,rson,mid+1,R,val);
    }
    PushUp(rt);
}

int Query( int l, int r, int rt, int pos)
{
    if( l == pos && r == pos)
        return num[rt];
    PushDown(rt,r-l+1);
    int mid = (l+r)>>1;
    if( pos <= mid)
        return Query(l,mid,lson,pos);
    else
        return Query(mid+1,r,rson,pos);
    PushUp(rt);
}

void Change( int u, int v, int val)
{
    int t1 = top[u];
    int t2 = top[v];
    while( t1 != t2)
    {
        if( dep[t1] < dep[t2])
        {
            swap(u,v);
            swap(t1,t2);
        }
        Update(1,pos,1,p[t1],p[u],val);
        u = fa[t1];
        t1 = top[u];
    }
    if( dep[u] > dep[v])
        swap(u,v);
    Update(1,pos,1,p[u],p[v],val);
}

int main()
{
    while( ~scanf("%d%d%d",&n,&m,&q))
    {
        Init();
        for( int i = 1; i <= n; i++)
            scanf("%d",&a[i]);
        int u,v;
        for( int i = 1; i <= m; i++)
        {
            scanf("%d%d",&u,&v);
            AddEdge(u,v);
            AddEdge(v,u);
        }
        dfs1(1,0,0);
        dfs2(1,1);
        Build(1,pos,1);
        while( q--)
        {
            char op[5];
            int c1,c2,k;
            scanf("%s",op);
            if( op[0] == 'Q')
            {
                scanf("%d",&k);
                printf("%d\n",Query(1,pos,1,p[k]));
            }
            else
            {
                scanf("%d%d%d",&c1,&c2,&k);
                if( op[0] == 'D')
                    k = -k;
                Change(c1,c2,k);
            }
        }
    }

    return 0;
}


展开阅读全文

没有更多推荐了,返回首页