제출 #629839

#제출 시각아이디문제언어결과실행 시간메모리
629839abeker송신탑 (IOI22_towers)C++17
23 / 100
4088 ms12624 KiB
#include <bits/stdc++.h>
#include "towers.h"
using namespace std;

const int INF = 2e9;

struct Node {
  int mini, arg_max;
};

class Tournament {
  int n, offset;
  vector <int> vals;
  vector <Node> tour;
public:
  Node merge(Node lhs, Node rhs) const {
    return {min(lhs.mini, rhs.mini), vals[lhs.arg_max] > vals[rhs.arg_max] ? lhs.arg_max : rhs.arg_max};
  }
  Tournament(int n, vector <int> _vals) : n(n), vals(_vals) {
    vals.push_back(0);
    for (offset = 1; offset <= n; offset *= 2);
    tour.resize(2 * offset);
    vals.resize(offset);
    for (int i = 0; i < offset; i++)
      tour[i + offset] = {vals[i], i};
    for (int i = offset - 1; i; i--)
      tour[i] = merge(tour[2 * i], tour[2 * i + 1]);
  }
  Tournament() {}
  Node query(int x, int lo, int hi, int from, int to) const {
    if (lo >= to || hi <= from)
      return {INF, n};
    if (lo >= from && hi <= to)
      return tour[x];
    int mid = (lo + hi) / 2;
    return merge(query(2 * x, lo, mid, from, to), query(2 * x + 1, mid, hi, from, to));
  }
  Node query(int from, int to) const {
    return query(1, 0, offset, from, to);
  }
  int get(int x) const {
    return vals[x];
  }
};

Tournament solver;

void init(int N, vector <int> h) {
  solver = Tournament(N, h);
}

int solve(int lo, int hi, int ub, int diff) {
  if (lo >= hi)
    return 0;
  Node tmp = solver.query(lo, hi);
  if (tmp.mini > ub)
    return 0;
  int mid = tmp.arg_max;
  int maks = solver.get(mid);
  return max(solve(lo, mid, maks - diff, diff) + solve(mid + 1, hi, maks - diff, diff), 1);
}

int max_towers(int l, int r, int d) {
  return solve(l, r + 1, 2e9, d);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...