题目链接:传送门
好像之前在洛谷上做过一个叫KIN的题
一个电影看多次就不会记贡献
那么这个电影产生贡献的区间就是(这一次看)到(上一次看的后一天)
在这一块内才会记录它的贡献
再往前要在(上一次看的上一次的后一天)到(上一次看的后一天)这段区间减去贡献
因为对于那个时间段这个电影已经产生不了贡献了
这题做法很多
看了一些做法感觉单点修改的最好理解
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define
using namespace std;
typedef long long ll;
struct node {
int l, r; ll w, f;
}tree[A];
void down(int k) {
tree[k << 1].f += tree[k].f;
tree[k << 1 | 1].f += tree[k].f;
tree[k << 1].w += tree[k].f;
tree[k << 1 | 1].w += tree[k].f;
tree[k].f = 0;
}
void build(int k, int l, int r) {
tree[k].l = l; tree[k].r = r;
if (l == r) {
tree[k].w = tree[k].f = 0;
return;
}
int m = (l + r) >> 1;
build(k << 1, l, m);
build(k << 1 | 1, m + 1, r);
}
void change(int k, int l, int r, ll val) {
if (tree[k].l >= l and tree[k].r <= r) {
tree[k].w += val; tree[k].f += val;
return;
}
if (tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1;
if (l <= m) change(k << 1, l, r, val);
if (r > m) change(k << 1 | 1, l, r, val);
tree[k].w = max(tree[k << 1].w, tree[k << 1 | 1].w);
}
ll ask(int k, int l, int r) {
if (tree[k].l >= l and tree[k].r <= r) return tree[k].w;
int m = (tree[k].l + tree[k].r) >> 1; ll ans = 0;
if (tree[k].f) down(k);
if (l <= m) ans = max(ans, ask(k << 1, l, r));
if (r > m) ans = max(ans, ask(k << 1 | 1, l, r));
return ans;
}
int n, m, pos[A], lst[A], pre[A]; ll ans, w[A];
int main(int argc, char const *argv[]) {
scanf("%d%d", &n, &m); build(1, 1, n);
for (int i = 1; i <= n; i++) scanf("%d", &pos[i]);
for (int i = 1; i <= m; i++) scanf("%lld", &w[i]);
for (int i = 1; i <= n; i++) pre[i] = lst[pos[i]], lst[pos[i]] = i;
for (int i = 1; i <= n; i++) {
ans = max(ans, tree[1].w);
change(1, pre[i] + 1, i, w[pos[i]]);
if (pre[i]) change(1, pre[pre[i]] + 1, pre[i], -w[pos[i]]);
ans = max(ans, ask(1, 1, i));
}
printf("%lld\n", ans);
return 0;
}