Language: C++

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,l,a[200010],p[200020];
long long ans;
struct data
{
	int l,r,wl,wr;
};
long long calc(vector<pair<int,int> >&w)
{
//	for(int i=0;i<w.size();++i)printf("%d %d\n",w[i].first,w[i].second);
	int sum=0;
	long long res=0;
	for(int i=0;i<w.size();++i)
	{
		if(i>=l-1)sum+=w[i-l+1].first;
		res+=sum*w[i].second;
	}
//	printf("%lld\n",res);
	return res;
}
int main()
{
	scanf("%d %d",&n,&l);ans=n;
	for(int i=1;i<=n;++i)scanf("%d",&a[i]),p[i]=i;
	sort(p+1,p+1+n,[](int &x,int &y){return a[x]<a[y];});
	int last=1,val=0;
	vector<data>A,B;
	while(last<=n||A.size())
	{
		if(A.empty())val=a[p[last]];
		else ++val;
		while(a[p[last]]==val)A.push_back(data{p[last],p[last],1,1}),++last;
		B.clear();
		sort(A.begin(),A.end(),[](data &x,data &y){return x.l<y.l;});
		for(int i=0;i<A.size();++i)
		{
			vector<pair<int,int> >cur,nxt;
			int r=i;cur.push_back(make_pair(A[i].wl,A[i].wr));
			while(r+1<A.size()&&A[r+1].l==A[r].r+1)++r,cur.push_back(make_pair(A[r].wl,A[r].wr));
			ans+=calc(cur);
			int len=r-i+1,cnt=(r-i+1)/l;
			nxt.resize(cnt);
			for(int j=0;j<cnt;++j)nxt[j].first=nxt[j].second=0;
			for(int j=0;j<len;++j)
			{
				if(j+1>=l)nxt[(j+1)/l-1].second+=A[i+j].wr;
				if(len-j>=l)nxt[cnt-(len-j)/l].first+=A[i+j].wl;
			}
			ans-=calc(nxt);
			for(int j=0;j<cnt;++j)B.push_back(data{A[i+j*l].l,A[j==cnt-1?r:i+j*l+l-1].r,nxt[j].first,nxt[j].second});
			i=r;
		}
		A=B;
	//	for(int i=0;i<A.size();++i)printf("%d %d %d %d\n",A[i].l,A[i].r,A[i].wl,A[i].wr);
	//	printf("\n");
	}
	printf("%lld",ans);
	return 0;
}


Compiler output:
a.cc:11:16: error: 'vector' was not declared in this scope
   11 | long long calc(vector<pair<int,int> >&w)
      |                ^~~~~~
a.cc:4:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
    3 | #include<algorithm>
  +++ |+#include <vector>
    4 | using namespace std;
a.cc:11:37: error: expected primary-expression before '>' token
   11 | long long calc(vector<pair<int,int> >&w)
      |                                     ^
a.cc:11:39: error: 'w' was not declared in this scope
   11 | long long calc(vector<pair<int,int> >&w)
      |                                       ^
a.cc: In function 'int main()':
a.cc:30:9: error: 'vector' was not declared in this scope
   30 |         vector<data>A,B;
      |         ^~~~~~
a.cc:30:9: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
a.cc:30:20: error: expected primary-expression before '>' token
   30 |         vector<data>A,B;
      |                    ^
a.cc:30:21: error: 'A' was not declared in this scope
   30 |         vector<data>A,B;
      |                     ^
a.cc:30:23: error: 'B' was not declared in this scope
   30 |         vector<data>A,B;
      |                       ^
a.cc:40:46: error: expected primary-expression before '>' token
   40 |                         vector<pair<int,int> >cur,nxt;
      |                                              ^
a.cc:40:47: error: 'cur' was not declared in this scope
   40 |                         vector<pair<int,int> >cur,nxt;
      |                                               ^~~
a.cc:40:51: error: 'nxt' was not declared in this scope
   40 |                         vector<pair<int,int> >cur,nxt;
      |                                                   ^~~
a.cc:43:34: error: 'calc' cannot be used as a function
   43 |                         ans+=calc(cur);
      |                              ~~~~^~~~~
a.cc:52:34: error: 'calc' cannot be used as a function
   52 |                         ans-=calc(nxt);
      |                              ~~~~^~~~~