結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー nebukuro09
提出日時 2018-10-05 22:02:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 908 ms / 2,500 ms
コード長 1,246 bytes
コンパイル時間 1,707 ms
コンパイル使用メモリ 197,608 KB
最終ジャッジ日時 2025-01-06 14:18:25
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);i++)
#define REP2(i,m,n) for (int i=m;i<(n);i++)
typedef long long ll;
typedef long double ld;

const ll MOD = 1000000007;
int N, K;
pair<int, int> LR[30303];
pair<int, int> RL[30303];
int same[30303];
int A[30303];

void solve() {
    int N; cin >> N;
    int x = 0, y = 0, z = 0;

    REP2(i, 1, N+1) {
        int m; cin >> m;
        if (i < m) LR[x++] = make_pair(i, m);
        else if (m < i) RL[y++] = make_pair(m, i);
        else same[z++] = i;
    }

    REP(i, z) A[same[i]] += 1;
    REP(i, N+1) A[i+1] += A[i];

    int ans = 0;

    sort(LR, LR+x);
    sort(RL, RL+y);


    REP(i, x) REP2(j, i+1, x) {
        if (LR[j].second <= LR[i].second)
            ans++;
    }

    REP(i, y) REP2(j, i+1, y) {
        if (RL[j].second <= RL[i].second)
            ans++;
    }

    REP(i, x) {
        ans += A[LR[i].second] - A[LR[i].first-1];
    }

    REP(i, y) {
        ans += A[RL[i].second] - A[RL[i].first-1];
    }

    REP(i, x) REP(j, y) {
        if (LR[i].first <= RL[j].second && LR[i].second >= RL[j].first)
            ans++;
    }

    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    solve();
}
0