Codeforces Round #426 (Div. 2), problem: (D) The Bakery Solution In C/C++

#include <bits/stdc++.h>
#define For(i,a,b) for(long long i=a;i<=b;i++)
#define Ford(i,a,b) for(int i=a;i>=b;i–)
#define dream_hyojooslh

using namespace std;
const int N=200000+1067,T=67;
int f[N][T],it[T][N],lz[T][N],trai[N],phai[N],a[N];

void build(int x,int l,int r) {
trai[x]=l;
phai[x]=r;
if (l==r) return;
int mid =(l+r)/2;
build(x*2,l,mid);
build(x*2+1,mid+1,r);
}

void down(int t,int x) {
lz[t][x*2]+=lz[t][x];
it[t][x*2]+=lz[t][x];
lz[t][x*2+1]+=lz[t][x];
it[t][x*2+1]+=lz[t][x];
lz[t][x]=0;
}

void update(int t,int x,int l,int r,int c) {
if (l>phai[x]||r<trai[x]) return;
if (trai[x]>=l&&phai[x]<=r) {
it[t][x]+=c;
lz[t][x]+=c;
return;
}
down(t,x);
update(t,x*2,l,r,c);
update(t,x*2+1,l,r,c);
it[t][x]=max(it[t][x*2],it[t][x*2+1]);
}
int last[N];
map <long long,long long> mp;
int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);
//freopen(“dream_hyojooslh.inp”,”r”,stdin);
int n,k;
cin>>n>>k;;
For(i,1,n) cin>>a[i];
build(1,0,n);
For(i,1,n) {
For(j,0,k-1) update(j,1,last[a[i]],i-1,1);
last[a[i]]=i;
For(j,1,k) {
f[i][j]=it[j-1][1];
update(j,1,i,i,f[i][j]);
}
}
cout<<f[n][k]<<endl;
return 0;
}

Why Pet Travel Gear...

📢 As an Amazon Associate, I earn from qualifying...

Mall Together-TENOKE

Simulation gaming enthusiasts have a fresh operational challenge to...

7 Reasons This Coffee...

📢 As an Amazon Associate, I earn from qualifying...

Why Kitchen Organization Tools...

📢 As an Amazon Associate, I earn from qualifying...

Is Dorm Room Hacks...

📢 As an Amazon Associate, I earn from qualifying...

Stop Buying Cheap Alternatives...

📢 As an Amazon Associate, I earn from qualifying...

Why Pet Travel Gear Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Pet Travel Gear through a proper 30-day real-world test, I can tell...

Mall Together-TENOKE

Simulation gaming enthusiasts have a fresh operational challenge to tackle with the arrival of Mall Together-TENOKE. This brand-new release delivers an immersive shopping mall...

7 Reasons This Coffee Bar Accessories Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Why Kitchen Organization Tools Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Is Dorm Room Hacks Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Stop Buying Cheap Alternatives This Camping Essentials Is the Real Deal

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Everything You Need to Know About Outdoor Patio Tech Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.I'll be honest — I was skeptical at first. My daily routine had a gap...

Why Bathroom Organization Hacks Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.I'll be honest — I was skeptical at first. My daily routine had a gap...

Why Electric Cleaning Tools Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Electric Cleaning Tools?The Electric Cleaning Tools...