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>
#include "meetings.h"
using namespace std;
const int MAX_N = 75e4 + 5;
int N, Q;
long long H[MAX_N];
pair <int, int> table[MAX_N][20];
vector <int> graph[MAX_N];
pair <int, int> getMax(int l, int r) {
int j = log2(r - l + 1);
return max(table[l][j], table[r - (1<<j) + 1][j]);
}
int build_tree(int l, int r) {
if(l > r) {
return -1;
}
int u = getMax(l, r).second;
int L = build_tree(l, u - 1);
int R = build_tree(u + 1, r);
if(L != -1) {
graph[u].push_back(L);
}
if(R != -1) {
graph[u].push_back(R);
}
return u;
}
long long dfs(int u, int l, int r, long long total = 0) {
long long now = H[u] * (r - l + 1) + total;
for(auto v : graph[u]) {
if(v < u) {
now = min(now, dfs(v, l, u - 1, total + H[u] * (r - u + 1)));
}
else {
now = min(now, dfs(v, u + 1, r, total + H[u] * (u - l + 1)));
}
}
return now;
}
vector <long long> minimum_costs(vector <int> h, vector <int> L, vector <int> R) {
N = h.size(), Q = L.size();
for(int i = 1; i <= N; i++) {
H[i] = h[i - 1];
table[i][0] = make_pair(H[i], i);
}
for(int j = 1; j < 20; j++) {
for(int i = 1; i + (1<<j) - 1 <= N; i++) {
table[i][j] = max(table[i][j - 1], table[i + (1<<(j - 1))][j - 1]);
}
}
int root = build_tree(1, N);
vector <long long> C;
for(int i = 0; i < Q; i++) {
L[i]++, R[i]++;
int rt = getMax(L[i], R[i]).second;
C.push_back(dfs(rt, L[i], R[i]));
}
return C;
}
Compilation message (stderr)
meetings.cpp: In function 'std::vector<long long int> minimum_costs(std::vector<int>, std::vector<int>, std::vector<int>)':
meetings.cpp:61:9: warning: unused variable 'root' [-Wunused-variable]
61 | int root = build_tree(1, N);
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |