This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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){
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |