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(1≤ai,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+;
const double eps = 1e-;
int n,sum[*N],m;
int lazy[*N],a[N],mi[N*],ma[N*];
vector<int>st[N],en[N];
struct man{
int u,v;
}q[N];
void init(){
for(int i=;i<N;i++){
st[i].clear();
en[i].clear();
}
}
void pushDown(int pos){
if(mi[pos]!=inf){
mi[pos*]=min(mi[pos*],mi[pos]);
mi[pos*+]=min(mi[pos*+],mi[pos]);
}
if(ma[pos]!=){
ma[pos*]=max(ma[pos*],ma[pos]);
ma[pos*+]=max(ma[pos*+],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)>>;
pushDown(pos);
if(L<=mid) update(L,R,val,l,mid,pos<<);
if(mid<R)update(L,R,val,mid+,r,pos<<|);
}
void query(int l,int r,int pos) {
if(l==r){
if(mi[pos]!=inf)st[mi[pos]].pb(l);
if(ma[pos]!=)en[ma[pos]+].pb(l);
return;
}
int mid=(l+r)>>;
pushDown(pos);
query(l,mid,pos<<);
query(mid+,r,pos<<|);
return;
}
void build(int l,int r,int pos){
mi[pos]=inf;
ma[pos]=;
if(l==r){
return;
}
int mid=(l+r)/;
build(l,mid,pos*);
build(mid+,r,pos*+);
}
int main() {
int ll,rr,cnt=;
while(~scanf("%d%d",&n,&m)){
init();
build(,n-,);
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=;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-,i,,n-,);
}
query(,n-,);
int ans=;
for(int i=;i<=m;i++){
for(int x:st[i]){
ans+=a[x];
}
for(int x:en[i]){
ans-=a[x];
}
printf("%d\n",ans);
}
}
return ;
}

最新文章

  1. 使用用户自定义类型 CLR UDT
  2. imx6 Image Vector Table (IVT)
  3. Dynamic CRM 2013学习笔记(十五)报表设计:报表入门、开发工具及注意事项
  4. git常见操作--忽略文件以及常用命令【转】
  5. MSSQL 2005数据库可疑状态
  6. casperjs 抓取爱奇艺高清视频
  7. [LeetCode] Interleaving String 解题思路
  8. css属性之vertical-align详解
  9. LINQ的用法
  10. ASP.NET 5:初始化数据库
  11. 1297: [SCOI2009]迷路
  12. OpenWRT (RT5350) 使能两个串口
  13. ubuntu导入公钥的方法
  14. LINUX监控-spotlight
  15. (74)Wangdao.com第十三天_Object 对象_属性描述对象
  16. Linux学习笔记:安装python
  17. jenkins 邮箱设置
  18. react 使用hooks
  19. spket插件安装并设置JQuery自动提示(转)
  20. codeforces round#509

热门文章

  1. webservice 针对WebService服务,客户端调用时报序列化的最大项数maxItemsInObjectGraph超过65536问题
  2. [uva11991]map和vector的入门
  3. bzoj 4773: 负环——倍增
  4. jquery的基础语法、选取元素、操作元素、加事件、挂事件及移除事件
  5. python初步学习-python运算符
  6. ie6下双边距的问题
  7. Linux 入门记录:三、Linux 文件基本操作管理
  8. 【设计模式】原型模式(Prototype)
  9. python基础===时间处理模块
  10. 微信小程序宽高适配