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 "aliens.h"
#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// find_by_order(), order_of_key()
typedef long long ll;
typedef long double dd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<dd, dd> pdd;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef vector<dd> vdd;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pdd> vpdd;
typedef vector<vii> vvii;
typedef vector<vll> vvll;
typedef vector<vdd> vvdd;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
typedef vector<vpdd> vvpdd;
typedef vector<bool> vb;
const int inf = 1 << 30;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define ffor(i, a, b) for(ll i = a; i < b; i++)
#define forin(x, a) for (auto &x: a)
#define all(x) x.begin(), x.end()
#ifdef TEST
#define dbg(x) cout << #x << ": " << x << '\n';
#define dbgc(x) cout << #x << ":"; forin(a, x) { cout << " " << a; } cout << endl;
#define tassert(x) assert(x);
#else
#define dbg(x)
#define dbgc(x)
#define tassert(x)
#endif
auto cmp = [](pll a, pll b) {
return (a.first == b.first ? b.second < a.second : a.first < b.first);
};
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
set<pll, decltype(cmp)> segs(cmp);
rep(i, n) {
segs.emplace(min(r[i], c[i]), max(r[i], c[i]) + 1);
}
ll res = 0;
pll last = {-1, -1};
ll l;
map<ll, ll> stoe;
forin(seg, segs) {
if (seg.second <= last.second) {
continue;
}
res += (seg.second - seg.first) * (seg.second - seg.first);
l = min(last.second, seg.second) - seg.first;
if (l > 0) {
res -= l * l;
}
stoe[seg.first] = seg.second;
last = seg;
}
last = {-1, -1};
ll area;
map<pll, ll> lookup;
set<tuple<ll, ll, ll>> pq;
forin(seg, stoe) {
if (last.first != -1) {
if (seg.first < last.second) {
area = 2 * (seg.first - last.first) * (seg.second - last.second);
} else {
area = 2 * (seg.first - last.first) * (seg.second - last.second) - (seg.first - last.second) * (seg.first - last.second);
}
assert(area > 0);
pq.emplace(area, last.first, seg.first);
lookup[{last.first, seg.first}] = area;
}
last = seg;
}
while (stoe.size() > k) {
auto &[area, a, b] = *pq.begin();
pq.erase(pq.begin());
res += area;
if (stoe.begin()->first < a) {
ll aa = prev(stoe.find(a))->first;
pll p = {aa, a};
pq.erase({lookup[p], aa, a});
lookup[p] += 2 * (a - aa) * (stoe[b] - stoe[a]);
pq.emplace(lookup[p], aa, a);
}
if (stoe.rend()->second > stoe[b]) {
ll bb = next(stoe.find(b))->first;
pll p = {b, bb};
pq.erase({lookup[p], b, bb});
lookup[p] += 2 * (b - a) * (stoe[bb] - stoe[b]);
pq.emplace(lookup[p], b, bb);
}
stoe[a] = stoe[b];
stoe.erase(b);
}
return res;
}
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:92:22: warning: comparison of integer expressions of different signedness: 'std::map<long long int, long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
92 | while (stoe.size() > k) {
| ~~~~~~~~~~~~^~~
# | 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... |