#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll,ll>
#ifndef DEBUG
#define cerr if (0) cerr
#define endl '\n'
#endif
const ll maxn=2e5+5;
const ll inf=1e9;
ll n;
vector<int> adj[maxn];
ll fen[maxn];
vector<int> vis[2*maxn];
vector<int> can[maxn];
vector<pair<int,int>> rngs[maxn];
void precomp(ll x){
adj[x].emplace_back(n+1);
rngs[x].emplace_back(0,-adj[x][0]+1);
ll sz=adj[x].size();
for (int i=1;i<sz;i++){
ll prv=rngs[x].back().second;
rngs[x].emplace_back(prv+1,prv+2-(adj[x][i]-adj[x][i-1]));
}
//rngs of negative values
can[x].emplace_back(-inf);
for (auto [l,r]:rngs[x]){
if (vis[l+200002].empty() || vis[l+200002].back()!=x)
vis[l+200002].emplace_back(x);
if (vis[r+200002].empty() || vis[r+200002].back()!=x)
vis[r+200002].emplace_back(x);
}
}
ll solve(ll x){
ll ans=0;
for (int i=1;i<=can[x].size();i++) fen[i]=0;
ll pos=lower_bound(can[x].begin(),can[x].end(),0)-can[x].begin();
ll tot=0;
for (auto [l,r]:rngs[x]){
while (can[x][pos]>=r){
tot-=fen[pos];
ans+=tot;
fen[pos]++;
pos--;
}
tot+=fen[pos+1]+fen[pos+2];
pos+=2;
}
return ans;
}
map<ll,ll> m;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll ele,cnt=1;
cin>>n;
for (int i=1;i<=n;i++){
cin>>ele;
if (m.count(ele)) ele=m[ele];
else{
m[ele]=cnt;
ele=cnt;
cnt++;
}
adj[ele].emplace_back(i);
}
ll ans=0;
for (int i=1;i<=n;i++){
if (adj[i].size()>1) precomp(i);
else ans+=adj[i].size();
}
for (int i=0;i<400005;i++){
for (auto u:vis[i]){
can[u].emplace_back(i-200002);
}
}
for (int i=1;i<=n;i++){
if (adj[i].size()>1) ans+=solve(i);
}
cout<<ans;
return 0;
}
Compilation message
Main.cpp: In function 'long long int solve(long long int)':
Main.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i=1;i<=can[x].size();i++) fen[i]=0;
| ~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
25436 KB |
Output is correct |
2 |
Correct |
8 ms |
25472 KB |
Output is correct |
3 |
Correct |
7 ms |
25436 KB |
Output is correct |
4 |
Correct |
6 ms |
25432 KB |
Output is correct |
5 |
Correct |
9 ms |
25436 KB |
Output is correct |
6 |
Correct |
7 ms |
25524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
25436 KB |
Output is correct |
2 |
Correct |
8 ms |
25472 KB |
Output is correct |
3 |
Correct |
7 ms |
25436 KB |
Output is correct |
4 |
Correct |
6 ms |
25432 KB |
Output is correct |
5 |
Correct |
9 ms |
25436 KB |
Output is correct |
6 |
Correct |
7 ms |
25524 KB |
Output is correct |
7 |
Correct |
7 ms |
25436 KB |
Output is correct |
8 |
Correct |
7 ms |
25436 KB |
Output is correct |
9 |
Correct |
8 ms |
25436 KB |
Output is correct |
10 |
Correct |
7 ms |
25568 KB |
Output is correct |
11 |
Correct |
7 ms |
25436 KB |
Output is correct |
12 |
Correct |
7 ms |
25436 KB |
Output is correct |
13 |
Correct |
8 ms |
25552 KB |
Output is correct |
14 |
Correct |
8 ms |
25436 KB |
Output is correct |
15 |
Correct |
8 ms |
25552 KB |
Output is correct |
16 |
Correct |
7 ms |
25436 KB |
Output is correct |
17 |
Correct |
7 ms |
25532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
27980 KB |
Output is correct |
2 |
Correct |
21 ms |
28428 KB |
Output is correct |
3 |
Correct |
16 ms |
26936 KB |
Output is correct |
4 |
Correct |
24 ms |
28488 KB |
Output is correct |
5 |
Correct |
26 ms |
28492 KB |
Output is correct |
6 |
Correct |
22 ms |
28492 KB |
Output is correct |
7 |
Correct |
21 ms |
28404 KB |
Output is correct |
8 |
Correct |
22 ms |
28496 KB |
Output is correct |
9 |
Correct |
21 ms |
28492 KB |
Output is correct |
10 |
Correct |
21 ms |
28492 KB |
Output is correct |
11 |
Correct |
28 ms |
35756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
25436 KB |
Output is correct |
2 |
Correct |
8 ms |
25472 KB |
Output is correct |
3 |
Correct |
7 ms |
25436 KB |
Output is correct |
4 |
Correct |
6 ms |
25432 KB |
Output is correct |
5 |
Correct |
9 ms |
25436 KB |
Output is correct |
6 |
Correct |
7 ms |
25524 KB |
Output is correct |
7 |
Correct |
7 ms |
25436 KB |
Output is correct |
8 |
Correct |
7 ms |
25436 KB |
Output is correct |
9 |
Correct |
8 ms |
25436 KB |
Output is correct |
10 |
Correct |
7 ms |
25568 KB |
Output is correct |
11 |
Correct |
7 ms |
25436 KB |
Output is correct |
12 |
Correct |
7 ms |
25436 KB |
Output is correct |
13 |
Correct |
8 ms |
25552 KB |
Output is correct |
14 |
Correct |
8 ms |
25436 KB |
Output is correct |
15 |
Correct |
8 ms |
25552 KB |
Output is correct |
16 |
Correct |
7 ms |
25436 KB |
Output is correct |
17 |
Correct |
7 ms |
25532 KB |
Output is correct |
18 |
Correct |
18 ms |
27980 KB |
Output is correct |
19 |
Correct |
21 ms |
28428 KB |
Output is correct |
20 |
Correct |
16 ms |
26936 KB |
Output is correct |
21 |
Correct |
24 ms |
28488 KB |
Output is correct |
22 |
Correct |
26 ms |
28492 KB |
Output is correct |
23 |
Correct |
22 ms |
28492 KB |
Output is correct |
24 |
Correct |
21 ms |
28404 KB |
Output is correct |
25 |
Correct |
22 ms |
28496 KB |
Output is correct |
26 |
Correct |
21 ms |
28492 KB |
Output is correct |
27 |
Correct |
21 ms |
28492 KB |
Output is correct |
28 |
Correct |
28 ms |
35756 KB |
Output is correct |
29 |
Correct |
40 ms |
35764 KB |
Output is correct |
30 |
Correct |
28 ms |
29556 KB |
Output is correct |
31 |
Correct |
68 ms |
34276 KB |
Output is correct |
32 |
Correct |
196 ms |
48716 KB |
Output is correct |
33 |
Correct |
62 ms |
34824 KB |
Output is correct |
34 |
Correct |
79 ms |
35408 KB |
Output is correct |
35 |
Correct |
40 ms |
31568 KB |
Output is correct |
36 |
Correct |
24 ms |
29020 KB |
Output is correct |
37 |
Correct |
31 ms |
29712 KB |
Output is correct |
38 |
Correct |
34 ms |
32720 KB |
Output is correct |
39 |
Correct |
35 ms |
32592 KB |
Output is correct |
40 |
Correct |
36 ms |
32856 KB |
Output is correct |
41 |
Correct |
36 ms |
32716 KB |
Output is correct |
42 |
Correct |
34 ms |
32708 KB |
Output is correct |
43 |
Correct |
57 ms |
34264 KB |
Output is correct |
44 |
Correct |
53 ms |
34268 KB |
Output is correct |
45 |
Correct |
52 ms |
34172 KB |
Output is correct |
46 |
Correct |
54 ms |
34280 KB |
Output is correct |
47 |
Correct |
60 ms |
34336 KB |
Output is correct |
48 |
Correct |
67 ms |
35656 KB |
Output is correct |
49 |
Correct |
74 ms |
35628 KB |
Output is correct |
50 |
Correct |
64 ms |
36756 KB |
Output is correct |
51 |
Correct |
57 ms |
36688 KB |
Output is correct |
52 |
Correct |
61 ms |
35972 KB |
Output is correct |
53 |
Correct |
64 ms |
35668 KB |
Output is correct |
54 |
Correct |
59 ms |
36184 KB |
Output is correct |