結果

問題 No.1864 Shortest Paths Counting
ユーザー shiomusubi496
提出日時 2022-01-05 16:27:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 291 ms / 2,000 ms
コード長 1,542 bytes
コンパイル時間 2,151 ms
コンパイル使用メモリ 207,680 KB
最終ジャッジ日時 2025-01-27 08:48:32
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 998244353;

class BinaryIndexedTree {
private:
    int n;
    vector<ll> data;
public:
    BinaryIndexedTree(int n) : n(n), data(n + 1) {}
    void add(int k, ll x) {
        for (++k; k <= n; k += k & -k) (data[k] += x) %= mod;
    }
    ll sum(int k) {
        ll res = 0;
        for (; k; k -= k & -k) (res += data[k]) %= mod;
        return res;
    }
};

int compress(vector<ll>& A) {
    auto B = A;
    sort(B.begin(), B.end());
    B.erase(unique(B.begin(), B.end()), B.end());
    for (auto&& i : A) {
        i = lower_bound(B.begin(), B.end(), i) - B.begin();
    }
    return B.size();
}

int main() {
    int N; cin >> N;
    vector<ll> X(N), Y(N);
    for (int i=0; i<N; ++i) {
        ll a, b; cin >> a >> b;
        X[i] = a + b;
        Y[i] = a - b;
    }
    if (X.front() > X.back()) {
        for (auto&& i : X) i = -i;
    }
    if (Y.front() > Y.back()) {
        for (auto&& i : Y) i = -i;
    }
    compress(X);
    int K = compress(Y);
    vector<pair<int, int>> A;
    for (int i=0; i<N; ++i) {
        if (X.front() > X[i] || X[i] > X.back()) continue;
        if (Y.front() > Y[i] || Y[i] > Y.back()) continue;
        A.emplace_back(X[i], Y[i]);
    }
    int M = A.size();
    sort(A.begin(), A.end());
    BinaryIndexedTree BIT(K);
    for (int i=0; i<M; ++i) {
        if (i == 0) BIT.add(0, 1);
        else if (i == M - 1) cout << BIT.sum(K) << endl;
        else BIT.add(A[i].second, BIT.sum(A[i].second + 1));
    }
}
0