結果
| 問題 |
No.956 Number of Unbalanced
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2019-12-19 02:40:07 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 97 ms / 2,000 ms |
| コード長 | 3,886 bytes |
| コンパイル時間 | 1,584 ms |
| コンパイル使用メモリ | 139,676 KB |
| 最終ジャッジ日時 | 2025-01-08 12:23:48 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 6 |
| other | AC * 24 |
ソースコード
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1},
// dx[] = {0, -1, -1, -1, 0, 1, 1, 1};
struct IOSetup {
IOSetup() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
cerr << fixed << setprecision(10);
}
} iosetup;
/*-------------------------------------------------*/
template <typename Abelian>
struct BIT {
BIT(int n, const Abelian &UNITY = 0) : n(n), UNITY(UNITY), dat(n, UNITY) {}
void add(int idx, const Abelian &value) {
while (idx < n) {
dat[idx] += value;
idx |= idx + 1;
}
}
Abelian sum(int idx) {
Abelian res = UNITY;
while (idx >= 0) {
res += dat[idx];
idx = (idx & (idx + 1)) - 1;
}
return res;
}
Abelian sum(int left, int right) {
if (right < left) return UNITY;
return sum(right) - sum(left - 1);
}
Abelian operator[](const int idx) { return sum(idx, idx); }
int lower_bound(Abelian value) {
if (value <= UNITY) return 0;
int res = 0, exponent = 1;
while (exponent <= n) exponent <<= 1;
for (int mask = exponent >> 1; mask > 0; mask >>= 1) {
if (res + mask - 1 < n && dat[res + mask - 1] < value) {
value -= dat[res + mask - 1];
res += mask;
}
}
return res;
}
private:
int n;
const Abelian UNITY;
vector<Abelian> dat;
};
long long solve(vector<int> &a) {
int n = a.size();
vector<int> b(a);
FOR(i, 1, n) a[i] += a[i - 1];
map<int, int> cnt;
REP(i, n) ++cnt[a[i]];
int m = cnt.size();
BIT<long long> bit(m);
int idx = 0;
for (auto it = cnt.begin(); it != cnt.end(); ++it, ++idx) {
bit.add(idx, (*it).second);
}
int fro = (*cnt.begin()).first, border = 1 - fro;
long long ans = 0;
REP(i, n) {
ans += bit.sum(max(border, 0), m - 1);
bit.add(a[i] - fro, -1);
border += b[i];
}
return ans;
}
int main() {
const int A = 100000;
int n; cin >> n;
vector<vector<int> > idx(A);
REP(i, n) {
int a; cin >> a; --a;
idx[a].emplace_back(i);
}
long long ans = 0;
REP(now, A) {
if (idx[now].empty()) continue;
vector<int> conc;
int cnt = 0;
REP(i, idx[now].size()) {
conc.emplace_back(idx[now][i]);
++cnt;
int j = idx[now][i] + 1;
while (cnt > 0 && j < n && (i + 1 == idx[now].size() || j < idx[now][i + 1])) {
conc.emplace_back(j++);
--cnt;
}
}
cnt = 0;
for (int i = idx[now].size() - 1; i >= 0; --i) {
++cnt;
int j = idx[now][i] - 1;
while (cnt > 0 && j >= 0 && (i == 0 || idx[now][i - 1] < j)) {
conc.emplace_back(j--);
--cnt;
}
}
sort(ALL(conc));
conc.erase(unique(ALL(conc)), conc.end());
set<int> st;
for (int e : idx[now]) st.emplace(e);
for (int i = 0; i < conc.size();) {
vector<int> sub{st.count(conc[i]) == 1 ? 1 : -1};
int j = i + 1;
while (j < conc.size() && conc[j] - conc[j - 1] == 1) {
sub.emplace_back(st.count(conc[j]) == 1 ? 1 : -1);
++j;
}
ans += solve(sub);
i = j;
}
}
cout << ans << '\n';
return 0;
}
emthrm