洛谷 P3582 [POI2015]KIN
作者:互联网
线段树维护区间最大字段和;
#include <bits/stdc++.h>
using namespace std;
#define ls rt<<1
#define rs rt<<1|1
#define LL long long
#define ll long long
#define PI acos(-1.0)
#define eps 1e-8
#define Pair pair<double,double>
// notice
#define mod 998244353
#define MAXN 1e18
#define MS 1000009
LL n,m;
LL a[MS];
LL w[MS];
LL pre[MS];
LL v[MS];
struct node{
LL lmax,rmax;
LL maxn;
LL sum;
}p[MS<<2];
void push_up(int rt){
p[rt].sum = p[ls].sum + p[rs].sum;
p[rt].lmax = max(p[ls].lmax ,p[ls].sum + p[rs].lmax);
p[rt].rmax = max(p[rs].rmax ,p[rs].sum + p[ls].rmax);
p[rt].maxn = max(p[rt].lmax ,p[rt].rmax);
p[rt].maxn = max(p[rt].maxn ,p[ls].rmax + p[rs].lmax);
p[rt].maxn = max(p[rt].maxn ,max(p[ls].maxn ,p[rs].maxn));
}
void update(int pos,int l,int r,int rt,LL val){
if(l == r){
p[rt] = {val,val,val,val};
return;
}
int m = l+r>>1;
if(m >= pos) update(pos,l,m,ls,val);
else update(pos,m+1,r,rs,val);
push_up(rt);
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i=1;i<=n;i++){
cin >> a[i];
pre[i] = v[a[i]];
v[a[i]] = i;
}
pre[0] = -1;
for(int i=1;i<=m;i++){
cin >> w[i];
}
LL ans = 0;
for(int i=1;i<=n;i++){
update(i,1,n,1,w[a[i]]);
if(pre[i] != 0){
update(pre[i],1,n,1,-w[a[i]]);
}
if(pre[pre[i]] != 0 && pre[pre[i]] != -1){
update(pre[pre[i]],1,n,1,0);
}
ans = max(ans,p[1].maxn);
}
cout << ans << "\n";
return 0;
}
标签:pre,洛谷,int,LL,pos,P3582,KIN,MS,define 来源: https://www.cnblogs.com/Tecode/p/14829189.html