結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー assy1028assy1028
提出日時 2016-12-17 18:36:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,329 ms / 5,000 ms
コード長 3,289 bytes
コンパイル時間 1,063 ms
コンパイル使用メモリ 113,108 KB
実行使用メモリ 68,980 KB
最終ジャッジ日時 2023-09-11 04:11:00
合計ジャッジ時間 7,418 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
21,216 KB
testcase_01 AC 82 ms
22,220 KB
testcase_02 AC 11 ms
20,128 KB
testcase_03 AC 541 ms
27,256 KB
testcase_04 AC 2,329 ms
68,980 KB
testcase_05 AC 537 ms
27,192 KB
testcase_06 AC 490 ms
27,052 KB
testcase_07 AC 615 ms
27,228 KB
testcase_08 AC 589 ms
27,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int N;
int a[1000100];

// 1 base
template<typename T, int max_n>
class BIT {
private:
    T bit[max_n + 1];
    int n;
public:
    BIT(int _n = max_n) {
        n = _n;
        memset(bit, 0, sizeof(bit));
    }
    // [1, i]
    T sum(int i) {
        T s = 0;
        while (i > 0) {
            s += bit[i];
            i -= i & -i;
        }
        return s;
    }
    // [a, b]
    T sum(int a, int b) {
        return sum(b) - sum(a-1);
    }

    void add(int i, T x) {
        while (i <= n) {
            bit[i] += x;
            i += i & -i;
        }
    }

    int lower_bound(T x) {
        int lb = 0, ub = n;
        while (ub - lb > 1) {
            int mid = (ub + lb) / 2;
            T v = sum(mid);
            if (v < x) {
                lb = mid;
            } else {
                ub = mid;
            }
        }
        return ub;
    }

    int upper_bound(T x) {
        int lb = 1, ub = n+1;
        while (ub - lb > 1) {
            int mid = (ub + lb) / 2;
            T v = sum(mid);
            if (v <= x) {
                lb = mid;
            } else {
                ub = mid;
            }
        }
        return ub;
    }
};

unordered_map<int, int> c;

BIT<ll, 1000003> rbit;
BIT<ll, 1000003> fbit;

ll solve() {
    vector<int> aa;
    for (int i = 0; i < N; i++) aa.push_back(a[i]);
    sort(ALL(aa));
    UNIQ(aa);
    int l = aa.size();
    for (int i = 0; i < l; i++) {
        c[aa[i]] = i+1;
    }

    ll same = 0, res = 0;
    for (int i = 2; i < N; i++) {
        rbit.add(c[a[i]], 1);
    }
    fbit.add(c[a[0]], 1);

    same = rbit.sum(c[a[0]], c[a[0]]);

    for (int i = 1; i < N-1; i++) {
        res += fbit.sum(c[a[i]]-1) * rbit.sum(c[a[i]]-1)
            + fbit.sum(c[a[i]]+1, l) * rbit.sum(c[a[i]]+1, l)
            - same + fbit.sum(c[a[i]], c[a[i]]) * rbit.sum(c[a[i]], c[a[i]]);
        fbit.add(c[a[i]], 1);
        same += rbit.sum(c[a[i]], c[a[i]]);
        rbit.add(c[a[i+1]], -1);
        same -= fbit.sum(c[a[i+1]], c[a[i+1]]);
    }

    return res;
}

void input() {
    cin >> N;
    for (int i = 0; i < N; i++) cin >> a[i];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}
0