#include using namespace std; typedef signed long long ll; #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) template bool chmax(T &a, const T &b) { if(a bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;} //------------------------------------------------------- int N; int A[202020]; set S[202020]; class AreaRect { map M; public: ll sum; AreaRect() { M[0]=1LL<<60; M[1LL<<60]=0; sum=0; } void add(ll x,ll y) { auto k=M.lower_bound(x); if(k->second>=y) return; while(prev(M.lower_bound(x))->second<=y) { auto p=*prev(M.lower_bound(x)); M.erase(p.first); sum-=(p.first-prev(M.lower_bound(p.first))->first)*(p.second-M.lower_bound(p.first)->second); } sum += (x-prev(M.lower_bound(x))->first)*(y-M.lower_bound(x)->second); M[x]=y; } }; AreaRect ar; void solve() { int i,j,k,l,r,x,y; string s; cin>>N; FOR(i,N) { cin>>A[i]; S[A[i]].insert(i+1); S[i+1].insert(N+1); } for(i=1;i<=N;i++) { x=*S[i].begin(); ar.add(N+1-i,x); } ll ret=1LL*N*(N+1)/2; FOR(i,N) { ret+=1LL*N*(N+1)-ar.sum; S[A[i]].erase(S[A[i]].begin()); x=*S[A[i]].begin(); ar.add(N+1-A[i],x); } cout<