結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-10-21 04:26:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,690 bytes
コンパイル時間 2,741 ms
コンパイル使用メモリ 199,124 KB
実行使用メモリ 149,620 KB
最終ジャッジ日時 2023-09-28 14:04:25
合計ジャッジ時間 12,169 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
9,344 KB
testcase_01 AC 230 ms
11,908 KB
testcase_02 AC 13 ms
4,380 KB
testcase_03 AC 1,384 ms
34,992 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.10.21 04:25:52
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


template < class Monoid >
struct SegmentTree {
private:
    using Func = std::function< Monoid(Monoid, Monoid) >;
    Func F;
    Monoid UNITY;
    int n;
    std::vector< Monoid > node;
    int _binary_search(int a, int b, const std::function<bool(Monoid)> &f, Monoid &s, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return n;
        if (a <= l && r <= b && !f(F(s,node[k]))) {
            s = F(s,node[k]);
            return n;
        }
        if (l == r - 1) {s = F(s,node[k]); return l;}
        int ret = _binary_search(a, b, f, s, 2 * k + 1, l, (r - l) / 2 + l);
        return ret != n ? ret : _binary_search(a, b, f, s, 2 * k + 2, (r - l) / 2 + l, r);
    }
public:
    SegmentTree() {}
    SegmentTree(const std::vector< Monoid > &v, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        int sz = v.size();
        n = 1;
        while (n < sz) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
        build();
    }
    
    SegmentTree(int m, const Monoid &val, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        n = 1;
        while (n < m) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        if (val != UNITY) {
            for (int i = 0; i < m; i++) node[i + n - 1] = val;
            build();
        }
    }
    
    void set(int k, const Monoid &x) {
        node[n + k - 1] = x;
    }
    void build() {
        for (int i = n - 2; i >= 0; i--) node[i] = F(node[2 * i + 1], node[2 * i + 2]);
    }
    void update_query(int x, const Monoid &val) {
        if (x >= n || x < 0) return;
        x += n - 1;
        node[x] = val;
        while (x > 0) {
            x = (x - 1) >> 1;
            node[x] = F(node[2 * x + 1], node[2 * x + 2]);
        }
    }
    
    Monoid get_query(int l, int r) {
        Monoid L = UNITY, R = UNITY;
        if (l < 0) l = 0;
        if (r < 0) return UNITY;
        if (l >= n) return UNITY;
        if (r >= n) r = n;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) L = F(L, node[l++ - 1]);
            if (r & 1) R = F(node[--r - 1], R);
        }
        return F(L, R);
    }
    
    
    int binary_search(int l, int r, const std::function<bool(Monoid)> &f) {
        Monoid s = UNITY;
        int ret = _binary_search(l,r,f,s);
        return ret != n ? ret : -1;
    }
    Monoid operator[](int x) const {
        return node[n + x - 1];
    }
    int size() {
        return n;
    }
    void print() {
        for (int i = 0; i < n; i++) {
            std::cout << i << "\t: " << node[n + i - 1] << std::endl;
        }
    }
};
int main() {
    int n;cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    
    SegmentTree<int> seg1(n,0,[](int a,int b){return a+b;},0);
    vector<pair<int,int>> b(n);
    for (int i = 0; i < n; i++) {
        b[i].first = a[i];
        b[i].second = i;
    }
    sort(b.begin(), b.end());
    vector<int> tmp;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        ans += (ll)seg1.get_query(0,b[i].second) * seg1.get_query(b[i].second,n);
        if (i == n-1 || b[i].first != b[i+1].first) {
            for (int t : tmp) {
                seg1.update_query(t,1);
            }
            seg1.update_query(b[i].second,1);
            tmp.clear();
        }
        else {
            tmp.push_back(b[i].second);
        }
    }
    
    SegmentTree<int> seg2(n,0,[](int a,int b){return a+b;},0);
    sort(b.rbegin(), b.rend());
    for (int i = 0; i < n; i++) {
        ans += (ll)seg2.get_query(0,b[i].second) * seg2.get_query(b[i].second,n);
        if (i == n-1 || b[i].first != b[i+1].first) {
            for (int t : tmp) {
                seg2.update_query(t,1);
            }
            seg2.update_query(b[i].second,1);
            tmp.clear();
        }
        else {
            tmp.push_back(b[i].second);
        }
    }
    map<int,int> sum;
    for (int i = 0; i < n; i++) {
        sum[a[i]]++;
    }
    map<int,int> pos;
    int idx = 0;
    for(auto p : sum) {
        pos[p.first] = idx++;
    }
    vector<int> c(idx);
    SegmentTree<ll> seg3(idx,0,[](ll a,ll b){return a+b;},0);
    for (int i = 0; i < n; i++) {
        ans -= seg3.get_query(0,pos[a[i]]) + seg3.get_query(pos[a[i]]+1,idx);
        seg3.update_query(pos[a[i]],seg3[pos[a[i]]] + sum[a[i]] - 2 * c[pos[a[i]]] - 1);
        c[pos[a[i]]]++;
    }
    cout << ans << endl;
    return 0;
}
0