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
using namespace std;
const int N=2e5+3;
const int LOG=18;
const int inf=1e18+2;
int n,num,bit_cnt[N],bit_sum[N],ans[N],nowl=1,nen[N],nowr=0;
pair<int,int> lis[N];
vector<int> idx;
queue<pair<pair<int,int>,pair<int,int> > > dac_lis;
void upd(int pos,int val){
// cout<<pos<<' '<<val<<endl;
while(pos<=n){
bit_sum[pos]+=val;
if(val>=0){
bit_cnt[pos]++;
}
else{
bit_cnt[pos]--;
}
pos+=(pos&-pos);
}
}
int get_sum(int x){ //sum of first x number
int sum=0;
for(int i=0,j=LOG-1;j>=0;j--){
if(i+(1<<j)<=n&&bit_cnt[i+(1<<j)]<=x){
x-=bit_cnt[i+(1<<j)];
sum+=bit_sum[i+(1<<j)];
i+=(1<<j);
}
}
return sum;
}
void solve(int l,int r,int lef,int rig){
int mid=(l+r)/2,max1,idxmax=0;
if(nowl>rig||nowr<l){
memset(bit_cnt,0,sizeof(bit_cnt));
memset(bit_sum,0,sizeof(bit_sum));
nowl=1;
nowr=0;
}
ans[mid]=-inf;
while(nowr<mid){
nowr++;
upd(n-nen[nowr]+1,lis[nowr].first); //take the max so just reverse them
}
while(nowl<=rig&&nowr-nowl+1>=num){
if(nowl>=lef&&nowl<=rig){
max1=get_sum(num)-lis[mid].second+lis[nowl].second;
if(ans[mid]<max1){
ans[mid]=max1;
idxmax=nowl;
}
}
if(nowl==rig){
break;
}
upd(n-nen[nowl]+1,-lis[nowl].first);
nowl++;
}
if(l>mid){
dac_lis.push({{l,mid-1},{lef,idxmax}});
}
if(mid<r){
dac_lis.push({{mid+1,r},{idxmax,rig}});
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int i,j,k,l;
cin>>n>>num;
for(i=1;i<=n;i++){
cin>>lis[i].first>>lis[i].second;
lis[i].second*=2;
idx.push_back(i);
}
sort(lis+1,lis+1+n,[&](pair<int,int> x,pair<int,int> y){
return x.second<y.second;
});
sort(idx.begin(),idx.end(),[&](int x,int y){
return lis[x].first<lis[y].first;
});
for(i=0;i<idx.size();i++){
nen[idx[i]]=i+1;
// cout<<idx[i]<<' '<<i+1<<endl;
}
dac_lis.push({{1,n},{1,n}});
while(dac_lis.size()){
solve(dac_lis.front().first.first,dac_lis.front().first.second,dac_lis.front().second.first,dac_lis.front().second.second);
dac_lis.pop();
//cout<<dac_lis.front().first.first<<' '<<dac_lis.front().first.second<<' '<<dac_lis.front().second.first<<' '<<dac_lis.front().second.second<<endl;
}
for(i=1;i<=n;i++){
ans[1]=max(ans[1],ans[i]);
}
cout<<ans[1];
}
Compilation message (stderr)
cake3.cpp: In function 'int main()':
cake3.cpp:85:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<idx.size();i++){
~^~~~~~~~~~~
cake3.cpp:72:8: warning: unused variable 'j' [-Wunused-variable]
int i,j,k,l;
^
cake3.cpp:72:10: warning: unused variable 'k' [-Wunused-variable]
int i,j,k,l;
^
cake3.cpp:72:12: warning: unused variable 'l' [-Wunused-variable]
int i,j,k,l;
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |