HDU3966-Aragorn's Story-树链剖分-点权

很模板的树链剖分题

注意什么时候用线段树上的标号,什么时候用点的标号。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;
const int maxn = 50005;
int val_pre[maxn],val[maxn],siz[maxn],son[maxn],id[maxn],fa[maxn],top[maxn],dep[maxn];
int topw;
int M,N,P;

vector<int> G[maxn];

void dfs_1(int u,int f,int d)
{
    siz[u] = 1;
    fa[u]  = f;
    dep[u] = d;
    son[u] = 0;
    for(int i=0;i<G[u].size();i++)
    {
        int v = G[u][i];
        if(v == f) continue;
        dfs_1(v,u,d+1);
        siz[u] += siz[v];
        if(siz[son[u]] < siz[v])
            son[u] = v;
    }
}

void dfs_2(int u,int tp)
{
    top[u] = tp;
    id[u] = ++topw;
    if(son[u]) dfs_2(son[u],tp);
    for(int i=0;i<G[u].size();i++)
    {
        int v = G[u][i];
        if(v == fa[u]|| v==son[u]) continue;
        dfs_2(v,v);
    }
}

void debug()
{
    for(int i=1;i<=N;i++)
    {
        printf("%d siz:%d son:%d dep:%d fa:%d ",i,siz[i],son[i],dep[i],fa[i]);
        printf("top:%d id:%d\n",top[i],id[i]);
    }
}

////////////////////////////////////////
//Segment Tree

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int add[maxn<<2];
void push_down(int rt)
{
    if(add[rt])
    {
        add[rt<<1] += add[rt];
        add[rt<<1|1] += add[rt];
        add[rt] = 0;
    }
}

void build(int l,int r,int rt)
{
    add[rt] = 0;
    if(l == r)
    {
        add[rt] = val[r];
        return ;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
}

void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l && R>= r)
    {
        add[rt] += c;
        return ;
    }
    push_down(rt);
    int m = (l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(R> m) update(L,R,c,rson);
}

int query(int x,int l,int r,int rt)
{
    if(l == r)
    {
        return add[rt];
    }
    push_down(rt);
    int m = (l+r)>>1;
    if(x <= m) query(x,lson);
    else       query(x,rson);
}

void change(int u,int v,int add)
{
    int fu = top[u],fv = top[v];
    while(fu != fv)
    {
        //printf("fu:%d u:%d v:%d fv:%d \n",fu,u,v,fv);
        if(dep[fu] < dep[fv])
        {
            swap(u,v);swap(fu,fv);
        }
        update(id[fu],id[u],add,1,topw,1);
        u = fa[fu];
        fu = top[u];
    }
    if(u == v)
    {
        update(id[u],id[v],add,1,topw,1);
        return;
    }
    else{
        if(dep[u] > dep[v]) swap(u,v);
        update(id[u],id[v],add,1,topw,1);
        return ;
    }
}

int main()
{
    //freopen("input.in","r",stdin);
    while(~scanf("%d%d%d",&N,&M,&P))
    {
        for(int i=1;i<=N;i++) scanf("%d",&val_pre[i]);
        for(int i=1,a,b;i<=M;i++)
        {
            scanf("%d%d",&a,&b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        topw = 0;
        dfs_1(1,0,1);
        dfs_2(1,1);
        for(int i=1;i<=N;i++)
        {
            val[id[i]] = val_pre[i];
        }
        build(1,topw,1);
        //debug();

        char op[10];
        int a,b,c;

        for(int i=0;i<P;i++)
        {
            scanf("%s",op);
            if(op[0] == ‘I‘)
            {
                scanf("%d%d%d",&a,&b,&c);
                change(a,b,c);
            }
            else if(op[0] == ‘D‘)
            {
                scanf("%d%d%d",&a,&b,&c);
                change(a,b,-c);
            }
            else if(op[0] == ‘Q‘)
            {
                scanf("%d",&a);
                int ans = query(id[a],1,topw,1);
                printf("%d\n",ans);
            }
        }
        for(int i=0;i<maxn;i++) G[i].clear();
    }
}

HDU3966-Aragorn's Story-树链剖分-点权

时间: 2024-11-15 19:21:45

HDU3966-Aragorn's Story-树链剖分-点权的相关文章

Hdu3966( Aragorn&#39;s Story ) 树链剖分

点更新树链剖分入门题,诶 错了好多发,提到同一点时没更新,边数组开小了一倍. #include <cstdio> #include <algorithm> #include <iostream> #include <string.h> using namespace std; #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 const int maxn = 55555; in

HDU-3966 Aragorn&#39;s Story(树链剖分+线段树)

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12479    Accepted Submission(s): 3331 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

HDU3966 Aragorn&#39;s Story 树链剖分+线段树

区间更新,单点查询,,,,奇葩,HDU上强行加了扩栈才过. 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 #define lson l,m,rt<<1 8 #define rson m+1,

HDU3966 Aragorn&#39;s Story(树链剖分 点权 模版题)

#include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <map> #include <set> #include <vector> #include <cstdio> using namespace std; const int N=50010; s

Hdu 3966 Aragorn&#39;s Story (树链剖分 + 线段树区间更新)

题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2:( D, i, j, x ) 从i到j的路径上经过的节点全部都减去x: 3:(Q, x) 查询节点x的权值为多少? 解题思路: 可以用树链剖分对节点进行hash,然后用线段树维护(修改,查询),数据范围比较大,要对线段树进行区间更新 1 #include <cstdio> 2 #include

Hdu 3699 Aragorn&#39;s Story (树链剖分)

题目大意: 对一颗树上进行路径加减,然后询问单点的值. 思路分析: 简单的树链剖分模板题. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #pragma comment(linker,"/STACk:10240000,10240000") #define maxn 50005 #define lson num<<1,s

HDU 2966 Aragorn&#39;s Story 树链剖分第一题 基础题

Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges c

HDU 3966 Aragorn&#39;s Story 树链剖分

Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966 这题注意要手动扩栈. 这题我交g++无限RE,即使手动扩栈了,但交C++就过了. 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include &

HDU 3966 Aragorn&#39;s Story 树链剖分+BIT区间修改/单点询问

Aragorn's Story Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M

hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

题目链接:hdu 3966 Aragorn's Story 题目大意:给定一个棵树,然后三种操作 Q x:查询节点x的值 I x y w:节点x到y这条路径上所有节点的值增加w D x y w:节点x到y这条路径上所有节点的值减少w 解题思路:树链剖分,用树状数组维护每个节点的值. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cstring>