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>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e18
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "cake3"
#define all(a) a.begin(),a.end()
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
const int MAXN = 3e5 + 9;
int n,m,a[MAXN],ans=INF;
ii p[MAXN];
vector<int> listval;
vector<int> valpos[MAXN];
vector<int> pre[MAXN<<2];
vector<int> wavelet[MAXN<<2];
vector<int> bit[MAXN<<2];
int getval(int x){
return lower_bound(all(listval),x) - listval.begin() + 1;
}
void merged(int node,int a,int b){
int ptr1 = 0, ptr2 = 0, prebit = 0, presum = 0;
bit[node].pb(0);
pre[node].pb(0);
while(ptr1 < wavelet[a].size() or ptr2 < wavelet[b].size()){
if (ptr1 == wavelet[a].size()){
wavelet[node].pb(wavelet[b][ptr2++]);
bit[node].pb(prebit);
presum = presum + p[wavelet[b][ptr2-1]].fi;
pre[node].pb(presum);
}
else if (ptr2 == wavelet[b].size()){
wavelet[node].pb(wavelet[a][ptr1++]);
bit[node].pb(++prebit);
presum = presum + p[wavelet[a][ptr1-1]].fi;
pre[node].pb(presum);
}
else if (wavelet[a][ptr1] < wavelet[b][ptr2]){
wavelet[node].pb(wavelet[a][ptr1++]);
bit[node].pb(++prebit);
presum = presum + p[wavelet[a][ptr1-1]].fi;
pre[node].pb(presum);
}
else {
wavelet[node].pb(wavelet[b][ptr2++]);
bit[node].pb(prebit);
presum = presum + p[wavelet[b][ptr2-1]].fi;
pre[node].pb(presum);
}
}
}
void build(int node,int l,int r){
if (l==r){
int presum = 0;
pre[node].pb(0);
for(auto x: valpos[l]){
presum += listval[l-1];
wavelet[node].pb(x);
pre[node].pb(presum);
}
return;
}
int mid = (l+r)>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
int id1 = 0, id2 = 0;
merged(node,node<<1,node<<1|1);
}
int getkth(int node,int l,int r,int u,int v,int k){
if (l==r){
return k * listval[l-1];
}
if (u>v) return 0;
int mid = (l+r)>>1;
int poslu = bit[node][u] + (bit[node][u] - bit[node][u-1] == 0);
int poslv = bit[node][v];
int posru = u - bit[node][u] + (bit[node][u] - bit[node][u-1] == 1);
int posrv = v - bit[node][v];
// cout<<l<<' '<<r<<' '<<poslu<<' '<<poslv<<' '<<posru<<' '<<posrv<<' '<<k<<endl;
if (posrv - posru + 1 >= k) {
return getkth(node<<1|1,mid+1,r,posru,posrv,k);
}
else{
// cout<<"ADD:"<<pre[node<<1|1][posrv] - pre[node<<1|1][posru-1]<<endl;
return pre[node<<1|1][posrv] - pre[node<<1|1][posru-1] + getkth(node<<1,l,mid,poslu,poslv,k-posrv+posru-1);
}
}
int C(int l,int r){
return getkth(1,1,sz(listval),l,r,m) - (p[r].se - p[l].se)*2;
}
int dp[MAXN];
void dnc(int l,int r,int optl,int optr){
ii best = {-INF,-1};
int mid = (l+r)>>1;
if (l>r) return;
for(int i=max(mid+m-1,optl);i<=optr;i++){
best = max(best, ii(C(mid,i),i));
}
dp[mid] = best.fi;
int opt = best.se;
dnc(l,mid-1,optl,opt);
dnc(mid+1,r,opt,optr);
}
main()
{
fast;
if (fopen(TASKNAME".inp","r")){
freopen(TASKNAME".inp","r",stdin);
freopen(TASKNAME".out","w",stdout);
}
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>p[i].fi>>p[i].se;
listval.pb(p[i].fi);
}
sort(listval.begin(),listval.end());
listval.erase(unique(all(listval)),listval.end());
sort(p+1,p+1+n,[](const ii &a,const ii &b){
return a.se < b.se;
});
ans=-INF;
for(int i=1;i<=n;i++){
int x = getval(p[i].fi);
valpos[x].pb(i);
}
build(1,1,sz(listval));
dnc(1,n-m+1,1,n);
for(int i=1;i<=n-m+1;i++){
maximize(ans, dp[i]);
}
// cout<<endl;
cout<<ans<<endl;
}
Compilation message (stderr)
cake3.cpp: In function 'void merged(long long int, long long int, long long int)':
cake3.cpp:37:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | while(ptr1 < wavelet[a].size() or ptr2 < wavelet[b].size()){
| ~~~~~^~~~~~~~~~~~~~~~~~~
cake3.cpp:37:45: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | while(ptr1 < wavelet[a].size() or ptr2 < wavelet[b].size()){
| ~~~~~^~~~~~~~~~~~~~~~~~~
cake3.cpp:38:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if (ptr1 == wavelet[a].size()){
| ~~~~~^~~~~~~~~~~~~~~~~~~~
cake3.cpp:45:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | else if (ptr2 == wavelet[b].size()){
| ~~~~~^~~~~~~~~~~~~~~~~~~~
cake3.cpp: In function 'void build(long long int, long long int, long long int)':
cake3.cpp:80:9: warning: unused variable 'id1' [-Wunused-variable]
80 | int id1 = 0, id2 = 0;
| ^~~
cake3.cpp:80:18: warning: unused variable 'id2' [-Wunused-variable]
80 | int id1 = 0, id2 = 0;
| ^~~
cake3.cpp: At global scope:
cake3.cpp:118:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
118 | main()
| ^~~~
cake3.cpp: In function 'int main()':
cake3.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | freopen(TASKNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
cake3.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
123 | freopen(TASKNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |