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 "nile.h"
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define all(a) a.begin(), a.end()
// #define endl "\n"
void printVector(vector<int> a){
for (auto x: a) cout << x << " ";
cout << endl;
}
int max(int x, int y){
if (x > y) return x;
return y;
}
int maxi(vector<int> a){
int e = 0;
for (auto x: a) e = max(e, x);
return e;
}
std::vector<long long> calculate_costs(std::vector<int> W, std::vector<int> A,
std::vector<int> B, std::vector<int> E) {
int Q = (int)E.size();
std::vector<long long> R(Q, 0);
int n = (int) W.size();
vector<vector<int>> edges; // edge in the form of {diff, val, l, r}
// auto comp = [] (int a, int b) {
// return W[a] < W[b];
// };
vector<int> o;
FOR(i,0,n) o.pb(i);
sort(all(o), [&](int a, int b){
return W[a] < W[b];
});
vector<long long> a(n), b(n), w(n);
FOR(i,0,n){
a[i] = A[o[i]];
b[i] = B[o[i]];
w[i] = W[o[i]];
}
// A = a;
// B = b;
// W = w;
// printVector(W);
// printVector(o);
FOR(i,0,n-1){
edges.pb({w[i+1]-w[i], a[i]+a[i+1]-b[i]-b[i+1], i, i+1, 0});
if (i < n-2) edges.pb({w[i+2]-w[i], i+1, 1});
assert(w[i+1] >= w[i]);
}
sort(all(edges));
map<long long, long long> answers;
vector<int> F = E;
sort(all(F));
long long ans = 0;
FOR(i,0,n) ans += a[i];
// int disc = 0;
int u = edges.size();
vector<long long> even(n), odd(n);
FOR(i,0,n){
odd[i] = a[i]-b[i];
assert(odd[i] > 0);
}
FOR(i,1,n-1){
even[i] = (a[i-1]-b[i-1])+(a[i]-b[i])+(a[i+1]-b[i+1]);
// cout << even[i] << " ";
}
int curr = 0;
set<pair<int, int>> intervals;
vector<int> visited(n);
for (auto X: F){
// cout << X << endl;
while (curr < u){
if (edges[curr][0] <= X){
if (edges[curr][(int)edges[curr].size()-1] == 0){
// cout << "O" << endl;
pair<int, int> current = {edges[curr][2], edges[curr][3]};
for (auto x: intervals){
if (x.second == edges[curr][2]){
current.first = x.first;
intervals.erase(x);
break;
}
}
for (auto x: intervals){
if (x.first == current.second){
current.second = x.second;
intervals.erase(x);
break;
}
}
// cout << "a" << current.first << current.second << endl;
intervals.insert(current);
}else{
int x = edges[curr][1];
assert(!visited[x]);
even[x] -= (a[x-1]-b[x-1])+(a[x+1]-b[x+1]);
// assert(even[x] > 0);
visited[x] = 1;
assert(((int)edges[curr].size()) == 3);
}
curr++;
}else{
break;
}
}
long long disc = 0;
for (auto x: intervals){
long long m = 1e17;
FOR(i,x.first,x.second+1){
disc += a[i]-b[i];
}
if ((x.second-x.first)%2 == 1) break;
FOR(i,x.first,x.second+1){
if ((i-x.first)%2 == 0){
m = min(m, odd[i]);
}else{
m = min(m, even[i]);
}
}
// cout << m << endl;
disc -= m;
}
answers[X] = ans-disc;
// // cout << "a" << intervals.size() << endl;
// // for (auto item: intervals){
// // cout << item.first << item.second << endl;
// // }
}
FOR(i,0,Q){
R[i] = answers[E[i]];
}
return R;
}
Compilation message (stderr)
nile.cpp: In function 'std::vector<long long int> calculate_costs(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
nile.cpp:58:21: warning: narrowing conversion of '(w.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)(i + 1))) - w.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i)))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
58 | edges.pb({w[i+1]-w[i], a[i]+a[i+1]-b[i]-b[i+1], i, i+1, 0});
nile.cpp:58:21: warning: narrowing conversion of '(w.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)(i + 1))) - w.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i)))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
nile.cpp:58:44: warning: narrowing conversion of '(((a.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i)) + a.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)(i + 1)))) - b.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i))) - b.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)(i + 1))))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
58 | edges.pb({w[i+1]-w[i], a[i]+a[i+1]-b[i]-b[i+1], i, i+1, 0});
nile.cpp:58:44: warning: narrowing conversion of '(((a.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i)) + a.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)(i + 1)))) - b.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i))) - b.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)(i + 1))))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
nile.cpp:59:34: warning: narrowing conversion of '(w.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)(i + 2))) - w.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i)))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
59 | if (i < n-2) edges.pb({w[i+2]-w[i], i+1, 1});
nile.cpp:59:34: warning: narrowing conversion of '(w.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)(i + 2))) - w.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i)))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |