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<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 int N = 2e5 + 10;
vector<pair<int, ll>> mas[N];
int a[N];
SegTree t(N);
vector<pair<int, ll>> solve(int l, int r) {
if (l > r) return vector<pair<int, ll>> {};
if (l == r) {
sort(all(mas[l]));
return mas[l];
}
int m = t.get(l, r).second;
auto arr1 = solve(l, m - 1), arr2 = solve(m + 1, r);
ll best1 = 0, best2 = 0;
for (auto p : arr1) {
if (p.first <= a[m]) {
best1 = max(best1, p.second);
}
}
for (auto p : arr2) {
if (p.first <= a[m]) {
best2 = max(best2, p.second);
}
}
vector<pair<int, ll>> res;
{
int j = -1;
ll best = 0;
for (auto p : arr1) {
while (j + 1 < arr2.size() && arr2[j + 1].first <= p.first) {
j++;
best = max(best, arr2[j].second);
}
if (p.first <= a[m]) {
res.pb({p.first, p.second + best});
} else {
res.pb({p.first, p.second + best2});
}
}
}
{
int j = -1;
ll best = 0;
for (auto p : arr2) {
while (j + 1 < arr1.size() && arr1[j + 1].first <= p.first) {
j++;
best = max(best, arr1[j].second);
}
if (p.first <= a[m]) {
res.pb({p.first, p.second + best});
} else {
res.pb({p.first, p.second + best1});
}
}
}
for (auto p : mas[m]) {
res.pb({p.first, p.second + best1 + best2});
}
sort(all(res));
return res;
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
t.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});
}
auto res = solve(0, n - 1);
ll mx = 0;
for (auto p : res) {
mx = max(mx, p.second);
}
cout << sum - mx << endl;
return 0;
}
Compilation message (stderr)
constellation3.cpp: In function 'std::vector<std::pair<long long int, long long int> > solve(long long int, long long int)':
constellation3.cpp:87:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | while (j + 1 < arr2.size() && arr2[j + 1].first <= p.first) {
| ~~~~~~^~~~~~~~~~~~~
constellation3.cpp:102:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | while (j + 1 < arr1.size() && arr1[j + 1].first <= p.first) {
| ~~~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |