解:直接开两个set一个从前遍历一个从后遍历,如果后面的元素在前面的元素出现过就加入后面的集合,如果后面的元素存在后面的集合中则累加个数+1,如果两个
集合相等就加上累加个数。一开始图省事直接用set是否相等来做判定条件结果T到死,因为整个集合要一一比较。。。所以用set的大小来做判定条件
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<vector> #include<map> #include <set> #include <bits/stdc++.h> using namespace std; const int N = 3e6+10; typedef long long LL; typedef pair<int,int> pi; set<int>q1,q2; int a[N]; struct FastIO { static const int S = 2*100; int wpos; char wbuf[S]; FastIO() : wpos(0) {} inline int xchar() { static char buf[S]; static int len = 0, pos = 0; if (pos==len) pos = 0, len = fread(buf, 1, S, stdin); if (pos==len) exit(0); return buf[pos ++]; } inline int xint() { int s = 1, c = xchar(), x = 0; while(c<=32) c = xchar(); if(c=='-') s = -1, c = xchar(); for(;'0'<=c && c<='9';c=xchar()) x = x*10+c-'0'; return x * s; } ~FastIO() { if(wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0; } }io; int num[N]; int main() { int n; scanf("%d", &n); for(int i=1;i<=n;i++) a[i]=io.xint(); int cnt=0, ans=0; q1.clear(),q2.clear(); int j=n; for(int i=1;i<=n;i++) { q1.insert(a[i]); while(q1.find(a[j])!=q1.end()&&j>=1) { num[j]=1;q2.insert(a[j]); j--; while(q2.find(a[j])!=q2.end()&&j>=1) { num[j]=num[j+1]+1; j--; } } if(q1.size()==q2.size()) ans+=num[j+1]; } printf("%d\n",ans); return 0; }