Language: C++

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<bitset>
#include<set>
#define N (200010)
#define P ()
#define M ()
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand())<<(15))^(rand()))
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
namespace fastIO1{
	inline char read(){
		static const int IN_LEN=1000000;
		static char buf[IN_LEN],*s,*t;
		return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++);
	}
	template<class T>
	inline void read(T &x){
		static bool iosig;
		static char c;
		for(iosig=false,c=read();!isdigit(c);c=read()){
			if(c=='-')iosig=true;
			if(c==-1)return;
		}
		for(x=0;isdigit(c);c=read())x=((x+(x<<2))<<1)+(c^'0');
		if(iosig)x=-x;
	}
	inline char readc(char &c){
		for(c=read();!isalpha(c)&&!isdigit(c);c=read())
		if(c==-1)return 0;
	}
	const int OUT_LEN = 10000000;
	char obuf[OUT_LEN],*ooh=obuf;
	inline void print(char c) {
		if(ooh==obuf+OUT_LEN)fwrite(obuf,1,OUT_LEN,stdout),ooh=obuf;
		*ooh++=c;
	}
	template<class T>
	inline void print(T x){
		static int buf[30],cnt;
		if(x==0)print('0');
		else{
			if(x<0)print('-'),x=-x;
			for(cnt=0;x;x/=10)buf[++cnt]=x%10+48;
			while(cnt)print((char)buf[cnt--]);
		}
	}
	inline void flush(){fwrite(obuf,1,ooh-obuf,stdout);}
}
namespace fastIO2{
	template<class T>
	inline void read(T &x){
		static bool iosig;
		static char c;
		for(iosig=false,c=getchar();!isdigit(c);c=getchar()){
			if(c=='-')iosig=true;
			if(c==-1)return;
		}
		for(x=0;isdigit(c);c=getchar())x=((x+(x<<2))<<1)+(c^'0');
		if(iosig)x=-x;
	}
}
using namespace fastIO1;
#define int long long
#define fi first
#define se second
#define mkp make_pair
int n,m,now,a[N]; pair<int,int> pos[N];
LL ans; vector<pair<pair<int,int>,pair<int,int> > >t,tmp;
vector<pair<int,int> > wk,pp;
//当前位置,当前 
int calc(vector<pair<int,int> > &a){
	LL sum=0,ans=0;
	for(int i=0,j=m-1;j<a.size();i++,j++)
	sum+=a[i].fi,ans+=sum*a[j].se;
	return ans;
}
int main(){
	read(n),read(m),ans=n;
	for(int i=1;i<=n;i++)read(a[i]),pos[i]=mkp(a[i],i);
	sort(pos+1,pos+n+1);
	for(int p=1,val=0;p<=n||!t.empty();){
//		cout<<p<<endl;
		if(t.empty())val=pos[p].fi;else val++;
		for(;pos[p].fi==val;p++)t.emplace_back(mkp(pos[p].se,pos[p].se),mkp(1,1));
		tmp.clear(),sort(t.begin(),t.end());
//		puts("faq");
		for(int i=0,j=0;i<t.size();i=j+1){
			for(j=i;j+1<t.size()&&t[j+1].fi.fi==t[j].fi.se+1;j++);
//			cout<<t.size()<<" "<<i<<" "<<j<<endl;
			int len=j-i+1,cnt=len/m;
			if(cnt){
//				puts("faq2");
				wk.clear();
				for(int k=i;k<=j;k++)wk.push_back(t[k].se);
				ans+=calc(wk),wk.clear(),wk.resize(cnt),pp.clear();
				for(int k=1;k<=cnt;k++)
				pp.emplace_back(t[i].fi.fi+k-1,(k==cnt)?t[j].fi.se:t[i].fi.fi+k-1);
				for(int k=i;k<=j;k++){
					int l=k-i+1,r=j-k+1;
					if(l>=m)wk[l/m-1].se+=t[k].se.se;
					if(r>=m)wk[cnt-r/m].fi+=t[k].se.fi;
				}
//				puts("faq3");
//				cout<<cnt<<" faq "<<wk.size()<<" "<<val<<endl;
				ans-=calc(wk);
				for(int k=0;k<pp.size();k++)tmp.emplace_back(pp[k],wk[k]);
//				cout<<tmp[k].fi.fi<<" "<<tmp[k].fi.se<<" "<<tmp[k].se.fi<<" "<<tmp[k].se.se<<endl;
			}
		}
		t=tmp;
//		cout<<t.size()<<" faq4\n";
	}
	cout<<ans;
}

Compiler output:
cc1plus: error: '::main' must return 'int'