AT_abc318_e [ABC318E] Sandwiches 题解

没啥困难的。

枚举 ii,然后找出所有在 ii 后面可以出现的 kk。对于某个 kk,设 (i,k)(i,k) 中有 xxaj=aia_j = a_i,那么 kk 的贡献是 k1ixk-1-i-x

发现后面 xx 是一个等差数列,前面可以把 ii 提出来,k1k-1 可以动态维护和,就做完了。

#include <bits/stdc++.h>
using namespace std;
//#define int long long

const int N = 3e5 + 5, MOD = 1e9 + 7; // Remember to change

int n, m, q, t, a[N];
set<int> st[N];
long long nowsum[N];

namespace FastIo
{
	#define QUICKCIN ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
	int read()
	{
		char ch = getchar();
		int x = 0, f = 1;
		while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
		while (ch == '-')
		{
			f = -f;
			ch = getchar();
		}
		while (ch >= '0' && ch <= '9')
		{
			x = (x << 1) + (x << 3) + (ch ^ 48);
			ch = getchar();
		}
		return x * f;
	}
	template<class T>
	void write(T x)
	{
		if (x < 0)
		{
			putchar('-');
			x = -x;
		}
		if (x > 9) write(x / 10);
		putchar(x % 10 + '0');
	}
	template<class T>
	void writeln(T x)
	{
		write(x);
		putchar('\n');
	}
}

int main()
{
	ios::sync_with_stdio(0), cin.tie(nullptr), cout.tie(nullptr);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i],st[a[i]].insert(i),nowsum[a[i]]+=(i-1);
	long long ans=0LL;
	for(int i=1;i<=n;i++)
	{
		nowsum[a[i]]-=(i-1);
		st[a[i]].erase(st[a[i]].begin());
		long long l=1,r=st[a[i]].size()-1;
		long long ss=(l>r?0LL:(l+r)*(r-l+1)/2);
		ans+=nowsum[a[i]]-(long long)st[a[i]].size()*(i)-ss;
	}
	cout<<ans<<"\n";
	return 0;
}
posted @   HappyBobb  阅读(5)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示