이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#pragma GCC optimize ("O3")
using namespace std;
const int N=2e5+1;
const int LOG=18;
const long long inf=1e18+1;
long long bit_sum[N],ans[N];
int n,num,bit_cnt[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){
while(pos<=n){
bit_sum[pos]+=val;
if(val>=0){
bit_cnt[pos]++;
}
else{
bit_cnt[pos]--;
}
pos+=(pos&-pos);
}
}
long long get_sum(int x){ //sum of first x number
long long 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);
}
if(x==0){
break;
}
}
return sum;
}
void solve(int l,int r,int lef,int rig){
if(r-lef<num-1){
for(int i=l;i<=r;i++){
ans[i]=-inf;
}
return;
}
int mid=(l+r)>>1,idxmax=0;
long long max1;
if(nowl>rig||nowr<l){
memset(bit_cnt,0,sizeof(bit_cnt));
memset(bit_sum,0,sizeof(bit_sum));
nowl=1;
nowr=0;
}
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}});
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int i,j,k;
cin>>n>>num;
for(i=1;i<=n;i++){
cin>>lis[i].first>>lis[i].second;
lis[i].second<<=1;
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;
}
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<<*max_element(ans+1,ans+1+n);
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In function 'int main()':
cake3.cpp:95:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<idx.size();i++){
~^~~~~~~~~~~
cake3.cpp:82:8: warning: unused variable 'j' [-Wunused-variable]
int i,j,k;
^
cake3.cpp:82:10: warning: unused variable 'k' [-Wunused-variable]
int i,j,k;
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |