对每条路求出最早与最晚使用时间 记为l与r 然后在l加上花费 在r+1减去花费
注意题目给的两个端点没有规定大小顺序!!!
#include <bits/stdc++.h>
using namespace std;
#define N 0x3f3f3f3f
struct node1
{
int l;
int r;
int minn;
int maxx;
};
struct node2
{
int cost;
int l;
int r;
};
node1 tree[800010];
node2 seg[200010];
int day[200010];
int n,num;
void build(int l,int r,int cur)
{
int m;
tree[cur].l=l;
tree[cur].r=r;
tree[cur].minn=N;
tree[cur].maxx=0;
if(l+1==r) return;
m=(l+r)/2;
build(l,m,2*cur);
build(m,r,2*cur+1);
return;
}
void pushdown(int cur)
{
if(tree[cur].maxx!=0)
{
tree[2*cur].minn=min(tree[2*cur].minn,tree[cur].minn);
tree[2*cur].maxx=max(tree[2*cur].maxx,tree[cur].maxx);
tree[2*cur+1].minn=min(tree[2*cur+1].minn,tree[cur].minn);
tree[2*cur+1].maxx=max(tree[2*cur+1].maxx,tree[cur].maxx);
}
return;
}
void update(int pl,int pr,int id,int cur)
{
if(pl<=tree[cur].l&&tree[cur].r<=pr)
{
tree[cur].minn=min(tree[cur].minn,id);
tree[cur].maxx=max(tree[cur].maxx,id);
return;
}
pushdown(cur);
if(pl<tree[2*cur].r) update(pl,pr,id,2*cur);
if(pr>tree[2*cur+1].l) update(pl,pr,id,2*cur+1);
return;
}
void query(int cur)
{
if(tree[cur].l+1==tree[cur].r)
{
num++;
seg[num].l=tree[cur].minn;
seg[num].r=tree[cur].maxx;
return;
}
pushdown(cur);
query(2*cur);
query(2*cur+1);
return;
}
int main()
{
int q,i,l,r;
while(scanf("%d%d",&n,&q)!=EOF)
{
for(i=1;i<=n-1;i++)
{
scanf("%d",&seg[i].cost);
}
build(1,n,1);
for(i=1;i<=q;i++)
{
scanf("%d%d",&l,&r);
if(l>r) swap(l,r);
update(l,r,i,1);
}
num=0;
query(1);
memset(day,0,sizeof(day));
for(i=1;i<=n-1;i++)
{
if(seg[i].l!=N&&seg[i].r!=0)
{
day[seg[i].l]+=seg[i].cost;
day[seg[i].r+1]-=seg[i].cost;
}
}
for(i=1;i<=q;i++)
{
day[i]=day[i-1]+day[i];
printf("%d\n",day[i]);
}
}
return 0;
}