Submission #867620

# Submission time Handle Problem Language Result Execution time Memory
867620 2023-10-29T02:23:05 Z NeroZein Constellation 3 (JOI20_constellation3) C++17
100 / 100
318 ms 60436 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int N = 2e5 + 5; 

int a[N]; 
int x[N], y[N], c[N]; 

int leaf[N]; 
int L[N], R[N]; 
long long dp[N];
int prop[N * 2];
vector<int> g[N];
long long tree[N];
vector<int> in_col[N]; 
int cnt, in[N], out[N];
vector<pair<int, int>> vec[N]; 
map<pair<int, int>, vector<pair<int, int>>> mp; 

int search(vector<int>& mono, int v) {
  int lo = 0, hi = (int) mono.size() - 1;
  while (lo < hi) {
    int mid = (lo + hi + 1) / 2; 
    if (a[mono[mid]] >= v) lo = mid;
    else hi = mid - 1; 
  }
  return mono[lo]; 
}

void assign(int nd, int l, int r, int s, int e, int v) {
  if (l >= s && r <= e) {
    prop[nd] = min(prop[nd], v); 
    return; 
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  if (mid >= e) {
    assign(nd + 1, l, mid, s, e, v); 
  } else {
    if (mid < s) {
      assign(rs, mid + 1, r, s, e, v); 
    } else {      
      assign(nd + 1, l, mid, s, e, v); 
      assign(rs, mid + 1, r, s, e, v); 
    }
  }
}

int get(int nd, int l, int r, int p) {
  int ret = prop[nd];
  if (l == r) {
    return ret;
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  if (p <= mid) {
    return min(ret, get(nd + 1, l, mid, p)); 
  } else {
    return min(ret, get(rs, mid + 1, r, p)); 
  }
}

void pre(int v) {
  in[v] = cnt++; 
  for (int u : g[v]) {
    pre(u); 
  }
  //deb(v) deb(in[v]) cout << '\n';
  out[v] = cnt - 1; 
}

void upd(int i, long long v) {
  ++i;
  while (i < N) {
    tree[i] += v; 
    i += i & -i; 
  }
}

long long qry(int i) {
  ++i;
  long long ret = 0;
  while (i) {
    ret += tree[i];
    i -= i & -i; 
  }
  return ret;
}

void dfs(int v) {
  for (int u : g[v]) {
    dfs(u);
    dp[v] += dp[u]; 
  }
  for (int u : g[v]) {
    upd(in[v], dp[u]); upd(out[v] + 1, -dp[u]);
    upd(in[u], -dp[u]); upd(out[u] + 1, dp[u]); 
  }
  for (auto [val, col] : vec[v]) {
    int pos = in[leaf[col]]; 
    dp[v] = max(dp[v], val + qry(pos)); 
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i]; 
  }
  int m;
  cin >> m;
  long long ans = 0;
  for (int i = 1; i <= m; ++i) {
    cin >> x[i] >> y[i] >> c[i];
    in_col[x[i]].push_back(i); 
    ans += c[i]; 
  }
  vector<int> stk;
  stk.push_back(0); 
  a[0] = INT_MAX; 
  for (int i = 1; i <= n; ++i) {
    while (a[stk.back()] < a[i]) {
      stk.pop_back(); 
    }
    for (int j : in_col[i]) {
      L[j] = search(stk, y[j]) + 1; 
    }
    stk.push_back(i); 
  }
  stk.resize(1);
  stk[0] = n + 1; 
  a[n + 1] = INT_MAX;
  for (int i = n; i >= 1; --i) {
    while (a[stk.back()] < a[i]) {
      stk.pop_back();
    }
    for (int j : in_col[i]) {
      R[j] = search(stk, y[j]) - 1; 
    }
    stk.push_back(i); 
  }
  for (int i = 1; i <= m; ++i) {
    mp[make_pair(L[i], R[i])].emplace_back(c[i], x[i]); 
  }
  vector<pair<int, int>> ranges;
  for (auto i : mp) {
    ranges.push_back(i.first); 
  }
  sort(ranges.begin(), ranges.end(), [&](pair<int, int> i, pair<int, int> j) {
    return i.second - i.first < j.second - j.first; 
  });
  set<pair<int, int>> active;
  memset(prop, 63, sizeof prop); 
  for (int i = 0; i < (int) ranges.size(); ++i) {
    while (true) {
      auto it = active.lower_bound(make_pair(ranges[i].first, -1));
      if (it != active.end() && it -> first <= ranges[i].second) {
        g[i].push_back(it -> second);
        active.erase(it);
      } else {
        break; 
      }
    }
    vec[i] = mp[ranges[i]]; 
    active.emplace(ranges[i].first, i); 
    assign(0, 1, n, ranges[i].first, ranges[i].second, i); 
  }
  for (auto it : active) {
    g[n].push_back(it.second); 
  }
  assign(0, 1, n, 1, n, n); 
  for (int i = 1; i <= n; ++i) {
    leaf[i] = get(0, 1, n, i);
  }
  pre(n); 
  dfs(n);
  ans -= dp[n];
  cout << ans << '\n'; 
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 25180 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 5 ms 25180 KB Output is correct
4 Correct 5 ms 25432 KB Output is correct
5 Correct 5 ms 25248 KB Output is correct
6 Correct 5 ms 25180 KB Output is correct
7 Correct 5 ms 25296 KB Output is correct
8 Correct 5 ms 25180 KB Output is correct
9 Correct 5 ms 25180 KB Output is correct
10 Correct 5 ms 25180 KB Output is correct
11 Correct 6 ms 25180 KB Output is correct
12 Correct 5 ms 25180 KB Output is correct
13 Correct 5 ms 25292 KB Output is correct
14 Correct 5 ms 25180 KB Output is correct
15 Correct 4 ms 25180 KB Output is correct
16 Correct 5 ms 25180 KB Output is correct
17 Correct 5 ms 25180 KB Output is correct
18 Correct 5 ms 25272 KB Output is correct
19 Correct 5 ms 25180 KB Output is correct
20 Correct 5 ms 25180 KB Output is correct
21 Correct 5 ms 25500 KB Output is correct
22 Correct 5 ms 25180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 25180 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 5 ms 25180 KB Output is correct
4 Correct 5 ms 25432 KB Output is correct
5 Correct 5 ms 25248 KB Output is correct
6 Correct 5 ms 25180 KB Output is correct
7 Correct 5 ms 25296 KB Output is correct
8 Correct 5 ms 25180 KB Output is correct
9 Correct 5 ms 25180 KB Output is correct
10 Correct 5 ms 25180 KB Output is correct
11 Correct 6 ms 25180 KB Output is correct
12 Correct 5 ms 25180 KB Output is correct
13 Correct 5 ms 25292 KB Output is correct
14 Correct 5 ms 25180 KB Output is correct
15 Correct 4 ms 25180 KB Output is correct
16 Correct 5 ms 25180 KB Output is correct
17 Correct 5 ms 25180 KB Output is correct
18 Correct 5 ms 25272 KB Output is correct
19 Correct 5 ms 25180 KB Output is correct
20 Correct 5 ms 25180 KB Output is correct
21 Correct 5 ms 25500 KB Output is correct
22 Correct 5 ms 25180 KB Output is correct
23 Correct 6 ms 25432 KB Output is correct
24 Correct 6 ms 25436 KB Output is correct
25 Correct 6 ms 25316 KB Output is correct
26 Correct 6 ms 25432 KB Output is correct
27 Correct 6 ms 25436 KB Output is correct
28 Correct 6 ms 25436 KB Output is correct
29 Correct 6 ms 25436 KB Output is correct
30 Correct 6 ms 25324 KB Output is correct
31 Correct 6 ms 25432 KB Output is correct
32 Correct 6 ms 25436 KB Output is correct
33 Correct 8 ms 25436 KB Output is correct
34 Correct 6 ms 25508 KB Output is correct
35 Correct 6 ms 25436 KB Output is correct
36 Correct 6 ms 25336 KB Output is correct
37 Correct 5 ms 25176 KB Output is correct
38 Correct 6 ms 25436 KB Output is correct
39 Correct 6 ms 25436 KB Output is correct
40 Correct 6 ms 25436 KB Output is correct
41 Correct 6 ms 25436 KB Output is correct
42 Correct 6 ms 25436 KB Output is correct
43 Correct 6 ms 25436 KB Output is correct
44 Correct 6 ms 25436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 25180 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 5 ms 25180 KB Output is correct
4 Correct 5 ms 25432 KB Output is correct
5 Correct 5 ms 25248 KB Output is correct
6 Correct 5 ms 25180 KB Output is correct
7 Correct 5 ms 25296 KB Output is correct
8 Correct 5 ms 25180 KB Output is correct
9 Correct 5 ms 25180 KB Output is correct
10 Correct 5 ms 25180 KB Output is correct
11 Correct 6 ms 25180 KB Output is correct
12 Correct 5 ms 25180 KB Output is correct
13 Correct 5 ms 25292 KB Output is correct
14 Correct 5 ms 25180 KB Output is correct
15 Correct 4 ms 25180 KB Output is correct
16 Correct 5 ms 25180 KB Output is correct
17 Correct 5 ms 25180 KB Output is correct
18 Correct 5 ms 25272 KB Output is correct
19 Correct 5 ms 25180 KB Output is correct
20 Correct 5 ms 25180 KB Output is correct
21 Correct 5 ms 25500 KB Output is correct
22 Correct 5 ms 25180 KB Output is correct
23 Correct 6 ms 25432 KB Output is correct
24 Correct 6 ms 25436 KB Output is correct
25 Correct 6 ms 25316 KB Output is correct
26 Correct 6 ms 25432 KB Output is correct
27 Correct 6 ms 25436 KB Output is correct
28 Correct 6 ms 25436 KB Output is correct
29 Correct 6 ms 25436 KB Output is correct
30 Correct 6 ms 25324 KB Output is correct
31 Correct 6 ms 25432 KB Output is correct
32 Correct 6 ms 25436 KB Output is correct
33 Correct 8 ms 25436 KB Output is correct
34 Correct 6 ms 25508 KB Output is correct
35 Correct 6 ms 25436 KB Output is correct
36 Correct 6 ms 25336 KB Output is correct
37 Correct 5 ms 25176 KB Output is correct
38 Correct 6 ms 25436 KB Output is correct
39 Correct 6 ms 25436 KB Output is correct
40 Correct 6 ms 25436 KB Output is correct
41 Correct 6 ms 25436 KB Output is correct
42 Correct 6 ms 25436 KB Output is correct
43 Correct 6 ms 25436 KB Output is correct
44 Correct 6 ms 25436 KB Output is correct
45 Correct 285 ms 52816 KB Output is correct
46 Correct 318 ms 52168 KB Output is correct
47 Correct 284 ms 52608 KB Output is correct
48 Correct 252 ms 52164 KB Output is correct
49 Correct 270 ms 52176 KB Output is correct
50 Correct 287 ms 51920 KB Output is correct
51 Correct 292 ms 52172 KB Output is correct
52 Correct 276 ms 52664 KB Output is correct
53 Correct 263 ms 52236 KB Output is correct
54 Correct 251 ms 57160 KB Output is correct
55 Correct 237 ms 53816 KB Output is correct
56 Correct 222 ms 51044 KB Output is correct
57 Correct 225 ms 49776 KB Output is correct
58 Correct 163 ms 39668 KB Output is correct
59 Correct 162 ms 39740 KB Output is correct
60 Correct 154 ms 60436 KB Output is correct
61 Correct 190 ms 44008 KB Output is correct
62 Correct 260 ms 57168 KB Output is correct
63 Correct 176 ms 43832 KB Output is correct
64 Correct 183 ms 43872 KB Output is correct
65 Correct 251 ms 56912 KB Output is correct
66 Correct 165 ms 43552 KB Output is correct