답안 #973760

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
973760 2024-05-02T10:38:29 Z Ooops_sorry 별자리 3 (JOI20_constellation3) C++14
100 / 100
1394 ms 96020 KB
#include<iostream>
#include<deque>
#include<algorithm>
#include<vector>
#include<map>
#include<random>
#include<time.h>
#include<cassert>
#include<chrono>
#include<set>
#include<unordered_set>
#include<array>
 
using namespace std;
 
#define ull unsigned long long
#define pb push_back
#define ll long long
#define all(a) a.begin(), a.end()
#define ld long double 
#define int long long 

mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

struct SegTree {
  vector<pair<int, int>> t;
  int n;
  SegTree(int n_) {
    n = 1;
    while (n < n_) {
      n *= 2;
    }
    t.resize(2 * n);
  }
  void upd(int i, pair<int, int> val) {
    i += n;
    t[i] = val;
    for (; i > 1; i /= 2) {
      t[i / 2] = max(t[i], t[i ^ 1]);
    } 
  }
  pair<int, int> get(int l, int r) {
    l += n, r += n + 1;
    pair<int, int> ans{-1, -1};
    while (l < r) {
      if (l & 1) {
        ans = max(ans, t[l++]);
      }
      if (r & 1) {
        ans = max(ans, t[--r]);
      }
      l /= 2, r /= 2;
    }
    return ans;
  }
};

const ll super_inf = 1e18;
const int N = 2e5 + 10;
vector<pair<int, ll>> mas[N];
int a[N];
SegTree tr(N);

ll t[4 * N], add[4 * N];

void inc(int v, ll val) {
  t[v] += val;
  add[v] += val;
}

void push(int v) {
  inc(v * 2, add[v]);
  inc(v * 2 + 1, add[v]);
  add[v] = 0;
}

void update_pos(int v, int l, int r, int pos, ll val) {
  if (l == r) {
    if (val == -super_inf) {
      t[v] = val;
    } else {
      t[v] = max(t[v], val);
    }
    return;
  }
  push(v);
  int m = (l + r) / 2;
  if (pos <= m) {
    update_pos(2 * v, l, m, pos, val);
  } else {
    update_pos(2 * v + 1, m + 1, r, pos, val);
  }
  t[v] = max(t[v * 2], t[v * 2 + 1]);
}

void update(int v, int tl, int tr, int l, int r, ll val) {
  if (l > r) return;
  if (tl == l && tr == r) {
    inc(v, val);
    return;
  }
  push(v);
  int tm = (tl + tr) / 2;
  update(2 * v, tl, tm, l, min(r, tm), val);
  update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, val);
  t[v] = max(t[v * 2], t[v * 2 + 1]);
}

ll get(int v, int tl, int tr, int l, int r) {
  if (l > r) return -super_inf;
  if (tl == l && tr == r) {
    return t[v];
  }
  push(v);
  int tm = (tl + tr) / 2;
  return max(get(2 * v, tl, tm, l, min(r, tm)), get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
}

int pr[N];
set<int> sets[N];

int solve(int l, int r) {
  if (pr[r + 1] - pr[l] == 0) return N - 1;
  int m = tr.get(l, r).second;
  if (mas[m].size() == pr[r + 1] - pr[l]) {
    for (auto p : mas[m]) {
      sets[l].insert(p.first);
      update_pos(1, 0, N - 1, p.first, p.second);
    }
    return l;
  }
  int sz1 = pr[m] - pr[l], sz2 = pr[r + 1] - pr[m + 1], i, j;
  vector<array<ll, 3>> lol;
  if (sz1 > sz2) {
    j = solve(m + 1, r);
    for (int p : sets[j]) {
      lol.pb({p, get(1, 0, N - 1, p, p), 0});
      update_pos(1, 0, N - 1, p, -super_inf);
    }
    assert(t[1] < 0);
    i = solve(l, m - 1);
  } else {
    j = solve(l, m - 1);
    for (int p : sets[j]) {
      lol.pb({p, get(1, 0, N - 1, p, p), 0});
      update_pos(1, 0, N - 1, p, -super_inf);
    }
    assert(t[1] < 0);
    i = solve(m + 1, r);
  }
  assert(sets[i].size() > 0);
  ll best2 = max(0ll, get(1, 0, N - 1, 0, a[m])), best1 = 0;
  if (j != N - 1) {
    assert(sets[j].size() > 0);
    for (int p : sets[j]) {
      sets[i].insert(p);
    }
    for (int i = 0; i < lol.size(); i++) {
      if (lol[i][0] <= a[m]) {
        lol[i][2] = max(0ll, get(1, 0, N - 1, 0, lol[i][0]));
      }
    }
    ll cur_mx = 0;
    int last = -1;
    for (int i = 0; i < lol.size(); i++) {
      int R = min(a[m], (i + 1 < lol.size() ? lol[i + 1][0] - 1 : N - 1));
      cur_mx = max(cur_mx, lol[i][1]);
      if (lol[i][0] <= a[m]) {
        best1 = max(best1, lol[i][1]);
      }
      assert(last < lol[i][0]);
      update(1, 0, N - 1, lol[i][0], R, cur_mx);
      last = R;
    }
    assert(last < a[m] + 1);
    update(1, 0, N - 1, a[m] + 1, N - 1, best1);
    for (int i = 0; i < lol.size(); i++) {
      if (lol[i][0] <= a[m]) {
        update_pos(1, 0, N - 1, lol[i][0], lol[i][2] + lol[i][1]);
      } else {
        update_pos(1, 0, N - 1, lol[i][0], best2 + lol[i][1]);
      }
    }
  }
  for (auto p : mas[m]) {
    update_pos(1, 0, N - 1, p.first, p.second + best1 + best2);
    sets[i].insert(p.first);
  }
  return i;
}

signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif // LOCAL
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  for (int i = 0; i < 4 * N; i++) {
    t[i] = -super_inf;
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    tr.upd(i, {a[i], i});
  }
  int m;
  cin >> m;
  ll sum = 0;
  for (int i = 0; i < m; i++) {
    int x, y, c;
    cin >> x >> y >> c;
    x--;
    if (y <= a[x]) continue;
    sum += c;
    mas[x].pb({y, c});
  }
  for (int i = 0; i < n; i++) {
    pr[i + 1] = pr[i] + mas[i].size();
  }
  solve(0, n - 1);
  ll mx = get(1, 0, N - 1, 0, N - 1);
  cout << sum - mx << endl;
  return 0; 
}

Compilation message

constellation3.cpp: In function 'long long int solve(long long int, long long int)':
constellation3.cpp:125:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  125 |   if (mas[m].size() == pr[r + 1] - pr[l]) {
      |       ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
constellation3.cpp:158:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |     for (int i = 0; i < lol.size(); i++) {
      |                     ~~^~~~~~~~~~~~
constellation3.cpp:165:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |     for (int i = 0; i < lol.size(); i++) {
      |                     ~~^~~~~~~~~~~~
constellation3.cpp:166:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |       int R = min(a[m], (i + 1 < lol.size() ? lol[i + 1][0] - 1 : N - 1));
      |                          ~~~~~~^~~~~~~~~~~~
constellation3.cpp:177:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  177 |     for (int i = 0; i < lol.size(); i++) {
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 34396 KB Output is correct
2 Correct 7 ms 34392 KB Output is correct
3 Correct 8 ms 34396 KB Output is correct
4 Correct 8 ms 34412 KB Output is correct
5 Correct 9 ms 34396 KB Output is correct
6 Correct 8 ms 34268 KB Output is correct
7 Correct 8 ms 34396 KB Output is correct
8 Correct 8 ms 34396 KB Output is correct
9 Correct 7 ms 34396 KB Output is correct
10 Correct 10 ms 34396 KB Output is correct
11 Correct 9 ms 34396 KB Output is correct
12 Correct 7 ms 34312 KB Output is correct
13 Correct 8 ms 34392 KB Output is correct
14 Correct 8 ms 34396 KB Output is correct
15 Correct 8 ms 34396 KB Output is correct
16 Correct 8 ms 34396 KB Output is correct
17 Correct 8 ms 34236 KB Output is correct
18 Correct 9 ms 34396 KB Output is correct
19 Correct 7 ms 34136 KB Output is correct
20 Correct 9 ms 34140 KB Output is correct
21 Correct 7 ms 34396 KB Output is correct
22 Correct 8 ms 34232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 34396 KB Output is correct
2 Correct 7 ms 34392 KB Output is correct
3 Correct 8 ms 34396 KB Output is correct
4 Correct 8 ms 34412 KB Output is correct
5 Correct 9 ms 34396 KB Output is correct
6 Correct 8 ms 34268 KB Output is correct
7 Correct 8 ms 34396 KB Output is correct
8 Correct 8 ms 34396 KB Output is correct
9 Correct 7 ms 34396 KB Output is correct
10 Correct 10 ms 34396 KB Output is correct
11 Correct 9 ms 34396 KB Output is correct
12 Correct 7 ms 34312 KB Output is correct
13 Correct 8 ms 34392 KB Output is correct
14 Correct 8 ms 34396 KB Output is correct
15 Correct 8 ms 34396 KB Output is correct
16 Correct 8 ms 34396 KB Output is correct
17 Correct 8 ms 34236 KB Output is correct
18 Correct 9 ms 34396 KB Output is correct
19 Correct 7 ms 34136 KB Output is correct
20 Correct 9 ms 34140 KB Output is correct
21 Correct 7 ms 34396 KB Output is correct
22 Correct 8 ms 34232 KB Output is correct
23 Correct 16 ms 34652 KB Output is correct
24 Correct 16 ms 34652 KB Output is correct
25 Correct 15 ms 34808 KB Output is correct
26 Correct 15 ms 34904 KB Output is correct
27 Correct 13 ms 34652 KB Output is correct
28 Correct 14 ms 34772 KB Output is correct
29 Correct 15 ms 34648 KB Output is correct
30 Correct 14 ms 34652 KB Output is correct
31 Correct 14 ms 34652 KB Output is correct
32 Correct 10 ms 34520 KB Output is correct
33 Correct 9 ms 34652 KB Output is correct
34 Correct 10 ms 34652 KB Output is correct
35 Correct 12 ms 34648 KB Output is correct
36 Correct 10 ms 34652 KB Output is correct
37 Correct 9 ms 34652 KB Output is correct
38 Correct 9 ms 34828 KB Output is correct
39 Correct 12 ms 34396 KB Output is correct
40 Correct 11 ms 34652 KB Output is correct
41 Correct 12 ms 34396 KB Output is correct
42 Correct 10 ms 34396 KB Output is correct
43 Correct 12 ms 34648 KB Output is correct
44 Correct 12 ms 34396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 34396 KB Output is correct
2 Correct 7 ms 34392 KB Output is correct
3 Correct 8 ms 34396 KB Output is correct
4 Correct 8 ms 34412 KB Output is correct
5 Correct 9 ms 34396 KB Output is correct
6 Correct 8 ms 34268 KB Output is correct
7 Correct 8 ms 34396 KB Output is correct
8 Correct 8 ms 34396 KB Output is correct
9 Correct 7 ms 34396 KB Output is correct
10 Correct 10 ms 34396 KB Output is correct
11 Correct 9 ms 34396 KB Output is correct
12 Correct 7 ms 34312 KB Output is correct
13 Correct 8 ms 34392 KB Output is correct
14 Correct 8 ms 34396 KB Output is correct
15 Correct 8 ms 34396 KB Output is correct
16 Correct 8 ms 34396 KB Output is correct
17 Correct 8 ms 34236 KB Output is correct
18 Correct 9 ms 34396 KB Output is correct
19 Correct 7 ms 34136 KB Output is correct
20 Correct 9 ms 34140 KB Output is correct
21 Correct 7 ms 34396 KB Output is correct
22 Correct 8 ms 34232 KB Output is correct
23 Correct 16 ms 34652 KB Output is correct
24 Correct 16 ms 34652 KB Output is correct
25 Correct 15 ms 34808 KB Output is correct
26 Correct 15 ms 34904 KB Output is correct
27 Correct 13 ms 34652 KB Output is correct
28 Correct 14 ms 34772 KB Output is correct
29 Correct 15 ms 34648 KB Output is correct
30 Correct 14 ms 34652 KB Output is correct
31 Correct 14 ms 34652 KB Output is correct
32 Correct 10 ms 34520 KB Output is correct
33 Correct 9 ms 34652 KB Output is correct
34 Correct 10 ms 34652 KB Output is correct
35 Correct 12 ms 34648 KB Output is correct
36 Correct 10 ms 34652 KB Output is correct
37 Correct 9 ms 34652 KB Output is correct
38 Correct 9 ms 34828 KB Output is correct
39 Correct 12 ms 34396 KB Output is correct
40 Correct 11 ms 34652 KB Output is correct
41 Correct 12 ms 34396 KB Output is correct
42 Correct 10 ms 34396 KB Output is correct
43 Correct 12 ms 34648 KB Output is correct
44 Correct 12 ms 34396 KB Output is correct
45 Correct 1306 ms 95184 KB Output is correct
46 Correct 1253 ms 93932 KB Output is correct
47 Correct 1394 ms 95864 KB Output is correct
48 Correct 1240 ms 93256 KB Output is correct
49 Correct 1264 ms 94228 KB Output is correct
50 Correct 1256 ms 93528 KB Output is correct
51 Correct 1305 ms 94600 KB Output is correct
52 Correct 1315 ms 96020 KB Output is correct
53 Correct 1217 ms 92864 KB Output is correct
54 Correct 333 ms 74900 KB Output is correct
55 Correct 396 ms 76336 KB Output is correct
56 Correct 415 ms 74064 KB Output is correct
57 Correct 397 ms 72684 KB Output is correct
58 Correct 321 ms 74792 KB Output is correct
59 Correct 325 ms 75120 KB Output is correct
60 Correct 193 ms 86608 KB Output is correct
61 Correct 308 ms 58356 KB Output is correct
62 Correct 334 ms 81104 KB Output is correct
63 Correct 297 ms 56744 KB Output is correct
64 Correct 296 ms 58000 KB Output is correct
65 Correct 295 ms 81264 KB Output is correct
66 Correct 274 ms 57132 KB Output is correct