답안 #964277

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
964277 2024-04-16T14:23:00 Z rolandpetrean 별자리 3 (JOI20_constellation3) C++17
100 / 100
545 ms 103500 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define int ll // srry

#define endl '\n'
#define pb push_back
using pi = array<int, 2>;

// i want to maximize costs of full set, remove from total
// max(a[i..j]) < min(s[i].y, s[j].y)
/*
cartesian tree on maximums of a
consider i
dp[i] = dp[parent]

consider some star with x = i
if i pick this star, then i can't pick any star in my subtree with y > a[i]
maximums of ancestors are increasing

~~let p be the first ancestor with a[p] >= y~~
~~dp[i] = max(dp[i], max_to_root[p] + s.cost)~~

let p be the last ancestor with a[p] < y
i have like chains from x to p, each has a cost
i need to pick disjoint set of chains with max cost
each node of the tree has <=2 children
*/

const int LG = 19;

struct BIT {
  int n;
  vector<int> bit;
  
  void update(int i, int x) {
    for (; i <= n; i += (i & -i)) bit[i] += x;
  }
  void update(int st, int dr, int x) {
    update(st, x);
    update(dr + 1, -x);
  }
  int query(int i) {
    int ret = 0;
    for (; i > 0; i -= (i & -i)) ret += bit[i];
    return ret;
  }
  
  BIT(int n) {
    this->n = n;
    bit.assign(n + 1, 0);
  }
};

struct Star {
  int x, y, cost;
};

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int n;
  cin >> n;
  
  vector<int> a(n + 1);
  for (int i = 1; i <= n; ++i) cin >> a[i];
  a[0] = n + 1;
  
  int m;
  cin >> m;
  
  int total_cost = 0;
  vector<Star> stars(m);
  vector<vector<int>> stars_x(n + 1);
  for (int i = 0; i < m; ++i) {
    cin >> stars[i].x >> stars[i].y >> stars[i].cost;
    total_cost += stars[i].cost;
    stars_x[stars[i].x].pb(i);
  }
  
  vector<int> parent(n + 1);
  stack<int> st;
  for (int i = 1; i <= n; ++i) {
    while (!st.empty() && a[st.top()] < a[i]) st.pop();
    if (!st.empty()) parent[i] = st.top();
    st.push(i);
  }
  while (!st.empty()) st.pop();
  for (int i = n; i >= 1; --i) {
    while (!st.empty() && a[st.top()] <= a[i]) st.pop();
    if (!st.empty()) {
      if (a[st.top()] <= a[parent[i]]) parent[i] = st.top();
    }
    st.push(i);
  }
  
  vector<vector<int>> children(n + 1);
  for (int i = 1; i <= n; ++i) {
    children[parent[i]].pb(i);
  }
  
  BIT bit(2 * n + 5);
  
  int timer = 0;
  vector<vector<int>> chains(n + 1);
  vector<int> tin(n + 1), tout(n + 1);
  vector<vector<int>> up(n + 1, vector<int>(LG));
  function<void(int)> dfs = [&](int x) {
    tin[x] = ++timer;
    
    up[x][0] = parent[x];
    for (int h = 1; h < LG; ++h) {
      up[x][h] = up[up[x][h - 1]][h - 1];
    }
    
    for (int u : children[x]) dfs(u);
    
    for (int idx : stars_x[x]) {
      int p = x;
      for (int jmp = LG - 1; jmp >= 0; --jmp) {
        if (a[up[p][jmp]] < stars[idx].y) p = up[p][jmp];
      }
      
      chains[p].pb(idx);
    }
    
    tout[x] = ++timer;
  };
  dfs(0);
  
  // adj_dp - dp[i]
  vector<int> dp(n + 1);
  function<void(int)> dfs2 = [&](int x) {
    //cout << "IN " << x << endl;
    
    int adj_dp = 0;
    for (int u : children[x]) {
      dfs2(u);
      adj_dp += dp[u];
    }
    dp[x] = adj_dp;
    
    bit.update(tin[x], tout[x], adj_dp);
    
    for (int idx : chains[x]) {
      const Star& s = stars[idx];
      int cx = s.x;
      
      int cost = s.cost;
      cost += bit.query(tin[cx]);
      dp[x] = max(dp[x], cost);
      
      //cout << "CHAIN " << x << " -> " << cx << " | " << cost << endl;
    }
    
    bit.update(tin[x], tout[x], -dp[x]);
    
    //cout << "OUT " << x << " | " << dp[x] << endl;
  };
  dfs2(0);
  
  int max_cost = dp[0];
  int ans = total_cost - max_cost;
  cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 352 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 456 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 352 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 456 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 3 ms 1112 KB Output is correct
24 Correct 3 ms 1132 KB Output is correct
25 Correct 2 ms 1116 KB Output is correct
26 Correct 2 ms 1116 KB Output is correct
27 Correct 2 ms 1116 KB Output is correct
28 Correct 2 ms 1116 KB Output is correct
29 Correct 3 ms 1116 KB Output is correct
30 Correct 2 ms 1116 KB Output is correct
31 Correct 2 ms 1404 KB Output is correct
32 Correct 2 ms 1116 KB Output is correct
33 Correct 2 ms 1372 KB Output is correct
34 Correct 2 ms 1116 KB Output is correct
35 Correct 2 ms 1140 KB Output is correct
36 Correct 2 ms 1372 KB Output is correct
37 Correct 2 ms 1368 KB Output is correct
38 Correct 2 ms 1372 KB Output is correct
39 Correct 2 ms 1116 KB Output is correct
40 Correct 2 ms 1372 KB Output is correct
41 Correct 3 ms 1116 KB Output is correct
42 Correct 2 ms 1116 KB Output is correct
43 Correct 2 ms 1372 KB Output is correct
44 Correct 2 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 352 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 456 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 3 ms 1112 KB Output is correct
24 Correct 3 ms 1132 KB Output is correct
25 Correct 2 ms 1116 KB Output is correct
26 Correct 2 ms 1116 KB Output is correct
27 Correct 2 ms 1116 KB Output is correct
28 Correct 2 ms 1116 KB Output is correct
29 Correct 3 ms 1116 KB Output is correct
30 Correct 2 ms 1116 KB Output is correct
31 Correct 2 ms 1404 KB Output is correct
32 Correct 2 ms 1116 KB Output is correct
33 Correct 2 ms 1372 KB Output is correct
34 Correct 2 ms 1116 KB Output is correct
35 Correct 2 ms 1140 KB Output is correct
36 Correct 2 ms 1372 KB Output is correct
37 Correct 2 ms 1368 KB Output is correct
38 Correct 2 ms 1372 KB Output is correct
39 Correct 2 ms 1116 KB Output is correct
40 Correct 2 ms 1372 KB Output is correct
41 Correct 3 ms 1116 KB Output is correct
42 Correct 2 ms 1116 KB Output is correct
43 Correct 2 ms 1372 KB Output is correct
44 Correct 2 ms 1116 KB Output is correct
45 Correct 226 ms 82928 KB Output is correct
46 Correct 249 ms 82000 KB Output is correct
47 Correct 235 ms 81484 KB Output is correct
48 Correct 220 ms 82772 KB Output is correct
49 Correct 240 ms 80500 KB Output is correct
50 Correct 208 ms 80424 KB Output is correct
51 Correct 233 ms 80768 KB Output is correct
52 Correct 224 ms 82536 KB Output is correct
53 Correct 237 ms 82356 KB Output is correct
54 Correct 505 ms 95800 KB Output is correct
55 Correct 441 ms 90440 KB Output is correct
56 Correct 463 ms 87596 KB Output is correct
57 Correct 407 ms 86096 KB Output is correct
58 Correct 329 ms 91084 KB Output is correct
59 Correct 273 ms 90280 KB Output is correct
60 Correct 186 ms 103500 KB Output is correct
61 Correct 220 ms 82340 KB Output is correct
62 Correct 545 ms 96468 KB Output is correct
63 Correct 227 ms 81040 KB Output is correct
64 Correct 213 ms 79580 KB Output is correct
65 Correct 495 ms 97240 KB Output is correct
66 Correct 228 ms 80648 KB Output is correct