Submission #684873

# Submission time Handle Problem Language Result Execution time Memory
684873 2023-01-22T18:03:24 Z peijar Constellation 3 (JOI20_constellation3) C++17
100 / 100
263 ms 129188 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
  bool first = true;
  string res = "[";
  for (const auto &x : v) {
    if (!first)
      res += ", ";
    first = false;
    res += to_string(x);
  }
  res += "]";
  return res;
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cout << ' ' << to_string(H);
  dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

template <class T> struct RMQ {
  vector<vector<T>> jmp;
  RMQ(const vector<T> &V) : jmp(1, V) {
    for (int pw = 1, k = 1; pw * 2 <= (int)V.size(); pw *= 2, ++k) {
      jmp.emplace_back((int)V.size() - pw * 2 + 1);
      for (int j = 0; j < (int)jmp[k].size(); ++j)
        jmp[k][j] = max(jmp[k - 1][j], jmp[k - 1][j + pw]);
    }
  }
  T query(int a, int b) { // [a, b)
    assert(a < b);
    int dep = 31 - __builtin_clz(b - a);
    return max(jmp[dep][a], jmp[dep][b - (1 << dep)]);
  }
};

template <class T> class Fenwick {
public:
  int lim;
  vector<T> bit;

  Fenwick(int n) : lim(n + 1), bit(lim) {}

  void upd(int pos, T val) {
    for (pos++; pos < lim; pos += pos & -pos)
      bit[pos] += val;
  }

  T sum(int r) { // < r
    T ret = 0;
    for (; r; r -= r & -r)
      ret += bit[r];
    return ret;
  }

  T sum(int l, int r) { // [l, r)
    return sum(r) - sum(l);
  }

  void rangeAdd(int deb, int fin, int x) { // [deb, fin)
    dbg(deb, fin, x);
    upd(deb, x);
    upd(fin, -x);
  }

  int point(int pos) { return sum(pos + 1); }
};

template <class T> using min_pq = priority_queue<T, vector<T>, greater<T>>;

vector<vector<pair<int, int>>> onPos;
RMQ<pair<int, int>> rmq({});
Fenwick<int> fen(0);

pair<min_pq<tuple<int, int, int>>, int> dfs(int deb, int fin, int prevHeight) {
  auto [valMax, posMax] = rmq.query(deb, fin);
  min_pq<tuple<int, int, int>> toProcess;
  for (auto [y, c] : onPos[posMax])
    toProcess.emplace(y, posMax, c);

  int solFree = 0;
  int addL = 0, addR = 0;
  if (deb < posMax) {
    auto [toAdd, freeL] = dfs(deb, posMax, valMax);
    if (toAdd.size() > toProcess.size())
      toAdd.swap(toProcess);
    while (!toAdd.empty()) {
      toProcess.emplace(toAdd.top());
      toAdd.pop();
    }
    addL = freeL;
    solFree += freeL;
  }
  if (posMax + 1 < fin) {
    auto [toAdd, freeR] = dfs(posMax + 1, fin, valMax);
    if (toAdd.size() > toProcess.size())
      toAdd.swap(toProcess);
    while (!toAdd.empty()) {
      toProcess.emplace(toAdd.top());
      toAdd.pop();
    }
    addR = freeR;
    solFree += freeR;
  }
  fen.rangeAdd(posMax, fin, addL);
  fen.rangeAdd(deb, posMax + 1, addR);

  while (!toProcess.empty() and get<0>(toProcess.top()) <= prevHeight) {
    auto [y, x, c] = toProcess.top();
    toProcess.pop();
    int gain = c + fen.point(x);
    dbg(x, y, gain, fen.point(x));
    solFree = max(solFree, gain);
  }
  dbg(deb, fin, solFree);
  return pair(move(toProcess), solFree);
}

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> height(N);
  for (int &x : height)
    cin >> x;
  vector<pair<int, int>> toRMQ;
  for (int i = 0; i < N; ++i)
    toRMQ.emplace_back(height[i], i);
  onPos.resize(N);
  rmq = RMQ(toRMQ);

  int nbEtoiles;
  cin >> nbEtoiles;
  int totCost = 0;
  for (int i = 0; i < nbEtoiles; ++i) {
    int x, y, c;
    cin >> x >> y >> c;
    totCost += c;
    onPos[x - 1].emplace_back(y, c);
  }
  fen = Fenwick<int>(N);

  cout << totCost - dfs(0, N, 1e18).second << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 444 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 452 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 444 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 452 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 716 KB Output is correct
24 Correct 2 ms 720 KB Output is correct
25 Correct 2 ms 708 KB Output is correct
26 Correct 2 ms 796 KB Output is correct
27 Correct 2 ms 724 KB Output is correct
28 Correct 2 ms 724 KB Output is correct
29 Correct 2 ms 724 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
31 Correct 2 ms 724 KB Output is correct
32 Correct 2 ms 1236 KB Output is correct
33 Correct 2 ms 1108 KB Output is correct
34 Correct 2 ms 1108 KB Output is correct
35 Correct 2 ms 980 KB Output is correct
36 Correct 2 ms 1148 KB Output is correct
37 Correct 2 ms 1108 KB Output is correct
38 Correct 2 ms 1364 KB Output is correct
39 Correct 2 ms 852 KB Output is correct
40 Correct 2 ms 1236 KB Output is correct
41 Correct 2 ms 852 KB Output is correct
42 Correct 2 ms 816 KB Output is correct
43 Correct 3 ms 1236 KB Output is correct
44 Correct 2 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 444 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 452 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 716 KB Output is correct
24 Correct 2 ms 720 KB Output is correct
25 Correct 2 ms 708 KB Output is correct
26 Correct 2 ms 796 KB Output is correct
27 Correct 2 ms 724 KB Output is correct
28 Correct 2 ms 724 KB Output is correct
29 Correct 2 ms 724 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
31 Correct 2 ms 724 KB Output is correct
32 Correct 2 ms 1236 KB Output is correct
33 Correct 2 ms 1108 KB Output is correct
34 Correct 2 ms 1108 KB Output is correct
35 Correct 2 ms 980 KB Output is correct
36 Correct 2 ms 1148 KB Output is correct
37 Correct 2 ms 1108 KB Output is correct
38 Correct 2 ms 1364 KB Output is correct
39 Correct 2 ms 852 KB Output is correct
40 Correct 2 ms 1236 KB Output is correct
41 Correct 2 ms 852 KB Output is correct
42 Correct 2 ms 816 KB Output is correct
43 Correct 3 ms 1236 KB Output is correct
44 Correct 2 ms 844 KB Output is correct
45 Correct 184 ms 68628 KB Output is correct
46 Correct 178 ms 67896 KB Output is correct
47 Correct 192 ms 66688 KB Output is correct
48 Correct 173 ms 68668 KB Output is correct
49 Correct 175 ms 66204 KB Output is correct
50 Correct 207 ms 66316 KB Output is correct
51 Correct 173 ms 66456 KB Output is correct
52 Correct 179 ms 67924 KB Output is correct
53 Correct 196 ms 67956 KB Output is correct
54 Correct 244 ms 112060 KB Output is correct
55 Correct 263 ms 106200 KB Output is correct
56 Correct 228 ms 100360 KB Output is correct
57 Correct 224 ms 96048 KB Output is correct
58 Correct 230 ms 105624 KB Output is correct
59 Correct 223 ms 105656 KB Output is correct
60 Correct 159 ms 129188 KB Output is correct
61 Correct 235 ms 75908 KB Output is correct
62 Correct 240 ms 119796 KB Output is correct
63 Correct 216 ms 74456 KB Output is correct
64 Correct 226 ms 72420 KB Output is correct
65 Correct 228 ms 120856 KB Output is correct
66 Correct 222 ms 74640 KB Output is correct