結果

問題 No.1014 competitive fighting
ユーザー 👑 emthrmemthrm
提出日時 2020-03-20 23:18:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 7,121 bytes
コンパイル時間 2,871 ms
コンパイル使用メモリ 239,252 KB
実行使用メモリ 9,452 KB
最終ジャッジ日時 2024-07-07 12:54:38
合計ジャッジ時間 8,120 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 95 ms
9,452 KB
testcase_06 AC 95 ms
9,320 KB
testcase_07 AC 95 ms
9,448 KB
testcase_08 AC 95 ms
9,448 KB
testcase_09 AC 92 ms
9,444 KB
testcase_10 AC 78 ms
9,448 KB
testcase_11 AC 70 ms
9,448 KB
testcase_12 AC 98 ms
9,452 KB
testcase_13 AC 43 ms
6,244 KB
testcase_14 AC 39 ms
5,980 KB
testcase_15 AC 47 ms
6,252 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 17 ms
5,376 KB
testcase_18 AC 83 ms
8,888 KB
testcase_19 AC 99 ms
9,320 KB
testcase_20 AC 32 ms
5,868 KB
testcase_21 AC 67 ms
8,676 KB
testcase_22 AC 55 ms
6,632 KB
testcase_23 AC 21 ms
5,376 KB
testcase_24 AC 23 ms
5,376 KB
testcase_25 AC 7 ms
5,376 KB
testcase_26 AC 70 ms
8,556 KB
testcase_27 AC 17 ms
5,376 KB
testcase_28 AC 19 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 90 ms
9,188 KB
testcase_31 AC 72 ms
8,680 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 5 ms
5,376 KB
testcase_34 AC 58 ms
8,548 KB
testcase_35 AC 81 ms
9,196 KB
testcase_36 AC 55 ms
8,424 KB
testcase_37 AC 3 ms
5,376 KB
testcase_38 AC 46 ms
6,500 KB
testcase_39 AC 25 ms
5,376 KB
testcase_40 AC 46 ms
6,628 KB
testcase_41 AC 32 ms
6,120 KB
testcase_42 AC 73 ms
9,192 KB
testcase_43 AC 6 ms
5,376 KB
testcase_44 AC 68 ms
8,808 KB
testcase_45 AC 40 ms
6,372 KB
testcase_46 AC 8 ms
5,376 KB
testcase_47 AC 22 ms
5,376 KB
testcase_48 AC 55 ms
8,520 KB
testcase_49 AC 3 ms
5,376 KB
testcase_50 AC 72 ms
9,064 KB
testcase_51 AC 40 ms
6,248 KB
testcase_52 AC 4 ms
5,376 KB
testcase_53 AC 95 ms
9,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename Monoid>
struct RMQ {
  RMQ(int sz, const Monoid UNITY) : UNITY(UNITY) {
    init(sz);
    dat.assign((n << 1) - 1, UNITY);
  }

  RMQ(const vector<Monoid> &a, const Monoid UNITY) : UNITY(UNITY) {
    int a_sz = a.size();
    init(a_sz);
    dat.resize((n << 1) - 1);
    REP(i, a_sz) dat[n - 1 + i] = a[i];
    for (int i = n - 2; i >= 0; --i) dat[i] = max(dat[(i << 1) + 1], dat[(i << 1) + 2]);
  }

  void update(int node, Monoid val) {
    node += n - 1;
    dat[node] = val;
    while (node > 0) {
      node = (node - 1) >> 1;
      dat[node] = max(dat[(node << 1) + 1], dat[(node << 1) + 2]);
    }
  }

  Monoid query(int a, int b) { return query(a, b, 0, 0, n); }

  int find(int a, int b, Monoid val) { return find(a, b, val, 0, 0, n); }

  Monoid operator[](const int idx) const { return dat[idx + n - 1]; }

private:
  int n = 1;
  const Monoid UNITY;
  vector<Monoid> dat;

  void init(int sz) { while (n < sz) n <<= 1; }

  Monoid query(int a, int b, int node, int left, int right) {
    if (right <= a || b <= left) return UNITY;
    if (a <= left && right <= b) return dat[node];
    return max(query(a, b, (node << 1) + 1, left, (left + right) >> 1), query(a, b, (node << 1) + 2, (left + right) >> 1, right));
  }

  int find(int a, int b, Monoid val, int node, int left, int right) {
    if (dat[node] < val || right <= a || b <= left) return -1;
    if (right - left == 1) return node - (n - 1);
    int res_l = find(a, b, val, (node << 1) + 1, left, (left + right) >> 1);
    if (res_l != -1) return res_l;
    return find(a, b, val, (node << 1) + 2, (left + right) >> 1, right);
  }
};

int main() {
  int n; cin >> n;
  struct Skill {
    int a, x, b, query;
    Skill(int a, int b, int c, int query) : a(a), b(b), query(query) { x = b - c; }
    bool operator<(const Skill &r) {
      return a != r.a ? a < r.a : x != r.x ? x < r.x : b < r.b;
    };
  };
  vector<Skill> skills;
  REP(i, n) {
    int a, b, c; cin >> a >> b >> c;
    skills.emplace_back(a, b, c, i);
  }
  sort(ALL(skills));
  if (n <= 8) {
    // lie
    vector<vector<int> > graph(n);
    REP(i, n) {
      const Skill &src = skills[i];
      REP(j, n) {
        if (skills[j].a > src.x) break;
        if (i == j) continue;
        graph[i].emplace_back(j);
      }
    }
    vector<ll> ans(n);
    REP(i, n) {
      vector<ll> dist(n, -1);
      dist[i] = skills[i].b;
      priority_queue<pair<ll, int> > que;
      que.emplace(dist[i], i);
      vector<int> topo(n, INF);
      topo[i] = 0;
      bool inf = false;
      while (!inf && !que.empty()) {
        ll cost; int ver; tie(cost, ver) = que.top(); que.pop();
        if (cost < dist[ver]) continue;
        for (int e : graph[ver]) {
          if (topo[e] < topo[ver]) {
            inf = true;
            break;
          } else if (cost + skills[e].b > dist[e]) {
            chmin(topo[e], topo[ver] + 1);
            dist[e] = cost + skills[e].b;
            que.emplace(dist[e], e);
          }
        }
      }
      ans[skills[i].query] = (inf ? LINF : *max_element(ALL(dist)));
    }
    REP(i, n) {
      if (ans[i] == LINF) {
        cout << "BAN\n";
      } else {
        cout << ans[i] << '\n';
      }
    }
    return 0;
  }
  vector<int> gen(n);
  REP(i, n) gen[i] = skills[i].a;
  vector<int> idx(n);
  iota(ALL(idx), 0);
  sort(ALL(idx), [&](int l, int r) {
    const Skill &L = skills[l], &R = skills[r];
    return L.x != R.x ? L.x < R.x : L.a != R.a ? L.a < R.a : L.b < R.b;
  });
  RMQ<int> rmq(n, -INF);
  REP(i, n) rmq.update(i, skills[i].x);
  RMQ<ll> ans(n, -LINF);
  REP(i, n) ans.update(i, LINF);
  vector<bool> fixed(n, false);
  REP(i, n) {
    int now = idx[i];
    const Skill &skill = skills[now];
    int bor = upper_bound(ALL(gen), skill.x) - gen.begin();
    if (bor == 0) {
      ans.update(now, skill.b);
      rmq.update(now, -INF);
      fixed[now] = true;
    } else {
      ans.update(now, -LINF);
      rmq.update(now, -INF);
      ll mx = ans.query(0, bor);
      if (mx == LINF) {
        int r = upper_bound(ALL(gen), rmq.query(0, bor)) - gen.begin();
        if (now < r) fixed[now] = true;
        ans.update(now, LINF);
        rmq.update(now, skills[now].x);
      } else {
        ans.update(now, mx + skill.b);
        rmq.update(now, -INF);
        fixed[now] = true;
      }
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    int now = idx[i];
    if (fixed[now]) continue;
    const Skill &skill = skills[now];
    int bor = upper_bound(ALL(gen), skill.x) - gen.begin();
    assert(bor > 0);
    ans.update(now, -LINF);
    rmq.update(now, -INF);
    ll mx = ans.query(0, bor);
    if (mx == LINF) {
      int r = upper_bound(ALL(gen), rmq.query(0, bor)) - gen.begin();
      // assert(now < r);
      ans.update(now, LINF);
      rmq.update(now, skills[now].x);
    } else {
      ans.update(now, mx + skill.b);
      rmq.update(now, -INF);
      fixed[now] = true;
    }
  }
  REP(i, n) {
    int now = idx[i];
    if (fixed[now]) continue;
    const Skill &skill = skills[now];
    int bor = upper_bound(ALL(gen), skill.x) - gen.begin();
    assert(bor > 0);
    ans.update(now, -LINF);
    rmq.update(now, -INF);
    ll mx = ans.query(0, bor);
    if (mx == LINF) {
      int r = upper_bound(ALL(gen), rmq.query(0, bor)) - gen.begin();
      // assert(now < r);
      ans.update(now, LINF);
      rmq.update(now, skills[now].x);
    } else {
      ans.update(now, mx + skill.b);
      rmq.update(now, -INF);
      fixed[now] = true;
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    int now = idx[i];
    if (fixed[now]) continue;
    const Skill &skill = skills[now];
    int bor = upper_bound(ALL(gen), skill.x) - gen.begin();
    assert(bor > 0);
    ans.update(now, -LINF);
    rmq.update(now, -INF);
    ll mx = ans.query(0, bor);
    if (mx == LINF) {
      int r = upper_bound(ALL(gen), rmq.query(0, bor)) - gen.begin();
      // assert(now < r);
      ans.update(now, LINF);
      rmq.update(now, skills[now].x);
    } else {
      ans.update(now, mx + skill.b);
      rmq.update(now, -INF);
      fixed[now] = true;
    }
  }
  vector<ll> answer(n);
  REP(i, n) answer[skills[i].query] = ans[i];
  REP(i, n) {
    if (answer[i] == LINF) {
      cout << "BAN\n";
    } else {
      cout << answer[i] << '\n';
    }
  }
  return 0;
}
0