很暴力的写法。直接怼了三颗线段树,怕出错
思路就是先区间更新出每条路最早使用时间跟最短使用时间,然后再把这个区间维护到另一颗线段树上用于求每一天的总花费,注意当这条路自始至终都没有用到的话要特判,不然会RE
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
typedef long long LL;
const int maxn = 200000 + 7;
int lazy1[maxn * 6], lazy2[maxn * 6];
int maxx[maxn], minn[maxn];
void PushDown1(int rt) {
if (lazy1[rt]) {
if (lazy1[rt << 1 | 1] == 0) lazy1[rt << 1 | 1] = lazy1[rt];
if (lazy1[rt << 1] == 0) lazy1[rt << 1] = lazy1[rt];
lazy1[rt] = 0;
}
}
void PushDown2(int rt) {
if (lazy2[rt]) {
if (lazy2[rt << 1 | 1] == 0) lazy2[rt << 1 | 1] = lazy2[rt];
if (lazy2[rt << 1] == 0) lazy2[rt << 1] = lazy2[rt];
lazy2[rt] = 0;
}
}
void build(int l, int r, int rt) {
lazy1[rt] = lazy2[rt] = 0;
if (l == r) return ;
int m = (l + r) >> 1;
build(lson);
build(rson);
}
void update1(int L,int R,int c,int l,int r,int rt) {
if (L <= l && r <= R) {
if (lazy1[rt]) return ;
lazy1[rt] = c;
return ;
}
PushDown1(rt);
int m = (l + r) >> 1;
if (L <= m) update1(L, R, c, lson);
if (R > m) update1(L, R, c, rson);
}
void update2(int L,int R,int c,int l,int r,int rt) {
if (L <= l && r <= R) {
if (lazy2[rt]) return ;
lazy2[rt] = c;
return ;
}
PushDown2(rt);
int m = (l + r) >> 1;
if (L <= m) update2(L, R, c, lson);
if (R > m) update2(L, R, c, rson);
}
void query1(int L,int R, int l, int r, int rt) {
if (l == r) {
minn[l] = lazy1[rt];
return ;
}
PushDown1(rt);
int m = (l + r) >> 1;
if (L <= m) query1(L, R, lson);
if (m < R) query1(L, R, rson);
}
void query2(int L,int R, int l, int r, int rt) {
if (l == r) {
maxx[l] = lazy2[rt];
return ;
}
PushDown2(rt);
int m = (l + r) >> 1;
if (L <= m) query2(L, R, lson);
if (m < R) query2(L, R, rson);
}
int w[maxn];
struct node {
int l, r;
} q[maxn];
LL add[maxn<<2];
LL sum[maxn<<2];
void PushUp(int rt) {
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(int rt,int m) {
if (add[rt]) {
add[rt<<1] += add[rt];
add[rt<<1|1] += add[rt];
sum[rt<<1] += add[rt] * (m - (m >> 1));
sum[rt<<1|1] += add[rt] * (m >> 1);
add[rt] = 0;
}
}
void buildsum(int l,int r,int rt) {
add[rt] = 0;
if (l == r) {
sum[l] = 0;
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt) {
if (L <= l && r <= R) {
add[rt] += c;
sum[rt] += (LL)c * (r - l + 1);
return ;
}
PushDown(rt, r - l + 1);
int m = (l + r) >> 1;
if (L <= m) update(L, R, c, lson);
if (m < R) update(L, R, c, rson);
PushUp(rt);
}
LL query(int L,int R,int l, int r, int rt) {
if (L <= l && r <= R)
return sum[rt];
PushDown(rt, r - l + 1);
int m = (l + r) >> 1;
LL ret = 0;
if (L <= m) ret += query(L, R, lson);
if (m < R) ret += query(L, R, rson);
return ret;
}
int main() {
// freopen("input.txt", "r", stdin);
int T, n, m;
while (scanf("%d%d", &n, &m) != EOF) {
memset(add, 0, sizeof (add));
memset(sum, 0, sizeof (sum));
for (int i = 1; i < n; ++i) scanf("%d", &w[i]);
build(1, n - 1, 1);
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &q[i].l, &q[i].r);
if (q[i].l > q[i].r) swap(q[i].l, q[i].r);
update1(q[i].l, q[i].r - 1, i, 1, n - 1, 1);
}
for (int i = m; i >= 1; --i)
update2(q[i].l, q[i].r - 1, i, 1, n - 1, 1);
for (int i = 1; i < n; ++i) {
query1(i, i, 1, n - 1, 1);
query2(i, i, 1, n - 1, 1);
}
buildsum(1, m, 1);
for (int i = 1; i < n; ++i) {
if (minn[i] == 0) continue;
update(minn[i], maxx[i], w[i], 1, m, 1);
}
for (int i = 1; i <= m; ++i)
printf("%lld\n", query(i, i, 1, m, 1));
}
return 0;
}