結果

問題 No.1864 Shortest Paths Counting
ユーザー KKT89KKT89
提出日時 2022-03-05 09:02:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 2,836 bytes
コンパイル時間 3,206 ms
コンパイル使用メモリ 145,196 KB
実行使用メモリ 9,836 KB
最終ジャッジ日時 2023-09-26 14:36:13
合計ジャッジ時間 6,895 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 59 ms
6,572 KB
testcase_10 AC 61 ms
6,816 KB
testcase_11 AC 56 ms
6,480 KB
testcase_12 AC 81 ms
7,860 KB
testcase_13 AC 58 ms
6,516 KB
testcase_14 AC 62 ms
6,884 KB
testcase_15 AC 67 ms
7,304 KB
testcase_16 AC 58 ms
6,536 KB
testcase_17 AC 66 ms
6,964 KB
testcase_18 AC 62 ms
6,752 KB
testcase_19 AC 59 ms
6,460 KB
testcase_20 AC 72 ms
7,268 KB
testcase_21 AC 57 ms
6,096 KB
testcase_22 AC 63 ms
6,744 KB
testcase_23 AC 58 ms
6,656 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 100 ms
9,836 KB
testcase_26 AC 34 ms
6,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

constexpr ll mod = 998244353;

// 0-indexed
struct BIT{
    vector<ll> s;
    BIT(int n): s(n){}
    void update(int pos,ll dif){
        for(;pos<s.size();pos|=pos+1)(s[pos]+=dif)%=mod;
    }
    ll query(int pos){ // sum of values in [0,pos)
        ll res=0;
        for(;pos>0;pos&=pos-1)(res+=s[pos-1])%=mod;
        return res;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<ll> x(n),y(n);
    for(int i=0;i<n;i++){
        cin >> x[i] >> y[i];
        ll X = x[i]+y[i];
        ll Y = x[i]-y[i];
        x[i] = X;
        y[i] = Y;
    }
    if(x[0] > x[n-1]){
        for(int i=0;i<n;i++){
            x[i] *= -1;
        }
    }
    if(y[0] > y[n-1]){
        for(int i=0;i<n;i++){
            y[i] *= -1;
        }
    }
    vector<ll> X,Y;
    for(int i=0;i<n;i++){
        if(x[0] <= x[i] and x[i] <= x[n-1] and y[0] <= y[i] and y[i] <= y[n-1]){
            X.push_back(x[i]);
            Y.push_back(y[i]);
        }
    }
    { // 座圧
        auto v = X;
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        for(int i=0;i<X.size();i++){
            X[i] = lower_bound(v.begin(), v.end(), X[i]) - v.begin();
        }

        auto vv = Y;
        sort(vv.begin(), vv.end());
        vv.erase(unique(vv.begin(), vv.end()), vv.end());
        for(int i=0;i<X.size();i++){
            Y[i] = lower_bound(vv.begin(), vv.end(), Y[i]) - vv.begin();
        }
    }

    int m = X.size();
    vector<int> idx(m);
    for(int i=0;i<m;i++){
        idx[i] = i;
    }
    sort(idx.begin(), idx.end(), [&](auto i,auto j){
        if(X[i] == X[j]){
            return Y[i] < Y[j];
        }
        else{
            return X[i] < X[j];
        }
    });

    vector<ll> dp(m);
    dp[0] = 1;
    BIT bit(m);
    bit.update(Y[0], dp[0]);
    for(int _i=1;_i<m;_i++){
        int i = idx[_i];
        ll val = bit.query(Y[i]+1);
        dp[_i] = val;
        bit.update(Y[i], dp[_i]);
    }
    cout << dp.back() << endl;
}
0