結果
| 問題 |
No.2382 Amidakuji M
|
| コンテスト | |
| ユーザー |
ococonomy1
|
| 提出日時 | 2023-07-14 22:06:36 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 82 ms / 2,000 ms |
| コード長 | 3,352 bytes |
| コンパイル時間 | 977 ms |
| コンパイル使用メモリ | 111,380 KB |
| 最終ジャッジ日時 | 2025-02-15 14:06:46 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
using lg = long long;
using pii = pair<int, int>;
using pll = pair<lg, lg>;
#define TEST cerr << "TEST" << endl
#define IINF 2147483647
#define LLINF 9223372036854775807LL
#define AMARI 998244353
// #define AMARI 1000000007
#define TEMOTO ((sizeof(long double) == 16) ? false : true)
#define TIME_LIMIT 1980 * (TEMOTO ? 1 : 1000)
#define el '\n'
#define El '\n'
//抽象再帰セグ木?(抽象セグ木の定義分からんな)(遅延ではない)
//ococo_segtree<int> rsq(n,0);みたいに宣言する
template<typename T>
class ococo_segtree {
T func(T a, T b) {
//ここに演算を入れる
return (a + b);
}
public:
int n;
T tmax;
//range minimum query
vector<T> rmq;
//(データの大きさ,単位元)
ococo_segtree(int N = 0, T t = IINF) {
syokica(N, t);
}
//配列の初期化 O(N)
void syokica(int a, T t) {
n = 1;
tmax = t;
while (n < a)n *= 2;
rmq.resize(2 * n - 1);
for (int i = 0; i < 2 * n - 1; i++) {
rmq[i] = tmax;
}
}
//a[i]をxにする O(logN)
void update_num(int i, T x) {
i += (n - 1);
rmq[i] = x;
while (i) {
i = (i - 1) / 2;
rmq[i] = func(rmq[i * 2 + 1], rmq[i * 2 + 2]);
}
}
//a[i]にxを加える O(logN)
void add_num(int i, T x) {
i += (n - 1);
rmq[i] = func(rmq[i], x);
while (i) {
i = (i - 1) / 2;
rmq[i] = func(rmq[i * 2 + 1], rmq[i * 2 + 2]);
}
}
T get_minimum2(int a, int b, int k, int l, int r) {
if (a <= l && r <= b)return rmq[k];
else if (r <= a || b <= l)return tmax;
else return func(get_minimum2(a, b, k * 2 + 1, l, (l + r) / 2), get_minimum2(a, b, k * 2 + 2, (l + r) / 2, r));
}
//[l,r)の区間演算の取得 O(logN)
T get_val(int l, int r) {
return get_minimum2(l, r, 0, 0, n);
}
};
#define MULTI_TEST_CASE false
void solve(void) {
int n;
lg m;
cin >> n >> m;
vector<int> p(n);
for(int i = 0; i < n; i++)cin >> p[i];
lg tentousuu = 0;
ococo_segtree<int> os(n + 2,0);
for(int i = 0; i < n; i++){
tentousuu += os.get_val(p[i],n + 2);
os.add_num(p[i],1);
}
if(m % 2 == 0 && tentousuu % 2 == 1){
cout << -1 << el;
return;
}
if(tentousuu % m == 0){
cout << tentousuu << el;
return;
}
lg ans = tentousuu;
ans += (m - (ans % m));
if(ans % 2 == tentousuu % 2){
cout << ans << el;
return;
}
ans += m;
if(ans % 2 == tentousuu % 2){
cout << ans << el;
return;
}
cout << -1 << el;
return;
}
void calc(void) {
return;
}
int main(void) {
cin.tie(nullptr);
ios::sync_with_stdio(false);
calc();
int t = 1;
if (MULTI_TEST_CASE) cin >> t;
while (t--) {
solve();
}
return 0;
}
ococonomy1