HUD6162(树链剖分)

本文介绍了一种利用离线处理结合树链剖分解决特定路径问题的方法,通过将问题转化为区间查询,实现对树上节点价值的有效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ch’s gift

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 613    Accepted Submission(s): 212


Problem Description
Mr. Cui is working off-campus and he misses his girl friend very much. After a whole night tossing and turning, he decides to get to his girl friend's city and of course, with well-chosen gifts. He knows neither too low the price could a gift be since his girl friend won't like it, nor too high of it since he might consider not worth to do. So he will only buy gifts whose price is between [a,b].
There are n cities in the country and (n-1) bi-directional roads. Each city can be reached from any other city. In the ith city, there is a specialty of price ci Cui could buy as a gift. Cui buy at most 1 gift in a city. Cui starts his trip from city s and his girl friend is in city t. As mentioned above, Cui is so hurry that he will choose the quickest way to his girl friend(in other words, he won't pass a city twice) and of course, buy as many as gifts as possible. Now he wants to know, how much money does he need to prepare for all the gifts?
 

Input
There are multiple cases.

For each case:
The first line contains tow integers n,m(1≤n,m≤10^5), representing the number of cities and the number of situations.
The second line contains n integers c1,c2,...,cn(1≤ci≤10^9), indicating the price of city i's specialty.
Then n-1 lines follows. Each line has two integers x,y(1≤x,y≤n), meaning there is road between city x and city y.
Next m line follows. In each line there are four integers s,t,a,b(1≤s,t≤n;1≤a≤b≤10^9), which indicates start city, end city, lower bound of the price, upper bound of the price, respectively, as the exact meaning mentioned in the description above
 

Output
Output m space-separated integers in one line, and the ith number should be the answer to the ith situation.
 

Sample Input
  
5 3 1 2 1 3 2 1 2 2 4 3 1 2 5 4 5 1 3 1 1 1 1 3 5 2 3
 

Sample Output
  
7 1 4
 

Source

解题思路:比赛时知道要离线处理,也知道大概是树链剖分或者dfs序之类的东西,但是对于链上的每个点的权值要在[l, r]之间这里我不知道怎样处理,其实对于这个问题,我们只需要把这个区间分为两部分[1, l - 1], [1, r],然后二者相减就行,然后我们把所有点的权值从小到大排序,把所有询问按位置大小从小到大排个序,之后用树链剖分弄一下就行。。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100000 + 10;
struct query{
    int id;
    LL flag;
    int s, t;
    LL sum;
    LL loc;
    query(){
        sum = 0;
    }
    bool operator <(const query &res) const{
        return loc < res.loc;
    }

}Query[maxn<<1];
struct node{
    LL value;
    int id;
    bool operator <(const node &res) const{
        return value < res.value;
    }
}Node[maxn];
int n, m;
LL re[maxn];
LL Tree[maxn];
int root;
vector<int> g[maxn];
int depth[maxn];
int father[maxn];
int Rank[maxn];
int son[maxn];
int Size[maxn];
int head[maxn];
int cnt;
int querysum;
int Hash[maxn][2];
int lowbit(int x)
{
    return x&(-x);
}
void update(int loc, LL value)
{
    for(int i = loc; i <= n; i += lowbit(i))
    {
        Tree[i] += value;
    }
}
LL get(int loc)
{
    LL ans = 0;
    for(int i = loc; i >= 1; i -= lowbit(i))
    {
        ans += Tree[i];
    }
    return ans;
}
void dfs1(int u, int d, int f)
{
    depth[u] = d;
    Size[u] = 1;
    son[u] = -1;
    int Max = -1;
    for(int i = 0; i < g[u].size(); i++)
    {
        int v = g[u][i];
        if(v == f) continue;
        dfs1(v, d + 1, u);
        father[v] = u;
        if(Size[v] > Max)
        {
            Max = Size[v];
            son[u] = v;
        }
        Size[u] += Size[v];

    }
}
void dfs2(int u, int f, int fp)
{
    head[u] = fp;
    Rank[u] = ++cnt;
    if(son[u] != -1)
    {
        dfs2(son[u], u, fp);
    }
    for(int i = 0; i < g[u].size(); i++)
    {
        int v = g[u][i];
        if(v == f || v == son[u]) continue;
        dfs2(v, u, v);
    }
}
LL querySum(int l, int r)
{
    return get(Rank[r]) - get(Rank[l] - 1);
}
LL solveSum(int u, int v)
{
    int f1 = head[u];
    int f2 = head[v];
    LL ans = 0;
    while(f1 != f2)
    {
        if(depth[f1] > depth[f2])
        {
            ans += querySum(f1, u);
            u = father[f1];
        }
        else
        {
            ans += querySum(f2, v);
            v = father[f2];
        }
        f1 = head[u];
        f2 = head[v];
    }
    if(depth[u] > depth[v])
    {
       ans += querySum(v, u);
    }
    else ans += querySum(u, v);
    return ans;
}
void init()
{
    memset(Tree, 0, sizeof(Tree));
    memset(re, 0, sizeof(re));
    root = 1;
    cnt = 0;
    querysum = 0;
    for(int i = 1; i <= n; i++)
    {
        g[i].clear();
    }
}
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        init();
        for(int i = 1; i <= n; i++)
        {
            scanf("%lld", &Node[i].value);
            Node[i].id = i;
        }
        sort(Node + 1, Node + n + 1);
        int x, y;
        for(int i = 1; i < n; i++)
        {
            scanf("%d%d", &x, &y);
            g[x].push_back(y);
            g[y].push_back(x);
        }
        dfs1(root, 0, root);
        dfs2(root, root, root);
        int s, t;
        LL a, b;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d%lld%lld", &s, &t, &a, &b);
            Query[++querysum].loc = a - 1;
            Query[querysum].id = i;
            Query[querysum].s = s;
            Query[querysum].t = t;
            Query[querysum].sum = 0;
            Query[querysum].flag = -1;
            Query[++querysum].loc = b;
            Query[querysum].id = i;
            Query[querysum].s = s;
            Query[querysum].t = t;
            Query[querysum].sum = 0;
            Query[querysum].flag = 1;
        }
        sort(Query + 1, Query + querysum + 1);
        int p1 = 1;
        int p2 = 1;
        while(p2 <= n && p1 <= querysum)
        {
            while(Node[p2].value <= Query[p1].loc && p2 <= n)
            {
                update(Rank[Node[p2].id], Node[p2].value);
                p2++;
            }
            Query[p1].sum = solveSum(Query[p1].s, Query[p1].t);
            p1++;
        }
        while(p1 <= querysum)
        {
            Query[p1].sum = solveSum(Query[p1].s, Query[p1].t);
            p1++;
        }
        for(int i = 1; i <= querysum; i++)
        {
            re[Query[i].id] += Query[i].sum * Query[i].flag;
        }
        for(int i = 1; i <= m; i++)
        {
            if(i == m) printf("%lld\n", re[i]);
            else printf("%lld ", re[i]);
        }
    }
    return 0;
}


 

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值