結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー けーむけーむ
提出日時 2020-08-17 17:49:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,779 bytes
コンパイル時間 1,470 ms
コンパイル使用メモリ 171,720 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-19 18:58:59
合計ジャッジ時間 2,159 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
6,944 KB
testcase_02 WA -
testcase_03 AC 2 ms
6,944 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
6,940 KB
testcase_11 WA -
testcase_12 AC 7 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T> void chmax(T &a, const T b){ a = max(a, b); }
template<class T> void chmin(T &a, const T b){ a = min(a, b); }

template<typename T>
struct BinaryIndexedTree {
private:
    int n;
    vector<T> bit;
    
public:
    BinaryIndexedTree(int _n) : n(_n + 1), bit(n + 1, 0){}
    
    T sum(int idx) {
        T s(0);
        for(int x = idx + 1; x > 0; x -= (x & -x)) s += bit[x];
        return s;
    }
    
    void add(int idx, T a) {
        for(int x = idx + 1; x <= n; x += (x & -x)) bit[x] += a;
    }
    
    T query(int l, int r) {
        return sum(r - 1) - sum(l - 1);
    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    const ll MV = 30000;
    ll n;
    cin >> n;
    vector<pair<ll, ll>> cat(n);
    rep(i, n) {
        ll m;
        cin >> m;
        cat[i] = make_pair(min(i + 1, m), max(i + 1, m));
    }
    sort(all(cat), [](const pair<ll, ll> &a, const pair<ll, ll> &b) {
        if (a.first != b.first) return a.first < b.first;
        return a.second > b.second;
    });
    BinaryIndexedTree<ll> bit(MV + 1);
    ll ans = 0;
    rep(i, n) {
        ans += bit.query(cat[i].first + 1, MV + 1);
        bit.add(cat[i].second, 1);
    }
    cout << ans << endl;
    return 0;
}
0