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"
using namespace std;
typedef pair <int, int> pii;
pii a[200010];
int opt[200010];
const long long inf = 1e18;
long long rev[200010];
int cmp[200010];
struct node {
node *l, *r;
int cnt;
long long sum;
node () {
l = r = NULL;
cnt = 0;
sum = 0;
}
void merge() {
cnt = 0;
sum = 0;
if(l) {
cnt += l -> cnt;
sum += l -> sum;
}
if(r) {
cnt += r -> cnt;
sum += r -> sum;
}
}
} *t[200010];
typedef node* pnode;
int idx;
void insert(pnode &prev, pnode &cur, int val, int b = 1, int e = idx) {
cur = new node();
if(b == e) {
cur -> cnt = prev -> cnt + 1;
cur -> sum = prev -> sum + rev[b];
return ;
}
int m = (b + e) >> 1;
if(val <= m) {
cur -> r = prev -> r;
insert(prev -> l, cur -> l, val, b, m);
} else {
cur -> l = prev -> l;
insert(prev -> r, cur -> r, val, m + 1, e);
}
cur -> merge();
}
typedef pair <int, long long> data;
long long Query(pnode prev, pnode cur, int k, int b = 1, int e = idx) {
if(b == e) {
return 1LL * rev[b] * k;
}
int m = (b + e) >> 1;
int rt = (cur -> r -> cnt) - (prev -> r -> cnt);
if(rt >= k) {
return Query(prev -> r, cur -> r, k, m + 1, e);
} else {
return (cur -> r -> sum) - (prev -> r -> sum) + Query(prev -> l, cur -> l, k - rt, b, m);
}
}
void build(pnode &cur, int b = 1, int e = idx) {
cur = new node();
if(b == e) {
return ;
}
int m = (b + e) >> 1;
build(cur -> l, b, m);
build(cur -> r, m + 1, e);
}
int n, m;
long long query(int i, int j) {
vector <int> v;
long long ans = -2 * (a[j].first - a[i].first);
ans += Query(t[i - 1], t[j], m);
return ans;
}
int search(int b, int e, int p, int q) {
if(b > e) return n+1;
if(b == e) {
return query(p, b) <= query(q, b) ? b : n+1;
}
int mid = (b + e) >> 1;
if(query(p, mid) <= query(q, mid)) {
return search(b, mid, p, q);
} else {
return search(mid + 1, e, p, q);
}
}
int main(int argc, char const *argv[])
{
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) {
int p, q;
scanf("%d %d", &p, &q);
a[i] = pii(q, p);
}
sort(a + 1, a + n + 1);
map <int, int> mp;
for(int i = 1; i <= n; i++) {
mp[a[i].second];
}
for(auto &i : mp) {
i.second = ++idx;
rev[idx] = i.first;
}
for(int i = 0; i <= n; i++) {
t[i] = NULL;
}
build(t[0]);
for(int i = 1; i <= n; i++) {
insert(t[i - 1], t[i], mp[a[i].second]);
}
long long ans = LLONG_MIN;
vector <pii> v;
v.emplace_back(m, 1);
int l = 0;
for(int i = 2; i <= n; i++) {
while(l + 1 < v.size() && v[l + 1].first <= i) {
++l;
}
opt[i] = v[l].second;
while(!v.empty() && i + m - 1 <= v.back().first && query(i, v.back().first) >= query(v.back().second, v.back().first)) {
v.pop_back();
}
int change = search(max(i + m - 1, v.back().first), n, v.back().second, i);
if(change <= n) {
v.emplace_back(change, i);
}
}
for(int i = m; i <= n; i++) {
ans = max(ans, query(opt[i], i));
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'int main(int, const char**)':
cake3.cpp:131:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(l + 1 < v.size() && v[l + 1].first <= i) {
~~~~~~^~~~~~~~~~
cake3.cpp:102:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
cake3.cpp:105:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &p, &q);
~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |