//#include "time_measure.h"
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 5;
const int OFF = (1 << 19);
struct Node{
int sum;
ll br;
int cnt;
int prop;
Node(int sum_ = 0, ll br_ = 0, int cnt_ = 0, int prop_ = 0) {
sum = sum_; br = br_; cnt = cnt_; prop = prop_;
}
};
int n;
int p[N], bio[2*OFF];
Node T[2*OFF];
vector <int> v[N], saz;
ll sol = 0;
Node mrg(Node a, Node b) {
assert(a.prop == 0 && a.prop == 0);
return Node(a.sum + b.sum, a.br + b.br + (ll)b.cnt * a.sum, a.cnt + b.cnt);
}
void propag(int pos) {
bio[pos] = 1;
if (T[pos].prop == 0) return;
T[pos].sum += (ll)T[pos].cnt * T[pos].prop;
T[pos].br += (ll)T[pos].cnt * (T[pos].cnt + 1) / 2 * T[pos].prop;
if (pos < OFF) {
T[pos * 2].prop += T[pos].prop;
T[pos * 2 + 1].prop += T[pos].prop;
bio[pos * 2] = bio[pos * 2 + 1] = 1;
}
T[pos].prop = 0;
}
Node query(int a, int b, int pos = 1, int lo = 0, int hi = OFF) {
propag(pos);
if (lo >= b || hi <= a) return Node();
if (lo >= a && hi <= b) return T[pos];
int mid = (lo + hi) / 2;
return mrg(query(a, b, pos * 2, lo, mid), query(a, b, pos * 2 + 1, mid, hi));
}
void update(int a, int b, int pos = 1, int lo = 0, int hi = OFF) {
propag(pos);
if (lo >= b || hi <= a) return;
if (lo >= a && hi <= b) {
T[pos].prop=1;
propag(pos);
return;
}
int mid=(lo+hi)/2;
update(a, b, pos * 2, lo, mid);
update(a, b, pos * 2 + 1, mid, hi);
T[pos] = mrg(T[pos * 2], T[pos * 2 + 1]);
}
void reset(int pos) {
T[pos].br = T[pos].sum = T[pos].prop = 0;
if (pos >= OFF) return;
if (bio[pos * 2]) reset(pos * 2);
if (bio[pos * 2 + 1]) reset(pos * 2 +1);
bio[pos] = 0;
}
void minus_jedan(int l, int r) {
sol += query(l, r).br + (ll)query(0, l).sum * (r - l + 1);
update(l, r + 1);
}
void jedan(int x) {
sol += query(0, x).sum;
update(x, x + 1);
}
void solve() {
for (int i = 0; i < OFF; ++i) T[OFF + i].cnt = 1;
for (int i = OFF - 1; i > 0; --i) T[i] = mrg(T[i * 2], T[i * 2 + 1]);
for (int x = 0; x < n; ++x) {
if (v[x].empty()) continue;
update(n, n + 1);
if (v[x][0]) minus_jedan(n - v[x][0], n - 1);
int pref = n - v[x][0];
jedan(++pref);
for (int i = 1; i < v[x].size(); ++i) {
int raz = v[x][i] - v[x][i - 1] - 1;
if (raz != 0) minus_jedan(pref - raz, pref - 1);
pref = pref - raz + 1;
jedan(pref);
}
if (v[x].back() != n - 1) minus_jedan(pref - (n - v[x].back() - 1), pref - 1);
reset(1);
}
printf("%lld\n", sol);
}
void load() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &p[i]);
saz.pb(p[i]);
}
sort(saz.begin(), saz.end());
saz.erase(unique(saz.begin(), saz.end()), saz.end());
for (int i = 0; i < n; ++i) {
p[i] = lower_bound(saz.begin(), saz.end(), p[i]) - saz.begin();
v[p[i]].pb(i);
}
}
int main() {
// auto vrijeme = GetTimeMs64();
load();
solve();
// FILE *fp=fopen("current_time", "w");
// fprintf(fp, "%lld\n", GetTimeMs64()-vrijeme);
// fclose(fp);
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:103:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int i = 1; i < v[x].size(); ++i) {
| ~~^~~~~~~~~~~~~
Main.cpp: In function 'void load()':
Main.cpp:116:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Main.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d", &p[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
29644 KB |
Output is correct |
2 |
Correct |
17 ms |
29656 KB |
Output is correct |
3 |
Correct |
17 ms |
29644 KB |
Output is correct |
4 |
Correct |
18 ms |
29628 KB |
Output is correct |
5 |
Correct |
18 ms |
29644 KB |
Output is correct |
6 |
Correct |
18 ms |
29644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
29644 KB |
Output is correct |
2 |
Correct |
17 ms |
29656 KB |
Output is correct |
3 |
Correct |
17 ms |
29644 KB |
Output is correct |
4 |
Correct |
18 ms |
29628 KB |
Output is correct |
5 |
Correct |
18 ms |
29644 KB |
Output is correct |
6 |
Correct |
18 ms |
29644 KB |
Output is correct |
7 |
Correct |
24 ms |
29668 KB |
Output is correct |
8 |
Correct |
19 ms |
29628 KB |
Output is correct |
9 |
Correct |
20 ms |
29632 KB |
Output is correct |
10 |
Correct |
21 ms |
29648 KB |
Output is correct |
11 |
Correct |
21 ms |
29716 KB |
Output is correct |
12 |
Correct |
22 ms |
29680 KB |
Output is correct |
13 |
Correct |
21 ms |
29660 KB |
Output is correct |
14 |
Correct |
20 ms |
29716 KB |
Output is correct |
15 |
Correct |
21 ms |
29660 KB |
Output is correct |
16 |
Correct |
23 ms |
29688 KB |
Output is correct |
17 |
Correct |
21 ms |
29644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
31828 KB |
Output is correct |
2 |
Correct |
208 ms |
32416 KB |
Output is correct |
3 |
Correct |
127 ms |
31328 KB |
Output is correct |
4 |
Correct |
215 ms |
32320 KB |
Output is correct |
5 |
Correct |
230 ms |
32320 KB |
Output is correct |
6 |
Correct |
244 ms |
32480 KB |
Output is correct |
7 |
Correct |
234 ms |
32484 KB |
Output is correct |
8 |
Correct |
247 ms |
32692 KB |
Output is correct |
9 |
Correct |
237 ms |
32472 KB |
Output is correct |
10 |
Correct |
249 ms |
32596 KB |
Output is correct |
11 |
Correct |
151 ms |
33588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
29644 KB |
Output is correct |
2 |
Correct |
17 ms |
29656 KB |
Output is correct |
3 |
Correct |
17 ms |
29644 KB |
Output is correct |
4 |
Correct |
18 ms |
29628 KB |
Output is correct |
5 |
Correct |
18 ms |
29644 KB |
Output is correct |
6 |
Correct |
18 ms |
29644 KB |
Output is correct |
7 |
Correct |
24 ms |
29668 KB |
Output is correct |
8 |
Correct |
19 ms |
29628 KB |
Output is correct |
9 |
Correct |
20 ms |
29632 KB |
Output is correct |
10 |
Correct |
21 ms |
29648 KB |
Output is correct |
11 |
Correct |
21 ms |
29716 KB |
Output is correct |
12 |
Correct |
22 ms |
29680 KB |
Output is correct |
13 |
Correct |
21 ms |
29660 KB |
Output is correct |
14 |
Correct |
20 ms |
29716 KB |
Output is correct |
15 |
Correct |
21 ms |
29660 KB |
Output is correct |
16 |
Correct |
23 ms |
29688 KB |
Output is correct |
17 |
Correct |
21 ms |
29644 KB |
Output is correct |
18 |
Correct |
167 ms |
31828 KB |
Output is correct |
19 |
Correct |
208 ms |
32416 KB |
Output is correct |
20 |
Correct |
127 ms |
31328 KB |
Output is correct |
21 |
Correct |
215 ms |
32320 KB |
Output is correct |
22 |
Correct |
230 ms |
32320 KB |
Output is correct |
23 |
Correct |
244 ms |
32480 KB |
Output is correct |
24 |
Correct |
234 ms |
32484 KB |
Output is correct |
25 |
Correct |
247 ms |
32692 KB |
Output is correct |
26 |
Correct |
237 ms |
32472 KB |
Output is correct |
27 |
Correct |
249 ms |
32596 KB |
Output is correct |
28 |
Correct |
151 ms |
33588 KB |
Output is correct |
29 |
Correct |
165 ms |
33684 KB |
Output is correct |
30 |
Correct |
212 ms |
31332 KB |
Output is correct |
31 |
Correct |
394 ms |
32840 KB |
Output is correct |
32 |
Correct |
947 ms |
36444 KB |
Output is correct |
33 |
Correct |
467 ms |
33036 KB |
Output is correct |
34 |
Correct |
479 ms |
33172 KB |
Output is correct |
35 |
Correct |
310 ms |
32036 KB |
Output is correct |
36 |
Correct |
194 ms |
31128 KB |
Output is correct |
37 |
Correct |
228 ms |
31432 KB |
Output is correct |
38 |
Correct |
339 ms |
33424 KB |
Output is correct |
39 |
Correct |
326 ms |
33420 KB |
Output is correct |
40 |
Correct |
443 ms |
33340 KB |
Output is correct |
41 |
Correct |
342 ms |
33568 KB |
Output is correct |
42 |
Correct |
364 ms |
33384 KB |
Output is correct |
43 |
Correct |
581 ms |
33836 KB |
Output is correct |
44 |
Correct |
484 ms |
33752 KB |
Output is correct |
45 |
Correct |
512 ms |
33716 KB |
Output is correct |
46 |
Correct |
451 ms |
33740 KB |
Output is correct |
47 |
Correct |
417 ms |
33796 KB |
Output is correct |
48 |
Correct |
522 ms |
33772 KB |
Output is correct |
49 |
Correct |
533 ms |
33752 KB |
Output is correct |
50 |
Correct |
654 ms |
34060 KB |
Output is correct |
51 |
Correct |
499 ms |
34180 KB |
Output is correct |
52 |
Correct |
598 ms |
33676 KB |
Output is correct |
53 |
Correct |
628 ms |
33600 KB |
Output is correct |
54 |
Correct |
725 ms |
33644 KB |
Output is correct |