HDU 5861 Road(线段树 区间修改 单点查询)

本文介绍了一个关于路段收费的算法问题,包含N个村庄通过N-1段道路连接,每段道路有不同的费用。需要通过算法确定如何打开和关闭路段以达到最小化总成本的目标。文章详细解释了解决方案,并提供了具体的实现代码。

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

Road

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1132    Accepted Submission(s): 309

Problem Description
There are n villages along a high way, and divided the high way into n-1 segments. Each segment would charge a certain amount of money for being open for one day, and you can open or close an arbitrary segment in an arbitrary day, but you can open or close the segment for just one time, because the workers would be angry if you told them to work multiple period.
We know the transport plan in the next m days, each day there is one cargo need to transport from village  ai to village bi, and you need to guarantee that the segments between ai and bi are open in the i-th day. Your boss wants to minimize the total cost of the next m days, and you need to tell him the charge for each day.
(At the beginning, all the segments are closed.)
Input
Multiple test case. For each test case, begins with two integers n, m(1<=n,m<=200000), next line contains n-1 integers. The i-th integer  wi(1<=wi<=1000) indicates the charge for the segment between village i and village i+1 being open for one day. Next m lines, each line contains two integers ai,bi(1ai,bi<=n,ai!=bi).
Output
For each test case, output m lines, each line contains the charge for the i-th day.
Sample Input
4 3 1 2 3 1 3 3 4 2 4
Sample Output
3 5 5
Author
BUPT
Source
【题意】n个点将一条线段分成n-1份,点的编号从左往右1~n,每条路你只能打开一次,打开后每天都收费,当然打开后你也可以选择关上,但是关上后这条路就再也不能打开了。每条线段有一个Cost值,然后Q次询问,没次询问给出两个点U,V,表示从U,到V,你必须保证U->V上的线段都打开才能过去,然后问你在保证你能过去的情况下,每天的最小花费。
【分析】要想避免无用的花费,那么我们可以对于每条路,他第一次使用就把它打开,他最后一条使用完过后就把它关了,因为这条路再也不用了。那么我们可以先找到每条路的打开时间和关闭时间,这个类似于区间覆盖,所以我们可以用线段树来维护,复杂度NlogN。然后就是要找到当前天,有多少路是打开的,这个我们可以用一个类似于莫队的做法,假设对于前一天我们已经知道了答案,那么对于今天,我们只要知道有多少条路是今天打开的和关闭的,那么我们今天就可以根据前一天的来推,复杂度O(M);
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define met(a,b) memset(a,b,sizeof a)
#define inf 10000000
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int N = 4e5+5;
const double eps = 1e-8;
int n,sum[2*N],m;
int lazy[2*N],a[N],mi[N*2],ma[N*2];
vector<int>st[N],en[N];
struct man{
    int u,v;
}q[N];
void init(){
    for(int i=0;i<N;i++){
        st[i].clear();
        en[i].clear();
    }
}
void pushDown(int pos){
     if(mi[pos]!=inf){
        mi[pos*2]=min(mi[pos*2],mi[pos]);
        mi[pos*2+1]=min(mi[pos*2+1],mi[pos]);
    }
    if(ma[pos]!=0){
        ma[pos*2]=max(ma[pos*2],ma[pos]);
        ma[pos*2+1]=max(ma[pos*2+1],ma[pos]);
    }
    return;
}

void update(int L,int R,int val,int l,int r,int pos) {
    if(l>=L&&r<=R) {
        mi[pos]=min(mi[pos],val);
        ma[pos]=max(ma[pos],val);
        return;
    }
    int mid=(l+r)>>1;
    pushDown(pos);
    if(L<=mid) update(L,R,val,l,mid,pos<<1);
    if(mid<R)update(L,R,val,mid+1,r,pos<<1|1);
}
void  query(int l,int r,int pos) {
    if(l==r){
        if(mi[pos]!=inf)st[mi[pos]].pb(l);
        if(ma[pos]!=0)en[ma[pos]+1].pb(l);
        return;
    }
    int mid=(l+r)>>1;
    pushDown(pos);
    query(l,mid,pos<<1);
    query(mid+1,r,pos<<1|1);
    return;
}
void build(int l,int r,int pos){
    mi[pos]=inf;
    ma[pos]=0;
    if(l==r){
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,pos*2);
    build(mid+1,r,pos*2+1);
}
int main() {
    int ll,rr,cnt=0;
    while(~scanf("%d%d",&n,&m)){
        init();
        build(1,n-1,1);
        for(int i=1;i<n;i++){
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=m;i++){
            scanf("%d%d",&q[i].u,&q[i].v);
            if(q[i].u>q[i].v)swap(q[i].u,q[i].v);
            update(q[i].u,q[i].v-1,i,1,n-1,1);
        }
        query(1,n-1,1);
        int ans=0;
        for(int i=1;i<=m;i++){
            for(int x:st[i]){
                ans+=a[x];
            }
            for(int x:en[i]){
                ans-=a[x];
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jianrenfang/p/6784446.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值