#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);
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> 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);
}
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);
}
cout << sol;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:98:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (int i = 1; i < v[x].size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
29652 KB |
Output is correct |
2 |
Correct |
21 ms |
29616 KB |
Output is correct |
3 |
Correct |
20 ms |
29632 KB |
Output is correct |
4 |
Correct |
23 ms |
29572 KB |
Output is correct |
5 |
Correct |
20 ms |
29648 KB |
Output is correct |
6 |
Correct |
20 ms |
29600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
29652 KB |
Output is correct |
2 |
Correct |
21 ms |
29616 KB |
Output is correct |
3 |
Correct |
20 ms |
29632 KB |
Output is correct |
4 |
Correct |
23 ms |
29572 KB |
Output is correct |
5 |
Correct |
20 ms |
29648 KB |
Output is correct |
6 |
Correct |
20 ms |
29600 KB |
Output is correct |
7 |
Correct |
23 ms |
29652 KB |
Output is correct |
8 |
Correct |
20 ms |
29576 KB |
Output is correct |
9 |
Correct |
26 ms |
29748 KB |
Output is correct |
10 |
Correct |
28 ms |
29620 KB |
Output is correct |
11 |
Correct |
26 ms |
29652 KB |
Output is correct |
12 |
Correct |
25 ms |
29652 KB |
Output is correct |
13 |
Correct |
30 ms |
29672 KB |
Output is correct |
14 |
Correct |
26 ms |
29636 KB |
Output is correct |
15 |
Correct |
28 ms |
29796 KB |
Output is correct |
16 |
Correct |
26 ms |
29652 KB |
Output is correct |
17 |
Correct |
23 ms |
29692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
32112 KB |
Output is correct |
2 |
Correct |
221 ms |
32744 KB |
Output is correct |
3 |
Correct |
145 ms |
31488 KB |
Output is correct |
4 |
Correct |
227 ms |
32704 KB |
Output is correct |
5 |
Correct |
234 ms |
32756 KB |
Output is correct |
6 |
Correct |
264 ms |
32892 KB |
Output is correct |
7 |
Correct |
244 ms |
32828 KB |
Output is correct |
8 |
Correct |
259 ms |
33152 KB |
Output is correct |
9 |
Correct |
253 ms |
32780 KB |
Output is correct |
10 |
Correct |
248 ms |
32824 KB |
Output is correct |
11 |
Correct |
166 ms |
34148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
29652 KB |
Output is correct |
2 |
Correct |
21 ms |
29616 KB |
Output is correct |
3 |
Correct |
20 ms |
29632 KB |
Output is correct |
4 |
Correct |
23 ms |
29572 KB |
Output is correct |
5 |
Correct |
20 ms |
29648 KB |
Output is correct |
6 |
Correct |
20 ms |
29600 KB |
Output is correct |
7 |
Correct |
23 ms |
29652 KB |
Output is correct |
8 |
Correct |
20 ms |
29576 KB |
Output is correct |
9 |
Correct |
26 ms |
29748 KB |
Output is correct |
10 |
Correct |
28 ms |
29620 KB |
Output is correct |
11 |
Correct |
26 ms |
29652 KB |
Output is correct |
12 |
Correct |
25 ms |
29652 KB |
Output is correct |
13 |
Correct |
30 ms |
29672 KB |
Output is correct |
14 |
Correct |
26 ms |
29636 KB |
Output is correct |
15 |
Correct |
28 ms |
29796 KB |
Output is correct |
16 |
Correct |
26 ms |
29652 KB |
Output is correct |
17 |
Correct |
23 ms |
29692 KB |
Output is correct |
18 |
Correct |
179 ms |
32112 KB |
Output is correct |
19 |
Correct |
221 ms |
32744 KB |
Output is correct |
20 |
Correct |
145 ms |
31488 KB |
Output is correct |
21 |
Correct |
227 ms |
32704 KB |
Output is correct |
22 |
Correct |
234 ms |
32756 KB |
Output is correct |
23 |
Correct |
264 ms |
32892 KB |
Output is correct |
24 |
Correct |
244 ms |
32828 KB |
Output is correct |
25 |
Correct |
259 ms |
33152 KB |
Output is correct |
26 |
Correct |
253 ms |
32780 KB |
Output is correct |
27 |
Correct |
248 ms |
32824 KB |
Output is correct |
28 |
Correct |
166 ms |
34148 KB |
Output is correct |
29 |
Correct |
209 ms |
35544 KB |
Output is correct |
30 |
Correct |
201 ms |
31576 KB |
Output is correct |
31 |
Correct |
411 ms |
33364 KB |
Output is correct |
32 |
Correct |
938 ms |
37692 KB |
Output is correct |
33 |
Correct |
421 ms |
33476 KB |
Output is correct |
34 |
Correct |
452 ms |
33732 KB |
Output is correct |
35 |
Correct |
299 ms |
32328 KB |
Output is correct |
36 |
Correct |
183 ms |
31420 KB |
Output is correct |
37 |
Correct |
215 ms |
31688 KB |
Output is correct |
38 |
Correct |
308 ms |
33720 KB |
Output is correct |
39 |
Correct |
315 ms |
33840 KB |
Output is correct |
40 |
Correct |
315 ms |
33756 KB |
Output is correct |
41 |
Correct |
316 ms |
33968 KB |
Output is correct |
42 |
Correct |
311 ms |
33668 KB |
Output is correct |
43 |
Correct |
407 ms |
35812 KB |
Output is correct |
44 |
Correct |
418 ms |
35728 KB |
Output is correct |
45 |
Correct |
424 ms |
35852 KB |
Output is correct |
46 |
Correct |
408 ms |
35724 KB |
Output is correct |
47 |
Correct |
434 ms |
35804 KB |
Output is correct |
48 |
Correct |
505 ms |
34480 KB |
Output is correct |
49 |
Correct |
506 ms |
34488 KB |
Output is correct |
50 |
Correct |
511 ms |
34624 KB |
Output is correct |
51 |
Correct |
479 ms |
34748 KB |
Output is correct |
52 |
Correct |
491 ms |
34452 KB |
Output is correct |
53 |
Correct |
489 ms |
34340 KB |
Output is correct |
54 |
Correct |
463 ms |
34344 KB |
Output is correct |