結果
| 問題 |
No.956 Number of Unbalanced
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-02-03 19:18:50 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 310 ms / 2,000 ms |
| コード長 | 2,682 bytes |
| コンパイル時間 | 1,857 ms |
| コンパイル使用メモリ | 172,576 KB |
| 実行使用メモリ | 9,472 KB |
| 最終ジャッジ日時 | 2024-09-19 04:56:53 |
| 合計ジャッジ時間 | 4,507 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 6 |
| other | AC * 24 |
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/vector:64,
from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/queue:61,
from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:86,
from main.cpp:2:
In member function 'std::vector<_Tp, _Alloc>::size_type std::vector<_Tp, _Alloc>::size() const [with _Tp = int; _Alloc = std::allocator<int>]',
inlined from 'void solve()' at main.cpp:71:23:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_vector.h:988:40: warning: iteration 100000 invokes undefined behavior [-Waggressive-loop-optimizations]
988 | { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
| ~~~~~~~~~~~~~~^~~~~~~~~
main.cpp: In function 'void solve()':
main.cpp:69:23: note: within this loop
69 | for (int A = 1; A <= MA; ++A) {
| ~~^~~~~
ソースコード
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
template<class Val>
struct BinaryIndexedTree {
int n;
vector<Val> t;
BinaryIndexedTree() {}
BinaryIndexedTree(int _n) :n(_n + 1), t(_n + 1) {}
void add(int k, Val val) {
k++;
while (k < n) {
t[k] += val;
k += (k&-k);
}
}
void set(int k, Val val) {
add(k, -sum(k, k + 1));
add(k, val);
}
Val sum(int k) {
Val r = 0;
while (k > 0) {
r += t[k];
k -= (k&-k);
}
return r;
}
Val sum(int l, int r) {
return sum(r) - sum(l);
}
};
const int MA = 100001, SQ = 320;
vi indices[MA];
void solve() {
int N;
cin >> N;
rep(i, N) {
int A;
cin >> A;
indices[A].push_back(i);
}
vi flag(N, -1);
ll ans = 0;
BinaryIndexedTree<ll> bit(2 * N + 2);
for (int A = 1; A <= MA; ++A) {
auto &ids = indices[A];
const int M = sz(ids);
each(id, ids) {
flag[id] = 1;
}
if (M < SQ) {
// 最も左の要素を固定
rep(i, M) {
int p = ids[i];
// 左にいくつ0があるか
int leftZeroes = (i ? ids[i] - ids[i - 1] - 1 : p);
int cnt = 0;
FOR(q, p, min(N, p + 2 * SQ)) {
cnt += flag[q];
// cnt, cnt-1, cnt-2, ,,, cnt-leftZeroes
// 1以上を数える
if (cnt > 0) {
ans += min(cnt, leftZeroes + 1);
}
}
}
} else {
int sum = 0;
fill(all(bit.t), 0);
bit.add(N, 1);
for (int i = 0; i < N; ++i) {
sum += flag[i];
ans += bit.sum(N + sum);
bit.add(N + sum, 1);
}
}
each(id, ids) {
flag[id] = -1;
}
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
solve();
return 0;
}