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>
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3,unroll-loops")
#define fastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
using namespace std;
using ll [[maybe_unused]] = long long;
using ull [[maybe_unused]] = unsigned long long;
using lll [[maybe_unused]] = __int128;
using lld [[maybe_unused]] = long double;
using llld [[maybe_unused]] = __float128;
template<typename T> using graph [[maybe_unused]] = vector<vector<pair<int,T>>>;
template<typename T> using matrix [[maybe_unused]] = vector<vector<T>>;
/**
* "Premature optimization is the root of all evil."
* <sub> Donald Knuth </sub>
*/
#ifdef ONLINE_JUDGE // Be careful if problem is about strings.
/**
* Namespace for Fast I/O
*
* @class@b INPUT
* class which can replace the cin
*
* @class@b OUTPUT
* class which can replace the cout
*
* @Description
* These classes use low-level i/o functions (@c fread()/mmap() for input, @c fwrite()/write() for output)
* so that they can read or write much faster than cin and cout. <br>
* Several macros are defined for convenience of using them.
*
* @Author <a href="https://blog.naver.com/jinhan814/222266396476">jinhan814</a>
* @Date 2021-05-06
*/
namespace FastIO {
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
constexpr int SZ = 1 << 20;
/* Class INPUT */
class INPUT {
private:
char* p;
bool __END_FLAG__, __GET_LINE_FLAG__; // NOLINT
public:
explicit operator bool() const { return !__END_FLAG__; }
INPUT() {
struct stat st;
fstat(0, &st);
p = (char*)mmap(0, st.st_size, PROT_READ, MAP_SHARED, 0, 0);
}
static bool IsBlank(char c) { return c == ' ' || c == '\n'; }
static bool IsEnd(char c) { return c == '\0'; }
char _ReadChar() { // NOLINT
return *p++;
}
char ReadChar() {
char ret = _ReadChar();
for (; IsBlank(ret); ret = _ReadChar());
return ret;
}
template<class T>
T ReadInt() {
T ret = 0;
char curr = _ReadChar();
bool minus_flag = false;
for (; IsBlank(curr); curr = _ReadChar());
if (curr == '-') minus_flag = true, curr = _ReadChar();
for (; !IsBlank(curr) && !IsEnd(curr); curr = _ReadChar())
ret = 10 * ret + (curr & 15);
if (IsEnd(curr)) __END_FLAG__ = true;
return minus_flag ? -ret : ret;
}
std::string ReadString() {
std::string ret;
char curr = _ReadChar();
for (; IsBlank(curr); curr = _ReadChar());
for (; !IsBlank(curr) && !IsEnd(curr); curr = _ReadChar())
ret += curr;
if (IsEnd(curr)) __END_FLAG__ = true;
return ret;
}
double ReadDouble() {
std::string ret = ReadString();
return std::stod(ret);
}
std::string getline() {
std::string ret;
char curr = _ReadChar();
for (; curr != '\n' && !IsEnd(curr); curr = _ReadChar())
ret += curr;
if (__GET_LINE_FLAG__) __END_FLAG__ = true;
if (IsEnd(curr)) __GET_LINE_FLAG__ = true;
return ret;
}
friend INPUT &getline(INPUT &in, std::string &s) {
s = in.getline();
return in;
}
} _in;
/* End of Class INPUT */
/* Class OUTPUT */
class OUTPUT {
private:
char writeBuffer[SZ];
int write_idx;
public:
~OUTPUT() { Flush(); }
explicit operator bool() const { return true; }
void Flush() {
write(1, writeBuffer, write_idx);
write_idx = 0;
}
void WriteChar(char c) {
if (write_idx == SZ) Flush();
writeBuffer[write_idx++] = c;
}
template<class T>
void WriteInt(T n) {
int sz = GetSize(n);
if (write_idx + sz >= SZ) Flush();
if (n < 0) writeBuffer[write_idx++] = '-', n = -n;
for (int i = sz; i-- > 0; n /= 10)
writeBuffer[write_idx + i] = n % 10 | 48;
write_idx += sz;
}
void WriteString(const std::string& s) {
for (auto &c: s) WriteChar(c);
}
void WriteDouble(double d) {
WriteString(std::to_string(d));
}
template<class T>
int GetSize(T n) {
int ret = 1;
for (n = n >= 0 ? n : -n; n >= 10; n /= 10) ret++;
return ret;
}
} _out;
/* End of Class OUTPUT */
/* Operators */
INPUT &operator>>(INPUT &in, char &i) {
i = in.ReadChar();
return in;
}
INPUT &operator>>(INPUT &in, std::string &i) {
i = in.ReadString();
return in;
}
template<class T, typename std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
INPUT &operator>>(INPUT &in, T &i) {
if constexpr (std::is_floating_point_v<T>) i = in.ReadDouble();
else if constexpr (std::is_integral_v<T>) i = in.ReadInt<T>();
return in;
}
OUTPUT &operator<<(OUTPUT &out, char i) {
out.WriteChar(i);
return out;
}
OUTPUT &operator<<(OUTPUT &out, const std::string &i) {
out.WriteString(i);
return out;
}
template<class T, typename std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
OUTPUT &operator<<(OUTPUT &out, T i) {
if constexpr (std::is_floating_point_v<T>) out.WriteDouble(i);
else if constexpr (std::is_integral_v<T>) out.WriteInt(i);
return out;
}
/* Macros for convenience */
#undef fastIO
#define fastIO 1
#define cin _in
#define cout _out
#define istream INPUT
#define ostream OUTPUT
};
using namespace FastIO;
#endif
class RMQ {
vector<pair<int,int>> tree; // {val, idx}
int size, n;
public:
RMQ(int sz) : size(1 << int(ceil(log2(sz)) + 1)) {
tree.resize(size + 1, {2e9, 0});
n = size >> 1;
}
void update(int i, int x) {
tree[i += n] = {x, i};
for (i >>= 1; i > 0; i >>= 1)
tree[i] = min(tree[i << 1], tree[i << 1 | 1]);
}
pair<int,int> query(int l, int r) {
pair<int,int> res = {2e9, 2e9};
for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
if (l & 1) res = min(res, tree[l++]);
if (~r & 1) res = min(res, tree[r--]);
}
return res;
}
};
int32_t main() {
fastIO;
int n;
cin >> n;
vector<pair<int,int>> v(n);
vector<int> decompress(n);
int max_x = 0;
for (int i = 0; i < n; i++) {
cin >> decompress[max_x] >> v[i].second;
v[i].first = max_x;
if (i & 1) max_x++;
}
RMQ segTree(max_x);
for (int i = 1; i < n - 1; i += 2) {
segTree.update(v[i].first, v[i].second);
}
priority_queue<ll> pq;
auto dnc = [&](auto &&self, int s, int e, int h) -> ll {
auto [y, mid] = segTree.query(s, e);
auto l = decompress[v[s << 1 | 1].first];
auto r = decompress[v[(e + 1) << 1].first];
ll upper_area = ll(r - l) * (y - h);
if (s >= e) return upper_area;
ll lower_area1 = self(self, s, mid - 1, y);
ll lower_area2 = self(self, mid + 1, e, y);
pq.emplace(min(lower_area1, lower_area2));
return upper_area + max(lower_area1, lower_area2);
};
pq.emplace(dnc(dnc, 0, (n >> 1) - 2, 0));
int k;
cin >> k;
ll ans = 0;
while (!pq.empty() && k--) {
ans += pq.top();
pq.pop();
}
cout << ans;
return 0;
}
# | 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... |