# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
894939 | fanwen | Constellation 3 (JOI20_constellation3) | C++17 | 324 ms | 60572 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define file(name) \
if(fopen(name".inp", "r")) \
freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
template <class T> struct Fenwick_Tree {
vector<T> bit;
int n;
Fenwick_Tree(int _n = 0) : n(_n), bit(_n + 5){}
void clear() { fill(bit.begin(), bit.end(), T(0)); }
void update(int u, T val) {
for (; u <= n; u += u & -u) bit[u] += val;
}
void update(int l, int r, T val) {
update(l, val);
update(r + 1, -val);
}
T get(int u) {
T ans = 0;
for (; u; u -= u & -u) ans += bit[u];
return ans;
}
T get(int l, int r) {
return get(r) - get(l - 1);
}
};
const int MAX = 2e5 + 5;
int n, m, a[MAX], time_in[MAX], time_out[MAX], anc[MAX][20];
vector <int> adj[MAX];
vector <pair <int, int>> stars[MAX];
long long dp[MAX], sum = 0;
void dfs(int u) {
static int run = 0;
for (int i = 1; i < 20; ++i) anc[u][i] = anc[anc[u][i - 1]][i - 1];
time_in[u] = ++run;
for (int v : adj[u]) {
dfs(v);
}
time_out[u] = run;
}
void _dfs(int u) {
static Fenwick_Tree <long long> bit(n);
for (int v : adj[u]) {
_dfs(v);
dp[u] += dp[v];
}
if((int) adj[u].size() == 2) {
for (int i = 0; i < 2; ++i) {
bit.update(time_in[adj[u][i]], time_out[adj[u][i]], dp[adj[u][1 ^ i]]);
}
}
for (pair <int, int> tmp : stars[u]) {
int v, c; tie(v, c) = tmp;
long long res = bit.get(time_in[v]) + c;
for (int x : adj[v]) res += dp[x];
dp[u] = max(dp[u], res);
}
}
void you_make_it(void) {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
static stack <int> st;
int last = -1;
while(!st.empty() && a[st.top()] < a[i]) {
last = st.top();
st.pop();
}
if(!st.empty()) anc[i][0] = st.top();
else anc[i][0] = i;
if(last != -1) anc[last][0] = i;
st.push(i);
}
int rt = 1;
for (int i = 1; i <= n; ++i) {
if(anc[i][0] == i) rt = i;
else adj[anc[i][0]].push_back(i);
}
dfs(rt);
cin >> m; while(m--) {
int x, y, c; cin >> x >> y >> c;
sum += c;
int u = x;
for (int i = 19; i >= 0; --i) if(a[anc[u][i]] < y) {
u = anc[u][i];
}
stars[u].emplace_back(x, c);
}
_dfs(rt);
cout << sum - dp[rt];
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
file("JOI20_constellation3");
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |