HDU 5861 Road(线段树 区间修改 单点查询)
2017-04-29 08:56
501 查看
Road
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1132 Accepted Submission(s): 309
[align=left]Problem Description[/align]
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.)
[align=left]Input[/align]
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(1≤ai,bi<=n,ai!=bi).
[align=left]Output[/align]
For each test case, output m lines, each line contains the charge for the i-th day.
[align=left]Sample Input[/align]
4 3
1 2 3
1 3
3 4
2 4
[align=left]Sample Output[/align]
3
5
5
[align=left]Author[/align]
BUPT
[align=left]Source[/align]
2016 Multi-University Training Contest 10
【题意】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 ,mi[N*2],ma[N*2]; vector<int>st ,en ; struct man{ int u,v; }q ; 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; }
相关文章推荐
- HDU 5861 Road 线段树区间更新单点查询
- hdu 1754 线段树单点修改+区间查询
- (HDU 1754)I Hate It 线段树区间查询入门,单点修改
- 树套树:二维线段树初步:hdu1823——Luck and Love(单点修改,区间查询)
- HDU 1166 敌兵布阵 <线段树 单点修改 区间查询>
- hdu 5381 The sum of gcd(线段树等差数列区间修改+单点查询)
- hdu 5861 (线段树,区间更新,单点查询)
- hdu 4819 二维线段树,单点修改区间查询
- HDU 4819:单点更新,区间查询的二维线段树
- 线段树 HDU 4217 Data Structure? 单点更新 区间查询
- hdu 1166 线段树 单点修改 + 询问区间求和 (线段树模板)
- 线段树 单点更新查询 区间最大值 hdu 2795 Billboard
- UVa 12299 RMQ with shifts(线段树单点修改 区间查询)
- hdu 4046 Panda 线段树 单点修改 求区间010个数
- HDU1754线段树单点更新区间查询(数组版)
- HDU 1540——Tunnel Warfare(线段树,区间合并+单点更新+单点查询)
- HDU 4027 Can you answer these queries? 线段树 区间修改 区间查询
- 【线段树II:区间修改+点查询】hdu 1556 Color the ball
- UVA - 12299 RMQ with Shifts (线段树:单点修改,区间查询)
- Wikilo 1191线段树区间修改单点查询