#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=1e15;
ll n;
vector<ll> adj[maxn];
ll fen[maxn];
void upd(ll x){
while (x<maxn){
fen[x]++;
x+=(x&-x);
}
}
ll query(ll x){
ll ans=0;
while (x){
ans+=fen[x];
x-=(x&-x);
}
return ans;
}
ll solve(ll x){
adj[x].emplace_back(n+1);
vector<ii> ranges;
ranges.emplace_back(0,-adj[x][0]+1);
for (int i=1;i<adj[x].size();i++){
ll prv=ranges.back().second;
ranges.emplace_back(prv+1,prv+2-(adj[x][i]-adj[x][i-1]));
}
//ranges of negative values
vector<ll> can;
can.emplace_back(-inf);
for (auto [l,r]:ranges){
can.emplace_back(l);
can.emplace_back(l+1);
can.emplace_back(r);
can.emplace_back(r+1);
}
ll ans=0;
sort(can.begin(),can.end());
can.erase(unique(can.begin(),can.end()),can.end());
for (int i=1;i<=can.size();i++) fen[i]=0;
for (auto [l,r]:ranges){
ll s=lower_bound(can.begin(),can.end(),r)-can.begin();
ll e=lower_bound(can.begin(),can.end(),l)-can.begin();
for (int i=e;i>=s;i--){
ans+=query(i-1);
upd(i);
}
}
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) ans+=solve(i);
else ans+=adj[i].size();
}
cout<<ans;
return 0;
}
Compilation message
Main.cpp: In function 'long long int solve(long long int)':
Main.cpp:36:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int i=1;i<adj[x].size();i++){
| ~^~~~~~~~~~~~~~
Main.cpp:52:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i=1;i<=can.size();i++) fen[i]=0;
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
3 ms |
6548 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
3 ms |
6548 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6488 KB |
Output is correct |
11 |
Correct |
3 ms |
6492 KB |
Output is correct |
12 |
Correct |
4 ms |
6560 KB |
Output is correct |
13 |
Correct |
3 ms |
6492 KB |
Output is correct |
14 |
Correct |
3 ms |
6492 KB |
Output is correct |
15 |
Correct |
3 ms |
6500 KB |
Output is correct |
16 |
Correct |
3 ms |
6560 KB |
Output is correct |
17 |
Correct |
2 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
15484 KB |
Output is correct |
2 |
Correct |
46 ms |
16876 KB |
Output is correct |
3 |
Correct |
29 ms |
12028 KB |
Output is correct |
4 |
Correct |
57 ms |
16792 KB |
Output is correct |
5 |
Correct |
51 ms |
18020 KB |
Output is correct |
6 |
Correct |
56 ms |
17908 KB |
Output is correct |
7 |
Correct |
55 ms |
17352 KB |
Output is correct |
8 |
Correct |
55 ms |
17372 KB |
Output is correct |
9 |
Correct |
57 ms |
18136 KB |
Output is correct |
10 |
Correct |
58 ms |
18180 KB |
Output is correct |
11 |
Correct |
54 ms |
20416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
3 ms |
6548 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6488 KB |
Output is correct |
11 |
Correct |
3 ms |
6492 KB |
Output is correct |
12 |
Correct |
4 ms |
6560 KB |
Output is correct |
13 |
Correct |
3 ms |
6492 KB |
Output is correct |
14 |
Correct |
3 ms |
6492 KB |
Output is correct |
15 |
Correct |
3 ms |
6500 KB |
Output is correct |
16 |
Correct |
3 ms |
6560 KB |
Output is correct |
17 |
Correct |
2 ms |
6744 KB |
Output is correct |
18 |
Correct |
39 ms |
15484 KB |
Output is correct |
19 |
Correct |
46 ms |
16876 KB |
Output is correct |
20 |
Correct |
29 ms |
12028 KB |
Output is correct |
21 |
Correct |
57 ms |
16792 KB |
Output is correct |
22 |
Correct |
51 ms |
18020 KB |
Output is correct |
23 |
Correct |
56 ms |
17908 KB |
Output is correct |
24 |
Correct |
55 ms |
17352 KB |
Output is correct |
25 |
Correct |
55 ms |
17372 KB |
Output is correct |
26 |
Correct |
57 ms |
18136 KB |
Output is correct |
27 |
Correct |
58 ms |
18180 KB |
Output is correct |
28 |
Correct |
54 ms |
20416 KB |
Output is correct |
29 |
Correct |
56 ms |
21696 KB |
Output is correct |
30 |
Correct |
21 ms |
9976 KB |
Output is correct |
31 |
Correct |
55 ms |
13452 KB |
Output is correct |
32 |
Correct |
166 ms |
20864 KB |
Output is correct |
33 |
Correct |
57 ms |
13912 KB |
Output is correct |
34 |
Correct |
51 ms |
14212 KB |
Output is correct |
35 |
Correct |
33 ms |
11500 KB |
Output is correct |
36 |
Correct |
22 ms |
9720 KB |
Output is correct |
37 |
Correct |
22 ms |
10400 KB |
Output is correct |
38 |
Correct |
73 ms |
12668 KB |
Output is correct |
39 |
Correct |
82 ms |
12624 KB |
Output is correct |
40 |
Correct |
74 ms |
12668 KB |
Output is correct |
41 |
Correct |
75 ms |
12508 KB |
Output is correct |
42 |
Correct |
78 ms |
12660 KB |
Output is correct |
43 |
Correct |
85 ms |
12312 KB |
Output is correct |
44 |
Correct |
87 ms |
12536 KB |
Output is correct |
45 |
Correct |
99 ms |
12324 KB |
Output is correct |
46 |
Correct |
88 ms |
12412 KB |
Output is correct |
47 |
Correct |
82 ms |
12416 KB |
Output is correct |
48 |
Correct |
78 ms |
9264 KB |
Output is correct |
49 |
Correct |
75 ms |
9396 KB |
Output is correct |
50 |
Correct |
82 ms |
10040 KB |
Output is correct |
51 |
Correct |
77 ms |
10060 KB |
Output is correct |
52 |
Correct |
72 ms |
9168 KB |
Output is correct |
53 |
Correct |
71 ms |
9004 KB |
Output is correct |
54 |
Correct |
73 ms |
9172 KB |
Output is correct |