結果

問題 No.2382 Amidakuji M
ユーザー オレンジジュースオレンジジュース
提出日時 2023-07-21 02:26:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,366 bytes
コンパイル時間 2,460 ms
コンパイル使用メモリ 206,636 KB
実行使用メモリ 7,092 KB
最終ジャッジ日時 2023-10-21 04:09:16
合計ジャッジ時間 5,405 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 38 ms
4,784 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 41 ms
5,048 KB
testcase_07 AC 25 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 57 ms
5,840 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 WA -
testcase_19 AC 97 ms
7,092 KB
testcase_20 AC 96 ms
7,092 KB
testcase_21 AC 97 ms
7,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

class SegTree {
private:
	void update(int idx, int lo, int hi, int pos, int val) {
		if (hi < pos || pos < lo) return;
		if (lo == hi) { tree[idx] = val; return; }
		int mid = (lo + hi) >> 1;
		update(idx << 1, lo, mid, pos, val);
		update(idx << 1 | 1, mid + 1, hi, pos, val);
		tree[idx] = tree[idx << 1] + tree[idx << 1 | 1];
	}
	int query(int idx, int lo, int hi, int s, int e) {
		if (hi < s || e < lo) return 0;
		if (s <= lo && hi <= e) return tree[idx];
		int mid = (lo + hi) >> 1;
		return query(idx << 1, lo, mid, s, e) + query(idx << 1 | 1, mid + 1, hi, s, e);
	}
public:
	SegTree(int n): n(n), tree(4 * n) { }
	void update(int pos, int val) {
		update(1, 1, n, pos, val);
	}
	int query(int s, int e) {
		return query(1, 1, n, s, e);
	}

	int n;
	vector<int> tree;
};

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

    int n; ll m;
    cin >> n >> m;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
    }

    ll s = 0;
    SegTree seg(n);
    for (int i = 0; i < n; i++) {
        seg.update(p[i], 1);
        s += seg.query(p[i] + 1, n);
    }

    if (s % 2 == 1 && m % 2 == 0) {
        cout << -1 << '\n';
    }
    else {
        if (s % m == 0) cout << s << '\n';
        else cout << (s / m + 1) * m << '\n';
    }
    return 0;
}
0