ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU - 3966-Aragorn' Story(树链剖分+线段树)

2019-06-04 08:51:47  阅读:238  来源: 互联网

标签:HDU camps 剖分 int Story Son MAXN include Aragorn


链接:https://vjudge.net/problem/HDU-3966

题意:

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 connect them. It is guaranteed that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the enemy change the number of soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they will increase or decrease K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular camps real-time.

思路:

第一次写树链剖分, 直接上模板。

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
#include <assert.h>
using namespace std;

typedef long long LL;
const int MAXN = 5e4+10;
vector<int> G[MAXN];
int Dis[MAXN], Fa[MAXN], Top[MAXN], Size[MAXN];
int Son[MAXN], Id[MAXN], Rk[MAXN];
int Seg[MAXN*4], A[MAXN], Add[MAXN*4];
int n, m, p;
int x, y;
int cnt = 0;

void Init()
{
    for (int i = 1;i <= n;i++)
        G[i].clear();
    memset(Seg, 0, sizeof(Seg));
    memset(Son, 0, sizeof(Son));
    memset(Add, 0, sizeof(Add));
    cnt = 0;
}

void Dfs1(int x, int u, int dep)
{
    Dis[u] = dep;
    Fa[u] = x;
    Size[u] = 1;
    for (int i = 0;i < G[u].size();i++)
    {
        int v = G[u][i];
        if (v == x)
            continue;
        Dfs1(u, v, dep+1);
        Size[u] += Size[v];
        if (Size[v] > Size[Son[u]])
            Son[u] = v;
    }
}

void Dfs2(int u, int top)
{
    Top[u] = top;
    Id[u] = ++cnt;
    Rk[cnt] = u;
    if (!Son[u])
        return;
    Dfs2(Son[u], top);
    for (int i = 0;i < G[u].size();i++)
    {
        int v = G[u][i];
        if (v != Son[u] && v != Fa[u])
            Dfs2(v, v);
    }
}

void PushUp(int root)
{
    Seg[root] = Seg[root<<1]+Seg[root<<1|1];
}

void PushDown(int root, int l, int r)
{
    if (Add[root])
    {
        int mid = (l+r)/2;
        Add[root<<1] += Add[root];
        Add[root<<1|1] += Add[root];
        Seg[root<<1] += (mid-l+1)*Add[root];
        Seg[root<<1|1] += (r-mid)*Add[root];
        Add[root] = 0;
    }
}

void Build(int root, int l, int r)
{
    if (l == r)
    {
        Seg[root] = A[Rk[l]];
        return;
    }
    int mid = (l+r)/2;
    Build(root<<1, l, mid);
    Build(root<<1|1, mid+1, r);
    PushUp(root);
}

int Query(int root, int l, int r, int ql, int qr)
{
    if (r < ql || qr < l)
        return 0;
    if (ql <= l && r <= qr)
        return Seg[root];
    PushDown(root, l, r);
    int mid = (l+r)/2;
    int res = 0;
    res += Query(root<<1, l, mid, ql, qr);
    res += Query(root<<1|1, mid+1, r, ql, qr);
    PushUp(root);
    return res;
}

void Update(int root, int l, int r, int ql, int qr, int c)
{
    if (r < ql || qr < l)
        return;
    if (ql <= l && r <= qr)
    {
        Seg[root] += (r-l+1)*c;
        Add[root] += c;
        return;
    }
    PushDown(root, l, r);
    int mid = (l+r)/2;
    Update(root<<1, l, mid, ql, qr, c);
    Update(root<<1|1, mid+1, r, ql, qr, c);
    PushUp(root);
}

void UpdateLine(int l, int r, int c)
{
    while (Top[l] != Top[r])
    {
        if (Dis[Top[l]] < Dis[Top[r]])
            swap(l, r);
        Update(1, 1, n, Id[Top[l]], Id[l], c);
        l = Fa[Top[l]];
    }
    if (Id[l] < Id[r])
        Update(1, 1, n, Id[l], Id[r], c);
    else
        Update(1, 1, n, Id[r], Id[l], c);
}

int main()
{
//    freopen("test.in", "r", stdin);
    while (~scanf("%d%d%d", &n, &m, &p))
    {
        Init();
        for (int i = 1;i <= n;i++)
            scanf("%d", &A[i]);
        for (int i = 1;i < n;i++)
        {
            scanf("%d%d", &x, &y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        Dfs1(0, 1, 1);
        Dfs2(1, 1);
        Build(1, 1, n);
//        for (int i = 1;i <= n;i++)
//            cout << Dis[i] << ' ';
//        cout << endl;
//        for (int i = 1;i <= n;i++)
//            cout << Id[i] << ' ';
//        cout << endl;
//        for (int i = 1;i <= n;i++)
//            cout << Top[i] << ' ' ;
//        cout << endl;
        char op[10];
        int l, r, v, w;
        while (p--)
        {
            scanf("%s", op);
            if (op[0] == 'I')
            {
                scanf("%d%d%d", &l, &r, &v);
//                cout << Top[l] << ' ' << Top[r] << endl;
                UpdateLine(l, r, v);
            }
            else if (op[0] == 'D')
            {
                scanf("%d%d%d", &l, &r, &v);
                v = -v;
                UpdateLine(l, r, v);
            }
            else
            {
                scanf("%d", &w);
                printf("%d\n", Query(1, 1, n, Id[w], Id[w]));
            }
        }
    }

    return 0;
}

  

标签:HDU,camps,剖分,int,Story,Son,MAXN,include,Aragorn
来源: https://www.cnblogs.com/YDDDD/p/10971403.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有