#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FOD(i, a, b) for(int i = a; i >= b; i--)
#define taskname "MAJORITY"
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 500010;
struct SegTree{
#define mid ((l + r) >> 1)
ll d[N * 4], lazy[N * 4], start[N * 4];
ll calc(int l, int r){
ll len = r - l + 1;
return (len + 1) * len / 2;
}
void doLazy(int id, int l, int r){
if (lazy[id] == 0 && start == 0)
return;
d[id] += lazy[id] * calc(l, r) + start[id] * (r - l + 1);
if (l != r){
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
start[id << 1] += start[id];
start[id << 1 | 1] += start[id] + (mid + 1 - l) * lazy[id];
}
lazy[id] = 0;
start[id] = 0;
}
void update(int id, int l, int r, int u, int v, int st, int val){
doLazy(id, l, r);
if (v < l || r < u)
return;
if (u <= l && r <= v){
lazy[id] += val;
start[id] += st + val * (l - u);
doLazy(id, l, r);
return;
}
update(id << 1, l, mid, u, v, st, val);
update(id << 1 | 1, mid + 1, r, u, v, st, val);
d[id] = d[id << 1] + d[id << 1 | 1];
}
ll get(int id, int l, int r, int u, int v){
doLazy(id, l, r);
if (v < l || r < u)
return 0;
if (u <= l && r <= v)
return d[id];
return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
}
#undef mid
} ST;
int n, a[N];
vector <int> pos[N];
void readInput(){
cin >> n;
FOR(i, 1, n)
pos[i].push_back(0);
vector<int> a(n);
for (int &x : a) {
cin >> x;
}
auto b = a;
sort(b.begin(), b.end());
for (int i = 1; i <= n; ++i) {
int x = lower_bound(b.begin(), b.end(), a[i - 1]) - b.begin() + 1;
pos[x].push_back(i);
}
FOR(i, 1, n)
pos[i].push_back(n + 1);
}
void solve(){
ll ans = 0;
FOR(i, 1, n){
FOR(j, 0, pos[i].size() - 2){
int r = j * 2 - pos[i][j];
int l = r - (pos[i][j + 1] - pos[i][j] - 1);
ans += ST.get(1, 0, 2 * n, l - 1 + n, r - 1 + n);
ST.update(1, 0, 2 * n, l + n, r + n, 0, 1);
ST.update(1, 0, 2 * n, r + n + 1, n * 2, (r - l + 1), 0);
}
FOR(j, 0, pos[i].size() - 2){
int r = j * 2 - pos[i][j];
int l = r - (pos[i][j + 1] - pos[i][j] - 1);
ST.update(1, 0, 2 * n, l + n, r + n, 0, -1);
ST.update(1, 0, 2 * n, r + n + 1, n * 2, -(r - l + 1), 0);
}
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
readInput();
solve();
return 0;
}
Compilation message
Main.cpp: In function 'void readInput()':
Main.cpp:6:22: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
6 | #define FOR(i, a, b) for(int i = a; i <= b; i++)
| ^~~
Main.cpp:66:5: note: in expansion of macro 'FOR'
66 | FOR(i, 1, n)
| ^~~
Main.cpp:68:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
68 | vector<int> a(n);
| ^~~~~~
Main.cpp: In function 'void solve()':
Main.cpp:6:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | #define FOR(i, a, b) for(int i = a; i <= b; i++)
......
86 | FOR(j, 0, pos[i].size() - 2){
| ~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:86:9: note: in expansion of macro 'FOR'
86 | FOR(j, 0, pos[i].size() - 2){
| ^~~
Main.cpp:6:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | #define FOR(i, a, b) for(int i = a; i <= b; i++)
......
93 | FOR(j, 0, pos[i].size() - 2){
| ~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:93:9: note: in expansion of macro 'FOR'
93 | FOR(j, 0, pos[i].size() - 2){
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12076 KB |
Output is correct |
3 |
Correct |
7 ms |
12100 KB |
Output is correct |
4 |
Correct |
8 ms |
12116 KB |
Output is correct |
5 |
Correct |
9 ms |
12076 KB |
Output is correct |
6 |
Correct |
7 ms |
12164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12076 KB |
Output is correct |
3 |
Correct |
7 ms |
12100 KB |
Output is correct |
4 |
Correct |
8 ms |
12116 KB |
Output is correct |
5 |
Correct |
9 ms |
12076 KB |
Output is correct |
6 |
Correct |
7 ms |
12164 KB |
Output is correct |
7 |
Correct |
11 ms |
12204 KB |
Output is correct |
8 |
Correct |
9 ms |
11988 KB |
Output is correct |
9 |
Correct |
11 ms |
12340 KB |
Output is correct |
10 |
Correct |
11 ms |
12244 KB |
Output is correct |
11 |
Correct |
10 ms |
12304 KB |
Output is correct |
12 |
Correct |
10 ms |
12244 KB |
Output is correct |
13 |
Correct |
10 ms |
12284 KB |
Output is correct |
14 |
Correct |
10 ms |
12244 KB |
Output is correct |
15 |
Correct |
11 ms |
12244 KB |
Output is correct |
16 |
Correct |
12 ms |
12228 KB |
Output is correct |
17 |
Correct |
10 ms |
12316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
18192 KB |
Output is correct |
2 |
Correct |
411 ms |
19736 KB |
Output is correct |
3 |
Correct |
192 ms |
16552 KB |
Output is correct |
4 |
Correct |
381 ms |
20112 KB |
Output is correct |
5 |
Correct |
429 ms |
20448 KB |
Output is correct |
6 |
Correct |
412 ms |
20736 KB |
Output is correct |
7 |
Correct |
431 ms |
20720 KB |
Output is correct |
8 |
Correct |
397 ms |
20752 KB |
Output is correct |
9 |
Correct |
429 ms |
20840 KB |
Output is correct |
10 |
Correct |
399 ms |
20840 KB |
Output is correct |
11 |
Correct |
426 ms |
31644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12076 KB |
Output is correct |
3 |
Correct |
7 ms |
12100 KB |
Output is correct |
4 |
Correct |
8 ms |
12116 KB |
Output is correct |
5 |
Correct |
9 ms |
12076 KB |
Output is correct |
6 |
Correct |
7 ms |
12164 KB |
Output is correct |
7 |
Correct |
11 ms |
12204 KB |
Output is correct |
8 |
Correct |
9 ms |
11988 KB |
Output is correct |
9 |
Correct |
11 ms |
12340 KB |
Output is correct |
10 |
Correct |
11 ms |
12244 KB |
Output is correct |
11 |
Correct |
10 ms |
12304 KB |
Output is correct |
12 |
Correct |
10 ms |
12244 KB |
Output is correct |
13 |
Correct |
10 ms |
12284 KB |
Output is correct |
14 |
Correct |
10 ms |
12244 KB |
Output is correct |
15 |
Correct |
11 ms |
12244 KB |
Output is correct |
16 |
Correct |
12 ms |
12228 KB |
Output is correct |
17 |
Correct |
10 ms |
12316 KB |
Output is correct |
18 |
Correct |
292 ms |
18192 KB |
Output is correct |
19 |
Correct |
411 ms |
19736 KB |
Output is correct |
20 |
Correct |
192 ms |
16552 KB |
Output is correct |
21 |
Correct |
381 ms |
20112 KB |
Output is correct |
22 |
Correct |
429 ms |
20448 KB |
Output is correct |
23 |
Correct |
412 ms |
20736 KB |
Output is correct |
24 |
Correct |
431 ms |
20720 KB |
Output is correct |
25 |
Correct |
397 ms |
20752 KB |
Output is correct |
26 |
Correct |
429 ms |
20840 KB |
Output is correct |
27 |
Correct |
399 ms |
20840 KB |
Output is correct |
28 |
Correct |
426 ms |
31644 KB |
Output is correct |
29 |
Correct |
483 ms |
33620 KB |
Output is correct |
30 |
Correct |
165 ms |
16700 KB |
Output is correct |
31 |
Correct |
343 ms |
21228 KB |
Output is correct |
32 |
Correct |
956 ms |
32320 KB |
Output is correct |
33 |
Correct |
349 ms |
21568 KB |
Output is correct |
34 |
Correct |
464 ms |
21676 KB |
Output is correct |
35 |
Correct |
230 ms |
17376 KB |
Output is correct |
36 |
Correct |
173 ms |
16516 KB |
Output is correct |
37 |
Correct |
167 ms |
16784 KB |
Output is correct |
38 |
Correct |
560 ms |
27412 KB |
Output is correct |
39 |
Correct |
472 ms |
27208 KB |
Output is correct |
40 |
Correct |
440 ms |
27376 KB |
Output is correct |
41 |
Correct |
591 ms |
27276 KB |
Output is correct |
42 |
Correct |
498 ms |
27396 KB |
Output is correct |
43 |
Correct |
585 ms |
31844 KB |
Output is correct |
44 |
Correct |
483 ms |
32056 KB |
Output is correct |
45 |
Correct |
527 ms |
31884 KB |
Output is correct |
46 |
Correct |
469 ms |
31932 KB |
Output is correct |
47 |
Correct |
519 ms |
31892 KB |
Output is correct |
48 |
Correct |
691 ms |
32504 KB |
Output is correct |
49 |
Correct |
611 ms |
32468 KB |
Output is correct |
50 |
Correct |
603 ms |
32664 KB |
Output is correct |
51 |
Correct |
667 ms |
32644 KB |
Output is correct |
52 |
Correct |
621 ms |
32416 KB |
Output is correct |
53 |
Correct |
657 ms |
32296 KB |
Output is correct |
54 |
Correct |
555 ms |
32304 KB |
Output is correct |