이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
vector<pair<int,int> > cake;
struct node{
int s,e;
vector<int> v;
vector<int> pref;
node *l,*r;
node (int _s, int _e){
//printf("new nd %lld %lld\n",_s,_e);
s = _s;
e = _e;
for (int x = s; x<=e; x++){
v.push_back(-cake[x].second);
}
sort(v.begin(),v.end());
pref.push_back(v[0]);
for (int x = 1; x<v.size(); x++){
pref.push_back(pref[x-1]+v[x]);
}
if (s!=e){
l = new node(s,(s+e)/2);
r = new node((s+e)/2+1,e);
}
}
int numBigger(int a, int b, int num){ ///number of elements >= num
//printf("qu %lld %lld %lld\n",a,b,num);
if (a<=s && e<=b){
return upper_bound(v.begin(),v.end(),-num)-v.begin();
}
if (b<=(s+e)/2) return l->numBigger(a,b,num);
if (a>(s+e)/2) return r->numBigger(a,b,num);
return l->numBigger(a,b,num)+r->numBigger(a,b,num);
}
int sumBigger(int a, int b, int num){
if (a<=s && e<=b){
int t = upper_bound(v.begin(),v.end(),-num)-v.begin()-1;
return t<=-1?0:-pref[t];
}
if (b<=(s+e)/2) return l->sumBigger(a,b,num);
if (a>(s+e)/2) return r->sumBigger(a,b,num);
return l->sumBigger(a,b,num)+r->sumBigger(a,b,num);
}
}*root;
main(){
scanf("%lld%lld",&n,&m);
for (int x = 0; x<n; x++){
int a,b;
scanf("%lld%lld",&a,&b);
cake.push_back({b,a});
}
sort(cake.begin(),cake.end());
root = new node(0,n-1);
int ans = -999999999999999999LL;
for (int x = 0; x<n; x++){
for (int y = x+m-1; y<n; y++){
///find m-th largest element in range
int l = 0;
int r = 10000000001LL;
while (l+1<r){
int mid = (l+r)/2;
if (root->numBigger(x,y,mid)>=m){
l = mid;
}
else r = mid;
}
//printf("l = %lld\n",l);
//printf("numbigger x,y,l %lld\n",root->numBigger(x,y,l));
int opt = root->sumBigger(x,y,l);
opt -= (root->numBigger(x,y,l)-m)*l;
opt -= 2*(cake[y].first-cake[x].first);
//printf("try %lld-%lld, got %lld\n",x,y,opt);
ans = max(ans,opt);
}
}
printf("%lld",ans);
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In constructor 'node::node(long long int, long long int)':
cake3.cpp:22:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int x = 1; x<v.size(); x++){
~^~~~~~~~~
cake3.cpp: At global scope:
cake3.cpp:51:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
cake3.cpp: In function 'int main()':
cake3.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&n,&m);
~~~~~^~~~~~~~~~~~~~~~~~
cake3.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&a,&b);
~~~~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |