2019 牛客多校 第八场 B Beauty Values (贡献)
链接:https://ac.nowcoder.com/acm/contest/888/B
题意:给出一个序列,求它的全部子区间的beauty(区间的数的种类数目)。 i<1e5 ,ai<1e5
题解:考虑每一个种类对答案的贡献,即考虑每一个数对答案的贡献,看这个数在多少区间出现过。
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+5; int n, a[maxn], pre[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>n; for(int i=1; i<=n; i++) cin>>a[i]; ll ans=0; for(int i=1; i<=n; i++) { ans+=1ll*(i-pre[a[i]])*(n-i+1); pre[a[i]]=i; } cout<<ans; return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步