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 pb push_back
#define pf push_front
using namespace std;
#define F first
#define S second
typedef long long ll;
#define pii pair <int, int>
#define pll pair <ll, ll>
typedef long double ld;
const ll N = 2 * 1e5 + 10;
const ll mod = 1e9 + 7;
ll um(ll a, ll b){
return ((1LL * a * b) % mod + mod) % mod;
}
ll subr(ll a, ll b){
return ((1LL * a - b) % mod + mod) % mod;
}
ll binpow(ll x, ll step){
ll res = 1LL;
while(step){
if(step & 1) res = um(res, x);
x = um(x, x);
step /= 2;
}
return res;
}
struct segtree {
int sz;
vector <int> tree;
void init(int n){
sz = 1;
while(sz < n) sz *= 2;
tree.assign(2 * sz - 1, 0);
}
void upd(int i, int v, int x, int lx, int rx){
if(rx - lx == 1){
tree[x] += v;
return;
}
int mid = (rx + lx) / 2;
if(i < mid) upd(i, v, 2 * x + 1, lx, mid);
else upd(i, v, 2 * x + 2, mid, rx);
tree[x] = max(tree[2 * x + 1], tree[2 * x + 2]);
}
void upd(int i, int v){
upd(i, v, 0, 0, sz);
}
};
int arr[N], b[N], sz, n;
ll ans;
void overall(){
segtree obj;
obj.init(sz);
for(ll i = 0; i < n; i++){
for(int j = i; j < n; j++){
obj.upd(arr[j], 1);
if(obj.tree[0] > (j - i + 1) / 2) ans++;
}
for(int j = i; j < n; j++){
obj.upd(arr[j], -1);
}
}
}
void subtask3(){
map <int, int> mp;
int cur = 0, act = 0;
ans = (1LL * (n * (n + 1))) / 2LL;
for(int i = 0; i < n; i++){
if(arr[i] == 0) cur--;
else cur++;
mp[cur]++;
}
cur = 0;
for(int i = 0; i < n; i++){
ans -= mp[act];
if(arr[i] == 0){
cur--;
act--;
}
else{
cur++;
act++;
}
mp[cur]--;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 0; i < n; i++){
cin >> arr[i];
b[i] = arr[i];
}
sort(b, b + n);
sz = unique(b, b + n) - b;
for(int i = 0; i < n; i++){
arr[i] = lower_bound(b, b + sz, arr[i]) - b;
}
if(sz <= 2){
subtask3();
} else{
overall();
}
cout << ans;
return 0;
}
# | 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... |