#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll
using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
/*
Competitive Programming notes that I need to study & fix my dumbass self:
1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you
2. Stress:
- Always try generating big testcases and try if they run
3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time
Time management schedule:
Offline / LAH days (4 problems - 3h):
15' thinking of solution / idea
1. no idea: skip
2. yes idea: continue thinking for <= 15'
+ implementing: <= 20'
+ brute-force: <= 5'
+ test generator: <= 5'
I hate offline because I am dumb
*/
typedef pair<int, int> ii;
const int N = 2e5+5;
const int B = 750;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;}
struct node {
int l = -1, r = -1, cnt = 0, sum = 0;
node(){}
node(int l, int r, int cnt, int sum): l(l), r(r), cnt(cnt), sum(sum) {}
};
int n,m, ans = -inf;
vector<ii> a;
vector<int> v;
vector<node> st;
int ver[N];
int build(int l, int r)
{
if (l == r)
{
st.pb(node());
return st.size()-1;
}
int mid = l+r>>1;
node cur = node();
cur.l = build(l, mid), cur.r = build(mid+1, r);
st.pb(cur);
return st.size()-1;
}
int update(int oldid, int l, int r, int i)
{
if (l == r)
{
st.pb(node(-1, -1, st[oldid].cnt+1, st[oldid].sum+v[l]));
return st.size()-1;
}
int mid = l+r>>1;
node cur = node();
if (i <= mid)
{
cur.l = update(st[oldid].l, l, mid, i);
cur.r = st[oldid].r;
} else
{
cur.l = st[oldid].l;
cur.r = update(st[oldid].r, mid+1, r, i);
}
cur.cnt = st[cur.l].cnt + st[cur.r].cnt;
cur.sum = st[cur.l].sum + st[cur.r].sum;
st.pb(cur);
return st.size()-1;
}
int walk(int oldid, int id, int l, int r, int k)
{
if (l == r)
{
return min(k, st[id].cnt - st[oldid].cnt) * v[l];
}
int mid = l+r>>1;
if (st[st[id].r].cnt - st[st[oldid].r].cnt >= k) return walk(st[oldid].r, st[id].r, mid+1, r, k);
return (st[st[id].r].sum - st[st[oldid].r].sum) + walk(st[oldid].l, st[id].l, l, mid, k-(st[st[id].r].cnt - st[st[oldid].r].cnt));
}
void dnc(int l, int r, int optl, int optr)
{
if (l > r) return;
int mid = l+r>>1;
ii best = {-inf, -1};
for (int i = optl; i <= min(optr, mid-m+1); i++)
{
int val = walk(ver[i-1], ver[mid], 0, v.size()-1, m) - 2ll * (a[mid].fi - a[i].fi);
// cout<<i<<" "<<mid<<" "<<val<<" "<<2 * (a[mid].fi - a[i].fi)<<endl;
best = max(best, {val, i});
}
// cout<<mid<<" "<<best.se<<" "<<best.fi<<endl;
// cout<<l<<" "<<r<<" "<<best.fi<<endl;
ans = max(ans, best.fi);
dnc(l, mid-1, optl, best.se); dnc(mid+1, r, best.se, optr);
}
void solve()
{
cin>>n>>m;
a.pb({0, 0});
for (int i = 1; i <= n; i++)
{
int x,y;
cin>>x>>y;
a.pb({y, x});
v.pb(x);
}
sort(all(v)); v.erase(unique(all(v)), v.end());
for (int i = 1; i <= n; i++) a[i].se = lower_bound(all(v), a[i].se) - v.begin();
sort(all(a));
// for (int i = 1; i <= n; i++) cout<<a[i].fi<<" "<<v[a[i].se]<<endl;
ver[0] = build(0,v.size()-1);
for (int i = 1; i <= n; i++)
{
ver[i] = update(ver[i-1], 0, v.size()-1, a[i].se);
}
dnc(m, n, 1, n-m+1);
cout<<ans;
}
/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/
signed main()
{
bruh
//freopen("input.inp","r",stdin);
//freopen("output.inp","w",stdout);
int t = 1;
// cin>>t;
while (t--)
{
solve();
cout<<"\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |