# Untitled

unknown
c_cpp
a month ago
3.9 kB
4
Indexable
Never
```#include <bits/stdc++.h>

#define no "NO"
#define yes "YES"
#define F first
#define S second
#define vec vector
#define task "main"
#define ll long long
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maximize(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool minimize(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}

const int N = (int)2e5 + 9;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

int n;
int a[N];
ll ans = 0;
int minVal[N], maxVal[N];
int sumMin[2], sumMax[2];

void solve(int l, int r) {
int mid = (l + r) >> 1;
minVal[mid + 1] = (int)1e9;
maxVal[mid + 1] = 0;
FOD(i, mid, l) {
minVal[i] = min(minVal[i + 1], a[i]);
maxVal[i] = max(maxVal[i + 1], a[i]);
}
FOR(i, mid + 1, r) {
minVal[i] = min(minVal[i - 1], a[i]);
maxVal[i] = max(maxVal[i - 1], a[i]);
}

// max and min on the left
for (int i = mid, R = mid; i >= l; i--) {
while (R < r && minVal[R + 1] >= minVal[i] && maxVal[R + 1] <= maxVal[i])
R++;
if (minVal[R] >= minVal[i] && maxVal[R] <= maxVal[i])
if ((maxVal[i] - minVal[i]) % 2 == 0)
ans += (R - mid + 1);
}

// max and min on the right
for (int i = l, L = r, cnt = ((maxVal[L] - minVal[L]) % 2 == 0); i <= mid; i++) {
while (L > mid && minVal[L - 1] < minVal[i] && maxVal[L - 1] > maxVal[i]) {
L--;
if ((maxVal[L] - minVal[L]) % 2 == 0) cnt++;
}
if (minVal[L] < minVal[i] && maxVal[L] > maxVal[i])
ans += cnt;
}

// max on the left, min on the right
memset(sumMin, 0, sizeof(sumMin));
sumMin[(minVal[mid] % 2)]++;
for (int i = mid, L = mid, R = mid; i >= l; i--) {
while (R < r && minVal[R] >= minVal[i]) {
R++;
sumMin[minVal[R] % 2]++;
}
while (R < r && maxVal[R + 1] <= maxVal[i]) {
R++;
sumMin[minVal[R] % 2]++;
}
while (L < r && minVal[L] >= minVal[i]) {
sumMin[minVal[L] % 2]--;
L++;
}
if (minVal[L] < minVal[i] && maxVal[i] >= maxVal[R])
ans += sumMin[maxVal[i] % 2];
}

// min on the left, max on the right
memset(sumMax, 0, sizeof(sumMax));
sumMax[(minVal[mid] % 2)]++;
for (int i = mid, L = mid, R = mid; i >= l; i--) {
while (R < r && maxVal[R] <= maxVal[i]) {
R++;
sumMax[maxVal[R] % 2]++;
}
while (R < r && minVal[R + 1] >= minVal[i]) {
R++;
sumMax[maxVal[R] % 2]++;
}
while (L < r && maxVal[L] <= maxVal[i]) {
sumMax[maxVal[L] % 2]--;
L++;
}
if (minVal[L] >= minVal[i] && maxVal[i] < maxVal[R])
ans += sumMax[minVal[i] % 2];
}
if (mid > l) solve(l, mid - 1);
if (mid < r) solve(mid + 1, r);
}

void main_code() {
cin >> n;
FOR(i, 1, n) cin >> a[i];

solve(1, n);
cout << ans;
}

int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0;
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); cout << "\n"; }
}

/* Lak lu theo dieu nhac */
```
Leave a Comment