이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using t4=tuple<int,int,int,int>;
const int N=2e5+5;
const int K=1<<19;
const ll INF=LLONG_MAX/2;
int n,m;
ll v[N],c[N],pos[N];
ll ans=-INF;
pair<int,int> a[N];
vector<pair<int,int>> vec;
vector<t4> q;
struct Node{
ll sum;
int freq;
Node():sum(0),freq(0){}
Node(ll v):sum(v),freq(1){}
Node(ll v,int f):sum(v),freq(f){}
friend Node operator+(const Node &lhs,const Node &rhs){
return Node(lhs.sum+rhs.sum,lhs.freq+rhs.freq);
}
};
struct SegTree{
Node t[K];
void build(int l,int r,int i){
t[i]=Node();
if(l==r)return;
int m=(l+r)/2;
build(l,m,i*2);
build(m+1,r,i*2+1);
}
void build(){
build(1,n,1);
}
void update(int l,int r,int i,int x,const Node &v){
if(x<l||r<x)return;
if(l==r)return void(t[i]=v);
int m=(l+r)/2;
update(l,m,i*2,x,v);
update(m+1,r,i*2+1,x,v);
t[i]=t[i*2]+t[i*2+1];
}
void update(int x,const Node &v){
update(1,n,1,x,v);
}
ll query(int l,int r,int i,int k){
if(l==r)return t[i].sum;
int m=(l+r)/2;
if(t[i*2+1].freq<k)return query(l,m,i*2,k-t[i*2+1].freq)+t[i*2+1].sum;
return query(m+1,r,i*2+1,k);
}
ll query(int k){
return query(1,n,1,k);
}
}s;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for(int i=1;i<=n;i++)cin >> a[i].second >> a[i].first;
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
tie(c[i],v[i])=a[i];
vec.emplace_back(v[i],i);
}
sort(vec.begin(),vec.end());
for(int i=0;i<n;i++)pos[vec[i].second]=i+1;
q.emplace_back(1,n-m+1,1,n);
while(!q.empty()){
vector<t4> nq;
int st=1,ed=0;
s.build();
for(auto [l,r,optl,optr]:q){
if(l>r)continue;
int mid=(l+r)/2;
ll res=-INF;
int opt=-1;
for(int i=max(mid,optl),cnt=0;i<=optr;i++){
while(ed<i)s.update(pos[++ed],Node(v[ed]));
while(st<mid)s.update(pos[st++],Node());
if(i-mid+1<m)continue;
ll tmp=s.query(m)-2*c[i];
if(tmp>res)res=tmp,opt=i;
}
res+=2*c[mid];
ans=max(ans,res);
nq.emplace_back(l,mid-1,optl,opt);
nq.emplace_back(mid+1,r,opt,optr);
}
swap(q,nq);
}
cout << ans;
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In function 'int main()':
cake3.cpp:86:41: warning: operation on 'ed' may be undefined [-Wsequence-point]
86 | while(ed<i)s.update(pos[++ed],Node(v[ed]));
| ^~~~
cake3.cpp:85:37: warning: unused variable 'cnt' [-Wunused-variable]
85 | for(int i=max(mid,optl),cnt=0;i<=optr;i++){
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |