Problem Link : BZOJ 3747
解题的大致思路是,当区间的右端点向右移动一格时,只有两个区间的左端点对应的答案发生了变化。
从 f[i] + 1 到 i 的区间中的答案增加了 W[A[i]], 从 f[f[i]] + 1 到 f[i] 的区间的答案减少了 W[A[i]] ,其余区间的答案没有发生变化。
那么就是线段树的区间修改和区间最值查询。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MaxN = + ;
int n, m;
int A[MaxN], W[MaxN], Last[MaxN], F[MaxN];
typedef long long LL;
LL Ans;
LL T[MaxN * ], D[MaxN * ];
inline LL gmax(LL a, LL b) {
return a > b ? a : b;
}
inline void Update(int x) {
T[x] = gmax(T[x << ], T[x << | ]);
}
inline void Read(int &num) {
char c; c = getchar();
while (c < '' || c > '') c = getchar();
num = c - ''; c = getchar();
while (c >= '' && c <= '') {
num = num * + c - '';
c = getchar();
}
}
inline void Paint(int x, LL num) {
D[x] += num;
T[x] += num;
}
inline void PushDown(int x) {
if (D[x] == ) return;
Paint(x << , D[x]);
Paint(x << | , D[x]);
D[x] = ;
}
LL Add(int x, int s, int t, int l, int r, int num) {
if (l <= s && r >= t) {
Paint(x, (LL)num);
return T[x];
}
PushDown(x);
int m = (s + t) >> ;
LL ret = ;
if (l <= m) ret = gmax(ret, Add(x << , s, m, l, r, num));
if (r >= m + ) ret = gmax(ret, Add(x << | , m + , t, l, r, num));
Update(x);
return ret;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) {
Read(A[i]);
F[i] = Last[A[i]];
Last[A[i]] = i;
}
for (int i = ; i <= m; i++) Read(W[i]);
Ans = ;
for (int i = ; i <= n; i++) {
Ans = gmax(Ans, Add(, , n, F[i] + , i, W[A[i]]));
if (F[i] != ) Ans = gmax(Ans, Add(, , n, F[F[i]] + , F[i], -W[A[i]]));
}
printf("%lld\n", Ans);
return ;
}