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;
using ll = long long ;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l) == r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 1<<18;
const ll linf = 1ll << 59;
int n, k;
// color, value
pair<int,int> cake[MAX_N];
struct lisan : vector<int> {
void done() { sort(AI()), erase(unique(AI()), end()); }
int operator()(int i) { return lower_bound(AI(), i) - begin(); }
}allv;
ll res = -linf;
struct sgt {
ll sum[MAX_N << 1], cnt[MAX_N << 1];
int cl = 0, cr = -1;
void add(int i, int d) {
int val = allv[i];
i += MAX_N;
sum[i] += val * d, cnt[i] += d;
for (;i>>=1;) {
sum[i] = sum[i<<1] + sum[i<<1|1];
cnt[i] = cnt[i<<1] + cnt[i<<1|1];
}
}
ll qry(int k = ::k) {
if (cnt[1] < k) return -linf;
int now = 1;
ll res = 0;
while (now < MAX_N) {
if (cnt[now<<1] > k)
now <<= 1;
else {
k -= cnt[now<<1];
res += sum[now<<1];
now = now << 1 | 1;
}
}
DE(res);
return res;
}
void reset() {
memset(sum, 0, sizeof(sum));
memset(cnt, 0, sizeof(cnt));
cl = 0, cr = -1;
}
void update(int l, int r) {
if (r < cr) reset();
DE(l, r);
while (cr < r) {
add(cake[++cr].second, 1);
}
while (cl < l) {
add(cake[cl++].second,-1);
}
}
}tree;
ll cost(int i, int j) {
return 2ll * abs(cake[i].first - cake[j].first);
}
void solve() {
queue< tuple<int,int,int,int> > q;
q.emplace(k-1, n-1, 0, n-1);
while (q.size()) {
auto [l, r, fl, fr] = q.front(); q.pop();
if (l > r) continue;
int mid = l + r >> 1;
ll mx = -linf, pos = -1;
for (int i = fl;i <= min(mid, fr);++i) {
tree.update(i, mid);
DE(tree.qry(), cost(i, mid));
if (chmax(mx, tree.qry() - cost(i, mid)))
pos = i;
}
DE(mid, pos, mx);
chmax(res, mx);
q.emplace(l, mid-1, fl, pos);
q.emplace(mid+1, r, pos, fr);
}
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for (int i = 0;i < n;++i) {
auto &[c, v] = cake[i];
cin >> v >> c;
allv.pb(v);
}
allv.done();
sort(cake, cake + n);
for (int i = 0;i < n;++i) {
auto &[c, v] = cake[i];
v = allv(v);
}
solve();
cout << res << '\n';
}
Compilation message (stderr)
cake3.cpp: In member function 'll sgt::qry(int)':
cake3.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
cake3.cpp:56:3: note: in expansion of macro 'DE'
56 | DE(res);
| ^~
cake3.cpp: In member function 'void sgt::update(int, int)':
cake3.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
cake3.cpp:66:3: note: in expansion of macro 'DE'
66 | DE(l, r);
| ^~
cake3.cpp: In function 'void solve()':
cake3.cpp:85:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
85 | int mid = l + r >> 1;
| ~~^~~
cake3.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
cake3.cpp:89:4: note: in expansion of macro 'DE'
89 | DE(tree.qry(), cost(i, mid));
| ^~
cake3.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
cake3.cpp:93:3: note: in expansion of macro 'DE'
93 | DE(mid, pos, mx);
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |