#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define pii pair<int, int>
#define pill pair<ll, ll>
#define s second
#define ld long double
using namespace std;
const ll N = 5e5 + 10;
const ll M = 5e6 + 10;
const ll mod = 1e4 + 7;
const ll big = 1e4;
const double eps = 1e-8;
ll t, n, k;
ll a[N];
//start : 9:59
vector<int> g[N], gg;
int kek[8 * N], sz = 0;
int cur_ver = 0;
struct seg {
ll t[8 * N] = {0};
ll c[8 * N] = {0};
int d[8 * N] = {0};
int ver[8 * N] = {0};
void push(ll v, ll l, ll r) {
if(ver[v] != cur_ver)t[v] = 0, c[v] = 0, d[v] = 0, ver[v] = cur_ver;
t[v] = t[v] + c[v] * (r - l + 1) + d[v] * (r - l + 1) * (r - l + 2) / 2ll;
if(l != r) {
int m = (l + r) >> 1ll;
if(ver[v * 2] != cur_ver)t[v * 2] = 0, c[v * 2] = 0, d[v * 2] = 0, ver[v * 2] = cur_ver;
if(ver[v * 2 + 1] != cur_ver)t[v * 2 + 1] = 0, c[v * 2 + 1] = 0, d[v * 2 + 1] = 0, ver[v * 2 + 1] = cur_ver;
c[v * 2] += c[v];
c[v * 2 + 1] += c[v] + d[v] * ((m + 1) - l);
d[v * 2] += d[v];
d[v * 2 + 1] += d[v];
}
c[v] = 0, d[v] = 0;
return;
}
void upd1(ll l, ll r, ll v = 1, ll tl = 0, ll tr = 2 * n) {
if(l > tr || tl > r)
return;
push(v, tl, tr);
if(l <= tl && tr <= r) {
c[v] += (tl - l);
d[v] += 1;
push(v, tl, tr);
return;
}
ll m = (tl + tr) >> 1ll;
upd1(l , r, v * 2, tl, m);
upd1(l , r, v * 2 + 1, m + 1, tr);
t[v] = t[v * 2] + t[v * 2 + 1];
}
void upd2(ll l, ll r, ll z, ll v = 1, ll tl = 0, ll tr = 2 * n) {
if(l > tr || tl > r)
return;
push(v, tl, tr);
if(l <= tl && tr <= r) {
c[v] += z;
push(v, tl, tr);
return;
}
ll m = (tl + tr) >> 1ll;
upd2(l , r, z, v * 2, tl, m);
upd2(l , r, z, v * 2 + 1, m + 1, tr);
t[v] = t[v * 2] + t[v * 2 + 1];
}
ll get(ll l, ll r, ll v = 1, ll tl = 0, ll tr = 2 * n) {
if(l > tr || tl > r)
return 0;
push(v, tl, tr);
if(l <= tl && tr <= r) {
return t[v];
}
ll m = (tl + tr) >> 1ll;
return get(l , r, v * 2, tl, m) + get(l , r, v * 2 + 1, m + 1, tr);
}
} rt;
int main() {
speed;
cin >> n;
vector<int> szh;
for(int i = 1; i <= n; i++) {
cin >> a[i];
szh.pb(a[i]);
}
sort(szh.begin(), szh.end());
szh.erase(unique(szh.begin(), szh.end()), szh.end());
for (int i = 1; i <= n; i++) {
a[i] = lower_bound(szh.begin(), szh.end(), a[i]) - szh.begin() + 1;
g[a[i]].pb(i);
}
k = szh.size();
ll ans = 0;
for(int i = 1; i <= k; i++) {
if(g[i].size() == 0)continue;
cur_ver++;
rt.upd1(-g[i][0] + 1 + n, n);
rt.upd2(n + 1, 2 * n, n - (n - g[i][0]));
for(int j = 0; j < g[i].size(); j++) {
int cnt = j + 1;
int cur = g[i][j];
int nxt = n + 1;
if(g[i].size() != j + 1)
nxt = g[i][j + 1];
ans = (ans + rt.get(2 * cnt - nxt + n, 2 * cnt - cur + n - 1));
rt.upd1(2 * cnt - (nxt - 1) + n, 2 * cnt - cur + n);
rt.upd2(2 * cnt - cur + n + 1, 2 * n, nxt - cur);
}
}
cout << ans << "\n";
}
/*
6 1
3 1 5 3 7 5
4 6 4
*/
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:109:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int j = 0; j < g[i].size(); j++) {
| ~~^~~~~~~~~~~~~
Main.cpp:113:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
113 | if(g[i].size() != j + 1)
| ~~~~~~~~~~~~^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12068 KB |
Output is correct |
3 |
Correct |
7 ms |
12084 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12068 KB |
Output is correct |
3 |
Correct |
7 ms |
12084 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
8 ms |
12236 KB |
Output is correct |
8 |
Correct |
7 ms |
12084 KB |
Output is correct |
9 |
Correct |
10 ms |
12244 KB |
Output is correct |
10 |
Correct |
8 ms |
12244 KB |
Output is correct |
11 |
Correct |
9 ms |
12220 KB |
Output is correct |
12 |
Correct |
9 ms |
12244 KB |
Output is correct |
13 |
Correct |
8 ms |
12244 KB |
Output is correct |
14 |
Correct |
8 ms |
12244 KB |
Output is correct |
15 |
Correct |
8 ms |
12244 KB |
Output is correct |
16 |
Correct |
9 ms |
12244 KB |
Output is correct |
17 |
Correct |
9 ms |
12244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
126 ms |
15244 KB |
Output is correct |
2 |
Correct |
165 ms |
16176 KB |
Output is correct |
3 |
Correct |
97 ms |
14548 KB |
Output is correct |
4 |
Correct |
177 ms |
16200 KB |
Output is correct |
5 |
Correct |
178 ms |
16180 KB |
Output is correct |
6 |
Correct |
192 ms |
16452 KB |
Output is correct |
7 |
Correct |
194 ms |
16372 KB |
Output is correct |
8 |
Correct |
195 ms |
16872 KB |
Output is correct |
9 |
Correct |
338 ms |
16396 KB |
Output is correct |
10 |
Correct |
200 ms |
16644 KB |
Output is correct |
11 |
Correct |
220 ms |
28220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12068 KB |
Output is correct |
3 |
Correct |
7 ms |
12084 KB |
Output is correct |
4 |
Correct |
6 ms |
12116 KB |
Output is correct |
5 |
Correct |
6 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
8 ms |
12236 KB |
Output is correct |
8 |
Correct |
7 ms |
12084 KB |
Output is correct |
9 |
Correct |
10 ms |
12244 KB |
Output is correct |
10 |
Correct |
8 ms |
12244 KB |
Output is correct |
11 |
Correct |
9 ms |
12220 KB |
Output is correct |
12 |
Correct |
9 ms |
12244 KB |
Output is correct |
13 |
Correct |
8 ms |
12244 KB |
Output is correct |
14 |
Correct |
8 ms |
12244 KB |
Output is correct |
15 |
Correct |
8 ms |
12244 KB |
Output is correct |
16 |
Correct |
9 ms |
12244 KB |
Output is correct |
17 |
Correct |
9 ms |
12244 KB |
Output is correct |
18 |
Correct |
126 ms |
15244 KB |
Output is correct |
19 |
Correct |
165 ms |
16176 KB |
Output is correct |
20 |
Correct |
97 ms |
14548 KB |
Output is correct |
21 |
Correct |
177 ms |
16200 KB |
Output is correct |
22 |
Correct |
178 ms |
16180 KB |
Output is correct |
23 |
Correct |
192 ms |
16452 KB |
Output is correct |
24 |
Correct |
194 ms |
16372 KB |
Output is correct |
25 |
Correct |
195 ms |
16872 KB |
Output is correct |
26 |
Correct |
338 ms |
16396 KB |
Output is correct |
27 |
Correct |
200 ms |
16644 KB |
Output is correct |
28 |
Correct |
220 ms |
28220 KB |
Output is correct |
29 |
Correct |
208 ms |
29704 KB |
Output is correct |
30 |
Correct |
101 ms |
17020 KB |
Output is correct |
31 |
Correct |
217 ms |
21756 KB |
Output is correct |
32 |
Correct |
545 ms |
31888 KB |
Output is correct |
33 |
Correct |
240 ms |
21944 KB |
Output is correct |
34 |
Correct |
237 ms |
22088 KB |
Output is correct |
35 |
Correct |
156 ms |
17760 KB |
Output is correct |
36 |
Correct |
92 ms |
16860 KB |
Output is correct |
37 |
Correct |
116 ms |
17148 KB |
Output is correct |
38 |
Correct |
221 ms |
23728 KB |
Output is correct |
39 |
Correct |
218 ms |
23704 KB |
Output is correct |
40 |
Correct |
229 ms |
23836 KB |
Output is correct |
41 |
Correct |
222 ms |
23896 KB |
Output is correct |
42 |
Correct |
223 ms |
23660 KB |
Output is correct |
43 |
Correct |
247 ms |
28036 KB |
Output is correct |
44 |
Correct |
247 ms |
28080 KB |
Output is correct |
45 |
Correct |
260 ms |
27968 KB |
Output is correct |
46 |
Correct |
264 ms |
27976 KB |
Output is correct |
47 |
Correct |
253 ms |
27976 KB |
Output is correct |
48 |
Correct |
351 ms |
28648 KB |
Output is correct |
49 |
Correct |
343 ms |
28564 KB |
Output is correct |
50 |
Correct |
316 ms |
28588 KB |
Output is correct |
51 |
Correct |
319 ms |
28576 KB |
Output is correct |
52 |
Correct |
356 ms |
28476 KB |
Output is correct |
53 |
Correct |
329 ms |
28460 KB |
Output is correct |
54 |
Correct |
333 ms |
28360 KB |
Output is correct |