結果

問題 No.1649 Manhattan Square
ユーザー kimiyukikimiyuki
提出日時 2021-08-21 09:46:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 631 ms / 3,000 ms
コード長 2,659 bytes
コンパイル時間 1,823 ms
コンパイル使用メモリ 109,668 KB
実行使用メモリ 13,644 KB
最終ジャッジ日時 2024-04-23 01:42:23
合計ジャッジ時間 23,332 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 6 ms
5,248 KB
testcase_03 AC 6 ms
5,376 KB
testcase_04 AC 6 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 6 ms
5,376 KB
testcase_07 AC 489 ms
13,496 KB
testcase_08 AC 489 ms
13,616 KB
testcase_09 AC 631 ms
13,628 KB
testcase_10 AC 490 ms
13,624 KB
testcase_11 AC 483 ms
13,492 KB
testcase_12 AC 401 ms
9,928 KB
testcase_13 AC 441 ms
11,320 KB
testcase_14 AC 424 ms
10,948 KB
testcase_15 AC 433 ms
11,204 KB
testcase_16 AC 381 ms
9,728 KB
testcase_17 AC 391 ms
9,924 KB
testcase_18 AC 362 ms
9,344 KB
testcase_19 AC 422 ms
11,080 KB
testcase_20 AC 397 ms
9,928 KB
testcase_21 AC 458 ms
11,084 KB
testcase_22 AC 478 ms
13,512 KB
testcase_23 AC 501 ms
13,640 KB
testcase_24 AC 481 ms
13,516 KB
testcase_25 AC 499 ms
13,516 KB
testcase_26 AC 483 ms
13,512 KB
testcase_27 AC 486 ms
13,508 KB
testcase_28 AC 489 ms
13,640 KB
testcase_29 AC 486 ms
13,512 KB
testcase_30 AC 480 ms
13,516 KB
testcase_31 AC 486 ms
13,636 KB
testcase_32 AC 481 ms
13,636 KB
testcase_33 AC 482 ms
13,508 KB
testcase_34 AC 485 ms
13,512 KB
testcase_35 AC 496 ms
13,512 KB
testcase_36 AC 476 ms
13,644 KB
testcase_37 AC 494 ms
13,512 KB
testcase_38 AC 475 ms
13,508 KB
testcase_39 AC 482 ms
13,516 KB
testcase_40 AC 551 ms
13,640 KB
testcase_41 AC 496 ms
13,640 KB
testcase_42 AC 409 ms
13,512 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <atcoder/modint>
#include <atcoder/segtree>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;
using namespace atcoder;

using mint = modint998244353;
mint plus_op(mint a, mint b) { return a + b; }
mint plus_e() { return 0; }

mint solve(int n, const std::vector<int64_t> &x, const std::vector<int64_t> &y) {
    mint ans = 0;

    // \sum \sum (x_i - x_j)^2 + (y_i - y_j)^2
    mint sum_x = 0;
    mint sum_y = 0;
    REP (i, n) {
        ans += mint(x[i]) * mint(x[i]) * (n - 1);
        ans += mint(y[i]) * mint(y[i]) * (n - 1);
        ans -= 2 * sum_x * mint(x[i]);
        ans -= 2 * sum_y * mint(y[i]);
        sum_x += x[i];
        sum_y += y[i];
    }

    // 2 \sum \sum |x_i - x_j| |y_i - y_j|
    vector<int> order_x(n);
    iota(ALL(order_x), 0);
    sort(ALL(order_x), [&](int i, int j) { return x[i] > x[j]; });
    vector<int64_t> compress_y = y;
    sort(ALL(compress_y));
    compress_y.erase(unique(ALL(compress_y)), compress_y.end());
    const int H = compress_y.size();
    segtree<mint, plus_op, plus_e> segtree_sum(H);
    segtree<mint, plus_op, plus_e> segtree_cnt(H);
    for (int j : order_x) {
        // - 2 \sum \sum x_j |y_i - y_j|
        int k = lower_bound(ALL(compress_y), y[j]) - compress_y.begin();
        ans -= 2 * x[j] * (segtree_sum.prod(k + 1, H) - segtree_cnt.prod(k + 1, H) * y[j]);
        ans -= 2 * x[j] * (segtree_cnt.prod(0, k) * y[j] - segtree_sum.prod(0, k));
        segtree_sum.set(k, segtree_sum.get(k) + y[j]);
        segtree_cnt.set(k, segtree_cnt.get(k) + 1);
    }
    for (int i : order_x) {
        // 2 \sum \sum x_i |y_i - y_j|
        int k = lower_bound(ALL(compress_y), y[i]) - compress_y.begin();
        segtree_sum.set(k, segtree_sum.get(k) - y[i]);
        segtree_cnt.set(k, segtree_cnt.get(k) - 1);
        ans += 2 * x[i] * (segtree_sum.prod(k + 1, H) - segtree_cnt.prod(k + 1, H) * y[i]);
        ans += 2 * x[i] * (segtree_cnt.prod(0, k) * y[i] - segtree_sum.prod(0, k));
    }

    return ans;
}

// generated by oj-template v4.8.0 (https://github.com/online-judge-tools/template-generator)
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N;
    std::cin >> N;
    std::vector<int64_t> x(N), y(N);
    REP (i, N) { std::cin >> x[i] >> y[i]; }
    auto ans = solve(N, x, y);
    std::cout << ans.val() << '\n';
    return 0;
}
0