結果

問題 No.1864 Shortest Paths Counting
ユーザー Carpenters-CatCarpenters-Cat
提出日時 2022-03-04 23:12:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 259 ms / 2,000 ms
コード長 8,148 bytes
コンパイル時間 2,147 ms
コンパイル使用メモリ 153,160 KB
実行使用メモリ 16,800 KB
最終ジャッジ日時 2023-09-26 02:26:43
合計ジャッジ時間 6,873 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 159 ms
6,820 KB
testcase_10 AC 164 ms
7,296 KB
testcase_11 AC 158 ms
6,520 KB
testcase_12 AC 200 ms
10,772 KB
testcase_13 AC 159 ms
6,464 KB
testcase_14 AC 166 ms
7,432 KB
testcase_15 AC 178 ms
8,828 KB
testcase_16 AC 159 ms
6,884 KB
testcase_17 AC 174 ms
8,556 KB
testcase_18 AC 171 ms
7,844 KB
testcase_19 AC 161 ms
6,588 KB
testcase_20 AC 182 ms
8,944 KB
testcase_21 AC 156 ms
6,256 KB
testcase_22 AC 170 ms
7,856 KB
testcase_23 AC 162 ms
6,992 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 259 ms
16,800 KB
testcase_26 AC 89 ms
6,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using P = pair<int, int>;
using Vp = vector<P>;
using VVp = vector<Vp>;
using Pl = pair<ll, int>;
using Vpl = vector<Pl>;
using VVpl = vector<Vpl>;
using tup = tuple<ll, ll, int>;
using Vt = vector<tuple<int, int, int>>;
using Pll = pair<ll, ll>;
using Vc = vector<char>;
using VVc = vector<Vc>;
template <class U>
using PQmax = priority_queue<U>;
template <class U>
using PQmin = priority_queue<U, vector<U>, greater<U>>;


void tprsort(int u, const VVi& gr, Vi& tpr, Vi& par);
void tprsort(const VVi& gr, Vi& tpr);
ll mpow(ll x, ll n, ll m = 1e9 + 7);
ll comb(int n, int r, const Vl& kai, const Vl& fkai, ll m = 1e9 + 7);
ll gcd(ll a, ll b);
int LCA(const VVi& par, const Vi& depth, int a, int b);
Vi sccResolve(const VVi& gr);
void dijkstra(const VVi& gr, const VVl& cost, Vl& dist, int s);
class unionfind
{
private:
    Vi par;
    Vi siz;
    Vi val;

public:
    unionfind(int N);
    unionfind();
    int root(int v);
    void merge(int a, int b);
    bool same(int a, int b);
    int value(int a);
};
class BIT
{
private:
    Vl bit;
    int siz;

public:
    BIT(int N);
    BIT();
    ll& get(int id);
    void add(int id, ll a, ll m);
    ll sum(int id, ll m);
};

int main()
{
    int N;
    cin >> N;
    Vp X(N);
    for (auto& [x, y] : X) {
        cin >> x >> y;
    }
    if (abs(X[N - 1].second - X[0].second) > abs(X[N - 1].first - X[0].first)) {
        for (auto& [x, y] : X) {
            swap(x, y);
        }
    }
    if (X[0].first > X[N - 1].first) {
        swap(X[0], X[N - 1]);
    }
    Vp A;
    A.emplace_back(X[0].first + X[0].second, X[0].first - X[0].second);
    A.emplace_back(X[N - 1].first + X[N - 1].second, X[N - 1].first - X[N - 1].second);
    set<int> st;
    st.insert(A[0].second);
    st.insert(A[1].second);
    for (int i = 1; i < N - 1; i++) {
        auto [x, y] = X[i];
        auto [a0, b0] = A[0];
        auto [a1, b1] = A[1];
        if (x + y < a0 || x + y > a1 || x - y < b0 || x - y > b1) {
            continue;
        }
        A.emplace_back(x + y, x - y);
        st.insert(A.back().second);
    }
    map<int, int> mpa;
    Vi inv;
    for (auto s : st) {
        mpa[s] = inv.size();
        inv.push_back(s);
    }
    sort(A.begin(), A.end());
    BIT dp(N + 1);
    ll m = 998244353;
    // dp.get(1) = 1;
    dp.add(1, 1, m);
    for (int i = 1; i < A.size(); i++) {
        auto& [x, y] = A[i];
        y = mpa[y] + 1;
        // cout << y << " : ";
        ll ad = dp.sum(y, m);
        // cout << ad << endl;
        if (i == A.size() - 1) {
            cout << ad << endl;
            return 0;
        }
        dp.add(y, ad, m);
    }
    // cout << dp.get(A.back().second) << endl;
}
// Library
void tprsort(const VVi& gr, Vi& tpr)
{
    queue<int> que;
    int N = gr.size();
    Vi siz(N, 0);
    for (int i = 0; i < N; i++) {
        for (int j : gr[i]) {
            siz[j]++;
        }
    }
    for (int i = 0; i < N; i++) {
        if (siz[i] == 0) {
            que.push(i);
        }
    }
    while (!que.empty()) {
        int u = que.front();
        que.pop();
        tpr.push_back(u);
        for (int v : gr[u]) {
            siz[v]--;
            if (siz[v] == 0) {
                que.push(v);
            }
        }
    }
    reverse(tpr.begin(), tpr.end());
}
void tprsort(int u, const VVi& gr, Vi& tpr, Vi& par)
{
    // idx[u] = tpr.size();
    /*for (int v : gr[u]) {
        if (par[v] == u || par[v] == -1) {
            par[v] = u;
            tprsort(v, gr, tpr, par);
        }
    }
    tpr.push_back(u);*/
    stack<int> st;
    st.push(u);
    bool vis[2000020];
    for (int i = 0; i <= gr.size(); i++) {
        vis[i] = false;
    }
    while (!st.empty()) {
        int v = st.top();
        if (!vis[v]) {
            vis[v] = true;
            for (int p : gr[v]) {
                if (par[p] == -1) {
                    par[p] = v;
                    st.push(p);
                }
            }
        } else {
            tpr.push_back(v);
            st.pop();
        }
    }
}

Vi sccResolve(const VVi& gr)
{
    int N = gr.size();
    Vi tpr;
    Vi par(N, -1);
    for (int i = 0; i < N; i++) {
        if (par[i] == -1) {
            tprsort(i, gr, tpr, par);
        }
    }
    Vi ret(N, -1);
    int now = 0;
    for (int i = N - 1; i >= 0; i--) {
        int u = tpr[i];
        if (ret[u] != -1) {
            continue;
        }
        ret[u] = now;
        stack<int> st;
        st.push(u);
        while (!st.empty()) {
            int v = st.top();
            st.pop();
            for (int p : gr[v]) {
                if (ret[p] == -1) {
                    st.push(p);
                    ret[p] = now;
                }
            }
        }
        now++;
    }
    return ret;
}

ll gcd(ll a, ll b)
{
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll mpow(ll x, ll n, ll m)
{
    ll ret = 1;
    while (n) {
        if (n % 2) {
            ret *= x;
            ret %= m;
        }
        x = (x * x) % m;
        n /= 2;
    }
    return ret;
}
ll comb(int n, int r, const Vl& kai, const Vl& fkai, ll m)
{
    if (n < 0 || r < 0 || n < r) {
        return 0;
    }
    ll ret = kai[n];
    ret *= fkai[r];
    ret %= m;
    ret *= fkai[n - r];
    ret %= m;
    return ret;
}

int LCA(const VVi& par, const Vi& depth, int a, int b)
{
    if (depth[a] < depth[b]) {
        swap(a, b);
    }
    int dis = depth[a] - depth[b];
    for (int i = 19; i >= 0; i--) {
        if ((dis >> i) & 1) {
            a = par[i][a];
        }
    }
    if (a == b) {
        return a;
    }
    for (int i = 19; i >= 0; i--) {
        if (par[i][a] != par[i][b]) {
            a = par[i][a];
            b = par[i][b];
        }
    }
    return par[0][a];
}

void dijkstra(const VVi& gr, const VVl& cost, Vl& dist, int s)
{
    ll INF = (ll)1e18;
    dist.assign(gr.size(), INF);
    dist[s] = 0;
    PQmin<Pl> pque;
    pque.push(make_pair(0, s));
    while (!pque.empty()) {
        auto [L, u] = pque.top();
        pque.pop();
        while (L != dist[u] && !pque.empty()) {
            tie(L, u) = pque.top();
            pque.pop();
        }
        for (int i = 0; i < gr[u].size(); i++) {
            int v = gr[u][i];
            ll c = cost[u][i];
            if (dist[v] > c + L) {
                dist[v] = c + L;
                pque.push(make_pair(dist[v], v));
            }
        }
    }
    return;
}
unionfind::unionfind(int N)
{
    par.resize(N + 1);
    siz.assign(N + 1, 1);
    val.assign(N + 1, 0);
    for (int i = 0; i <= N; i++) {
        par[i] = i;
    }
}
unionfind::unionfind()
{
    par.resize(100010);
    siz.assign(100010, 1);
    val.assign(100010, 0);
    for (int i = 0; i <= 100010; i++) {
        par[i] = i;
    }
}
int unionfind::root(int v)
{
    if (v == par[v]) {
        return v;
    }
    return par[v] = root(par[v]);
}
void unionfind::merge(int a, int b)
{
    a = root(a);
    b = root(b);
    if (a == b) {
        return;
    }
    if (siz[a] < siz[b]) {
        int t = a;
        a = b;
        b = t;
    }
    par[b] = a;
    siz[a] += siz[b];
    /*
    ここにvalについての演算処理を書く
    */
}
bool unionfind::same(int a, int b)
{
    a = root(a);
    b = root(b);
    return a == b;
}
int unionfind::value(int a)
{
    return siz[this->root(a)];
}

BIT::BIT(int N)
{
    siz = N;
    bit.assign(N + 1, 0);
}

BIT::BIT()
{
    siz = 0;
}

ll& BIT::get(int id)
{
    return bit[id];
}

void BIT::add(int id, ll a, ll m)
{
    while (id <= siz) {
        bit[id] += a;
        bit[id] %= m;
        id += (id & (-id));
    }
}

ll BIT::sum(int id, ll m)
{
    ll ret = 0;
    while (id) {
        ret += bit[id];
        ret %= m;
        id -= (id & (-id));
    }
    return ret;
}
0