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;
#define fo(i,s,t) for(int i = s; i <= t; ++ i)
#define fd(i,s,t) for(int i = s; i >= t; -- i)
#define bf(i,s) for(int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1ll<<60;
const db Inf = 1e20;
const db eps = 1e-9;
void gmax(int &a,int b){a = (a > b ? a : b);}
void gmin(int &a,int b){a = (a < b ? a : b);}
const int maxn = 200050;
const int _L = 1;
const int _R = 1e9;
int n, m, rt[maxn], tot;
int ls[maxn*30], rs[maxn*30], nd[maxn*30];
ll sum[maxn*30];
ll ans = -INF;
struct cake{int v, c;}c[maxn];
bool cmp(cake a, cake b)
{
return a.c < b.c;
}
void upd(int &now, int pre, int p, int l = 1, int r = 1000000000)
{
now = ++ tot;
nd[now] = nd[pre] + 1; sum[now] = sum[pre] + p;
ls[now] = ls[pre]; rs[now] = rs[pre];
if(l == r) return;
int mid = l + r >> 1;
if(p <= mid) upd(ls[now], ls[pre], p, l, mid);
else upd(rs[now], rs[pre], p, mid+1, r);
}
ll query(int now, int pre, int num, int l = 1, int r = 1000000000)
{
if(!now) return 0;
if(l == r) return num * 1ll * l;
int mid = l + r >> 1, c = nd[rs[now]] - nd[rs[pre]];
if(c == num) return sum[rs[now]] - sum[rs[pre]];
else if(c < num) return sum[rs[now]] - sum[rs[pre]] + query(ls[now], ls[pre], num - c, l, mid);
else return query(rs[now], rs[pre], num, mid+1, r);
}
ll ask(int l, int r)
{
return query(rt[r], rt[l-1], m);
}
void solve(int l, int r, int ll, int rr)
{
if(l > r) return;
int mid = l + r >> 1, p;
long long best = -INF;
fo(i, ll, rr)
if(mid - i + 1 >= m)
{
long long v = ask(i, mid) - (c[mid].c - c[i].c)*2;
if(best < v)
{
best = v;
p = i;
}
}
ans = max(ans, best);
solve(l, mid-1, ll, p);
solve(mid+1, r, p, rr);
}
int main()
{
#ifdef MPS
fp("1.in","r",stdin);
fp("1.out","w",stdout);
#endif
sf("%d%d",&n,&m);
fo(i,1,n) sf("%d%d",&c[i].v,&c[i].c);
sort(c+1,c+n+1,cmp);
fo(i,1,n) upd(rt[i], rt[i-1], c[i].v);
solve(1, n, 1, n);
pf("%lld\n",ans);
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'void upd(int&, int, int, int, int)':
cake3.cpp:53:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
cake3.cpp: In function 'll query(int, int, int, int, int)':
cake3.cpp:61:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1, c = nd[rs[now]] - nd[rs[pre]];
~~^~~
cake3.cpp: In function 'void solve(int, int, int, int)':
cake3.cpp:73:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1, p;
~~^~~
cake3.cpp: In function 'int main()':
cake3.cpp:95:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
sf("%d%d",&n,&m);
^
cake3.cpp:96:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fo(i,1,n) sf("%d%d",&c[i].v,&c[i].c);
^
cake3.cpp: In function 'void solve(int, int, int, int)':
cake3.cpp:86:7: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
solve(l, mid-1, ll, p);
~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |