結果

問題 No.1997 X Lighting
ユーザー trineutrontrineutron
提出日時 2022-07-01 22:43:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 2,218 bytes
コンパイル時間 4,693 ms
コンパイル使用メモリ 209,496 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2023-08-17 10:18:28
合計ジャッジ時間 6,175 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
7,772 KB
testcase_01 AC 7 ms
7,828 KB
testcase_02 AC 6 ms
7,824 KB
testcase_03 AC 5 ms
7,668 KB
testcase_04 AC 6 ms
7,788 KB
testcase_05 AC 9 ms
7,784 KB
testcase_06 AC 33 ms
8,616 KB
testcase_07 AC 6 ms
7,660 KB
testcase_08 AC 9 ms
7,828 KB
testcase_09 AC 6 ms
7,708 KB
testcase_10 AC 5 ms
7,796 KB
testcase_11 AC 6 ms
7,824 KB
testcase_12 AC 6 ms
7,884 KB
testcase_13 AC 52 ms
9,248 KB
testcase_14 AC 160 ms
12,676 KB
testcase_15 AC 36 ms
9,100 KB
testcase_16 AC 47 ms
9,748 KB
testcase_17 AC 102 ms
11,004 KB
testcase_18 AC 60 ms
10,120 KB
testcase_19 AC 122 ms
11,480 KB
testcase_20 AC 153 ms
11,788 KB
testcase_21 AC 152 ms
12,800 KB
testcase_22 AC 123 ms
11,504 KB
testcase_23 AC 80 ms
10,348 KB
testcase_24 AC 8 ms
7,740 KB
testcase_25 AC 147 ms
12,596 KB
testcase_26 AC 123 ms
11,760 KB
testcase_27 AC 126 ms
11,040 KB
testcase_28 AC 47 ms
9,172 KB
testcase_29 AC 45 ms
9,480 KB
testcase_30 AC 143 ms
12,636 KB
testcase_31 AC 98 ms
10,864 KB
testcase_32 AC 31 ms
8,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
void compress(vector<T> &a) {
    auto sorted{a};
    sort(sorted.begin(), sorted.end());
    sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());
    for (auto &&x : a) {
        x = lower_bound(sorted.begin(), sorted.end(), x) - sorted.begin();
    }
}

int main() {
    int64_t n, m;
    cin >> n >> m;
    vector<int64_t> l(m), r(m);
    for (int i = 0; i < m; i++) {
        int64_t x, y;
        cin >> x >> y;
        l.at(i) = x + y;
        r.at(i) = x - y;
    }
    sort(l.begin(), l.end());
    l.erase(unique(l.begin(), l.end()), l.end());
    sort(r.begin(), r.end());
    r.erase(unique(r.begin(), r.end()), r.end());
    int64_t ans = 0;
    for (auto &&x : l) {
        ans += min(x - 1, 2 * n + 1 - x);
    }
    for (auto &&x : r) {
        ans += min(x + n, n - x);
    }

    vector<int64_t> merged0, merged1;
    int l0 = 0, l1 = 0;
    for (auto &&x : l) {
        if (x % 2 == 0) {
            merged0.emplace_back(x);
            l0++;
        } else {
            merged1.emplace_back(x);
            l1++;
        }
    }
    for (auto &&x : r) {
        if (x % 2 == 0) {
            merged0.emplace_back(2 + abs(x));
            merged0.emplace_back(2 * n - abs(x) + 1);
        } else {
            merged1.emplace_back(2 + abs(x));
            merged1.emplace_back(2 * n - abs(x) + 1);
        }
    }
    compress(merged0);
    compress(merged1);

    vector<int64_t> s0(300001), s1(300001);
    for (int i = 0; i < l0; i++) {
        s0.at(merged0.at(i) + 1)++;
    }
    for (int i = 0; i < l1; i++) {
        s1.at(merged1.at(i) + 1)++;
    }
    for (int i = 0; i < 300000; i++) {
        s0.at(i + 1) += s0.at(i);
        s1.at(i + 1) += s1.at(i);
    }

    int r0 = (merged0.size() - l0) / 2;
    int r1 = (merged1.size() - l1) / 2;
    for (int i = 0; i < r0; i++) {
        int a = merged0.at(l0 + 2 * i);
        int b = merged0.at(l0 + 2 * i + 1);
        ans += s0.at(a) - s0.at(b);
    }
    for (int i = 0; i < r1; i++) {
        int a = merged1.at(l1 + 2 * i);
        int b = merged1.at(l1 + 2 * i + 1);
        ans += s1.at(a) - s1.at(b);
    }

    cout << ans << endl;
    return 0;
}
0