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);
FOR(i, 1, n){
int x;
cin >> x;
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(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
readInput();
solve();
return 0;
}
Compilation message (stderr)
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++)
......
81 | FOR(j, 0, pos[i].size() - 2){
| ~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:81:9: note: in expansion of macro 'FOR'
81 | 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++)
......
88 | FOR(j, 0, pos[i].size() - 2){
| ~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:88:9: note: in expansion of macro 'FOR'
88 | FOR(j, 0, pos[i].size() - 2){
| ^~~
Main.cpp: In function 'int main()':
Main.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |