結果
| 問題 |
No.121 傾向と対策:門松列(その2)
|
| コンテスト | |
| ユーザー |
sugim48
|
| 提出日時 | 2015-01-08 23:51:53 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 831 ms / 5,000 ms |
| コード長 | 1,978 bytes |
| コンパイル時間 | 773 ms |
| コンパイル使用メモリ | 100,360 KB |
| 実行使用メモリ | 42,096 KB |
| 最終ジャッジ日時 | 2024-06-28 18:32:09 |
| 合計ジャッジ時間 | 4,941 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 9 |
ソースコード
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v, w; };
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int INF = INT_MAX / 2;
struct bit {
vector<ll> v;
bit(int n) : v(n + 1) {}
ll sum(int i) {
ll res = 0;
for (; i > 0; i -= i & -i) res += v[i];
return res;
}
void add(int i, ll x) {
for (i++; i < v.size(); i += i & -i) v[i] += x;
}
int lower_bound(ll x) {
if (x <= 0) return 0;
int res = 0;
for (int i = 1 << 24; i > 0; i >>= 1)
if (res + i < v.size() && v[res + i] < x) {
res += i; x -= v[res];
}
return res + 1;
}
};
int main() {
int N; cin >> N;
vector<int> A(N), _A;
for (int i = 0; i < N; i++)
cin >> A[i];
_A = A;
sort(_A.begin(), _A.end());
_A.erase(unique(_A.begin(), _A.end()), _A.end());
for (int i = 0; i < N; i++)
A[i] = lower_bound(_A.begin(), _A.end(), A[i]) - _A.begin();
int n = _A.size();
bit b1(n), b2(n);
int cnt1 = 0, cnt2 = N;
vector<ll> l(n), r(n);
for (int i = 0; i < N; i++) {
b2.add(A[i], 1);
r[A[i]]++;
}
r[A[0]]--;
ll hoge = 0;
ll ans = 0;
for (int i = 0; i < N; i++) {
b2.add(A[i], -1); cnt2--;
ans += b1.sum(A[i]) * b2.sum(A[i]);
ans += (cnt1 - b1.sum(A[i] + 1)) * (cnt2 - b2.sum(A[i] + 1));
b1.add(A[i], 1); cnt1++;
ans -= hoge - l[A[i]] * r[A[i]];
if (i + 1 == N) break;
r[A[i + 1]]--;
hoge -= l[A[i + 1]];
hoge += r[A[i]];
l[A[i]]++;
//cout << hoge << endl;
//cout << ans << endl;
}
cout << ans << endl;
}
sugim48