#include<cstdio>
#include<algorithm>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MX = 35000 + 5;
int n,m,pre[MX],now[MX];
int a[MX],f[MX],maxv[MX<<2],add[MX<<2];
void build(int l,int r,int rt)
{
add[rt]=0;
if(l==r){
maxv[rt]=f[l];
return;
}
int m=(l+r)>>1;
build(lson);//左孩子 l,m,rt<<1
build(rson);// 右孩子 rt<<1|1
}
void PushDown(int rt){
if(add[rt]){
maxv[rt<<1]+=add[rt];
maxv[rt<<1|1]+=add[rt];
add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
add[rt]=0;
}
}
void PushUP(int rt){
maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
}
//update(pre[j],j-1,1,0,n,1);
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&R>=r){
maxv[rt]+=c;
add[rt]+=c;
return;
}
PushDown(rt);
int m=(l+r)>>1;
if(L<=m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
PushUP(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r) return maxv[rt];
PushDown(rt);
int m=(l+r)>>1;
int ret=0;
if(L<=m) ret=max(ret,query(L,R,lson));
if(R>m) ret=max(ret,query(L,R,rson));
PushUP(rt);
return ret;
}
int main(){
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) scanf("%d",&a[i]);
for(int i=1;i<=n;i++){
pre[i]=now[a[i]];
now[a[i]]=i;
}
for(int i=1;i<=m;i++)
{
build(0,n,1);
for(int j=1;j<=n;j++){
update(pre[j],j-1,1,0,n,1);
f[j]=query(0,j-1,0,n,1);
}
}
printf("%d\n",f[n]);
return 0;
}
線段樹 分成幾 段和最大
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 線段樹(又稱區(qū)間樹), 是一種高級數(shù)據(jù)結(jié)構(gòu),他可以支持這樣的一些操作: 查找給定的點(diǎn)包含在了哪些區(qū)間內(nèi) 查找給定的...
- 01 懷念小時(shí)候,不管那段童年的回憶是好是壞,是苦是甜,但都過去了,都說時(shí)間可以帶走疼痛,留下回憶。但我覺得時(shí)間帶...