Hdu3966( Aragorn'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;
int head[maxn];
int top[maxn];
struct Node
{
    int to; int next;
}e[maxn * 2];
int father[maxn];
int deep[maxn];
int size[maxn];
int son[maxn];
int pos[maxn];
int color[maxn << 2];
int vis[maxn];
int len, z;
void add(int from, int to)
{
    e[len].to = to;
    e[len].next = head[from];
    head[from] = len++;
}

void init(int x)
{
    size[x] = 1; son[x] = 0;
    for (int i = head[x]; i != -1; i = e[i].next){
        int cc = e[i].to;
        if (cc == father[x]) continue;
        father[cc] = x; deep[cc] = deep[x] + 1;
        init(cc);
        size[x] += size[cc];
        if (size[cc] > size[son[x]]) son[x] = cc;
    }
}

void dfs(int x, int tp)
{
    pos[x] = ++z; top[x] = tp;
    if (son[x]) dfs(son[x], tp);
    for (int i = head[x]; i != -1; i = e[i].next){
        int cc = e[i].to;
        if (cc == father[x] || cc == son[x]) continue;
        dfs(cc, cc);
    }
}

void down(int rt)
{
    if (color[rt]){
        color[rt << 1] += color[rt];
        color[rt << 1 | 1] += color[rt];
        color[rt] = 0;
    }
}

void update(int L, int R, int ans, int l, int r, int rt)
{
    if (L <= l&&r <= R){
        color[rt] += ans; return;
    }
    int mid = (l + r) >> 1;
    down(rt);
    if (L <= mid) update(L, R, ans, lson);
    if (R > mid) update(L, R, ans, rson);
}

int ask(int key, int l, int r, int rt)
{
    if (l == r) return color[rt];
    down(rt);
    int mid = (l + r) >> 1;
    if (key <= mid) return ask(key, lson);
    else return ask(key, rson);
}

void gao(int x, int y, int ans)
{
    int f1 = top[x]; int f2 = top[y];
    while (f1 != f2){
        if (deep[f1] < deep[f2]){
            swap(f1, f2); swap(x, y);
        }
        update(pos[f1], pos[x], ans, 1, z, 1);
        x = father[f1]; f1 = top[x];
    }
    if (deep[x]>deep[y]){
        swap(x, y);
    }
    update(pos[x], pos[y], ans, 1, z, 1);
}

int main()
{
    int n, m, k;
    int a, b, c;
    char str[100];
    while (cin >> n >> m >> k){
        memset(head, -1, sizeof(head));
        memset(color, 0, sizeof(color));
        memset(size, 0, sizeof(size));
        deep[1] = 1;
        len = 0; z = 0;
        for (int i = 1; i <= n; i++){
            scanf("%d", &vis[i]);
        }
        for (int i = 0; i < m; i++){
            scanf("%d%d", &a, &b);
            add(a, b); add(b, a);
        }
        init(1); dfs(1, 1);
        for (int i = 0; i < k; i++){
            scanf("%s", str);
            if (str[0] == ‘Q‘){
                scanf("%d", &a);
                printf("%d\n", vis[a] + ask(pos[a], 1, z, 1));
            }
            if (str[0] == ‘I‘){
                scanf("%d%d%d", &a, &b, &c);
                gao(a, b, c);
            }
            if (str[0] == ‘D‘){
                scanf("%d%d%d", &a, &b, &c);
                gao(a, b, -c);
            }
        }
    }
    return 0;
}

Hdu3966( Aragorn's Story ) 树链剖分

时间: 2024-10-10 04:07:55

Hdu3966( Aragorn's Story ) 树链剖分的相关文章

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,

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>

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

HDU Aragorn'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 k