結果

問題 No.1864 Shortest Paths Counting
ユーザー tnakao0123tnakao0123
提出日時 2022-03-07 13:12:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 1,852 bytes
コンパイル時間 676 ms
コンパイル使用メモリ 60,048 KB
実行使用メモリ 10,360 KB
最終ジャッジ日時 2023-09-29 13:19:59
合計ジャッジ時間 4,141 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,052 KB
testcase_01 AC 2 ms
7,104 KB
testcase_02 AC 2 ms
7,064 KB
testcase_03 AC 2 ms
7,000 KB
testcase_04 AC 2 ms
7,092 KB
testcase_05 AC 2 ms
7,088 KB
testcase_06 AC 2 ms
7,100 KB
testcase_07 AC 1 ms
7,104 KB
testcase_08 AC 2 ms
7,060 KB
testcase_09 AC 55 ms
8,828 KB
testcase_10 AC 58 ms
8,984 KB
testcase_11 AC 54 ms
8,784 KB
testcase_12 AC 69 ms
9,440 KB
testcase_13 AC 54 ms
8,840 KB
testcase_14 AC 56 ms
9,020 KB
testcase_15 AC 61 ms
9,260 KB
testcase_16 AC 53 ms
8,844 KB
testcase_17 AC 60 ms
9,100 KB
testcase_18 AC 59 ms
9,060 KB
testcase_19 AC 54 ms
8,860 KB
testcase_20 AC 63 ms
9,340 KB
testcase_21 AC 54 ms
8,764 KB
testcase_22 AC 58 ms
9,092 KB
testcase_23 AC 56 ms
8,900 KB
testcase_24 AC 2 ms
7,104 KB
testcase_25 AC 77 ms
10,360 KB
testcase_26 AC 35 ms
8,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1864.cc:  No.1864 Shortest Paths Counting - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const int MOD = 998244353;

/* typedef */

typedef long long ll;
typedef pair<ll,ll> pll;

template <typename T>
struct BIT {
  int n;
  vector<T> bits;
  
  BIT() {}
  BIT(int _n) { init(_n); }

  void init(int _n) {
    n = _n;
    bits.resize(n + 1, 0);
  }

  T sum(int x) {
    x = min(x, n);
    T s = 0;
    while (x > 0) {
      s = (s + bits[x]) % MOD;
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    if (x <= 0) return;
    while (x <= n) {
      bits[x] = (bits[x] + v) % MOD;
      x += (x & -x);
    }
  }
};

/* global variables */

ll xs[MAX_N], ys[MAX_N], uys[MAX_N];
pll ps[MAX_N];
BIT<int> bit;

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    int xi, yi;
    scanf("%d%d", &xi, &yi);
    xs[i] = xi + yi, ys[i] = xi - yi;
  }

  bool xrv = (xs[0] > xs[n - 1]);
  bool yrv = (ys[0] > ys[n - 1]);
  for (int i = 0; i < n; i++) {
    if (xrv) xs[i] = -xs[i];
    if (yrv) ys[i] = -ys[i];
  }

  ll x0 = xs[0], y0 = ys[0];
  ll gx = xs[n - 1] - x0, gy = ys[n - 1] - y0;

  int m = 0;
  for (int i = 0; i < n; i++) {
    ll xi = xs[i] - x0, yi = ys[i] - y0;
    if (0 <= xi && xi <= gx && 0 <= yi && yi <= gy) ps[m++] = pll(xi, yi);
  }
  if (m > 2) sort(ps + 1, ps + m - 1);

  for (int i = 0; i < m; i++) uys[i] = ps[i].second;
  sort(uys, uys + m);
  int k = unique(uys, uys + m) - uys;

  bit.init(k);
  bit.add(1, 1);
  for (int i = 1; i < m - 1; i++) {
    int yi = lower_bound(uys, uys + k, ps[i].second) - uys;
    int di = bit.sum(yi + 1);
    bit.add(yi + 1, di);
  }

  int sum = bit.sum(k);
  printf("%d\n", sum);
  return 0;
}
0