結果

問題 No.3597 Queen Score Attack 2
コンテスト
ユーザー tnakao0123
提出日時 2026-07-25 16:29:25
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 88 ms / 2,000 ms
+ 57µs
コード長 943 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 185 ms
コンパイル使用メモリ 54,092 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2026-07-25 16:29:30
合計ジャッジ時間 4,537 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/* -*- coding: utf-8 -*-
 *
 * 3597.cc:  No.3597 Queen Score Attack 2 - yukicoder
 */

#include<cstdio>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 500000;
const long long LINF = 1LL << 60;

/* typedef */

using ll = long long;

/* global variables */

ll dp[MAX_N + 1];

/* subroutines */

bool move(int x0, int y0, int x1, int y1) {
  return x0 == x1 || y0 == y1 || abs(x1 - x0) == abs(y1 - y0);
}

void setmax(ll &a, ll b) { if (a < b) a = b; }

/* main */

int main() {
  int h, w, sx, sy, n;
  scanf("%d%d%d%d%d", &h, &w, &sx, &sy, &n);

  ll maxdp = -LINF;
  int px = sx, py = sy;
  for (int i = 0; i < n; i++) {
    int xi, yi, ci;
    scanf("%d%d%d", &xi, &yi, &ci);

    dp[i + 1] = maxdp + ci;
    if (move(px, py, xi, yi))
      setmax(dp[i + 1], dp[i] + ci);
    setmax(maxdp, dp[i]);
    px = xi, py = yi;
  }

  ll maxsc = *max_element(dp, dp + n + 1);
  printf("%lld\n", maxsc);

  return 0;
}

0