这是一道不错的题,不过我并没有思考充分就实在忍不住看了题解,有点遗憾
记pre[i]表示原序列i位置的数上一次出现的位置
每加入一个数,我们发现pre[i+1]~i作为左端点的答案就增加了w,1~pre[i]减少了w
我们可以枚举右端点,每加入一个数进行区间加/减操作,不过我们发现,虽然从理论上来讲的确是1~pre[i]减少了w,不过如果每次都区间减,那会减出问题的。所以实际上,每次只需要对pre[pre[i]]+1~pre[i]减就行了。
至于答案,每加入新端点就查询1~n的最大值即可
区间修改+最大=线段树
#include<bits/stdc++.h>
#define ll long long
const int N=1000005;
using namespace std;
template<class T>
inline void read(T &x)
{
x=0; int f=1;
static char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
x*=f;
}
int n,m,pre[N],f[N],base[N];
ll w[N];
struct Tree
{
int l,r;
ll maxv,f;
}tree[4*N];
inline void build(int now,int l,int r)
{
tree[now].l=l; tree[now].r=r;
if(l==r)