이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using t4=tuple<ll,ll,ll,ll>;
const ll N=2e5+5;
const ll K=1<<19;
const ll INF=LLONG_MAX/2;
ll n,m;
ll v[N],c[N],pos[N];
ll ans=-INF;
pair<ll,ll> a[N];
vector<pair<ll,ll>> vec;
vector<t4> q;
struct Node{
ll sum;
ll freq;
Node():sum(0),freq(0){}
Node(ll v):sum(v),freq(1){}
Node(ll v,ll 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(ll l,ll r,ll i){
t[i]=Node();
if(l==r)return;
ll m=(l+r)/2;
build(l,m,i*2);
build(m+1,r,i*2+1);
}
void build(){
build(1,n,1);
}
void update(ll l,ll r,ll i,ll x,const Node &v){
if(x<l||r<x)return;
if(l==r)return void(t[i]=v);
ll 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(ll x,const Node &v){
update(1,n,1,x,v);
}
ll query(ll l,ll r,ll i,ll k){
if(l==r)return t[i].sum;
ll 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(ll k){
return query(1,n,1,k);
}
}s;
ll st=1,ed=0;
void work(ll l,ll r){
while(ed<r)s.update(pos[++ed],Node(v[ed]));
while(st>l)s.update(pos[--st],Node(v[st]));
while(ed>r)s.update(pos[ed--],Node());
while(st<l)s.update(pos[st++],Node());
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for(ll i=1;i<=n;i++)cin >> a[i].second >> a[i].first;
sort(a+1,a+n+1);
for(ll i=1;i<=n;i++){
tie(c[i],v[i])=a[i];
vec.emplace_back(v[i],i);
}
sort(vec.begin(),vec.end());
for(ll 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;
for(auto [l,r,optl,optr]:q){
if(l>r)continue;
ll mid=(l+r)/2;
ll res=-INF;
ll opt=-1;
for(ll i=max(mid+m-1,optl),cnt=0;i<=optr;i++){
work(mid,i);
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 'void work(ll, ll)':
cake3.cpp:67:29: warning: operation on 'ed' may be undefined [-Wsequence-point]
67 | while(ed<r)s.update(pos[++ed],Node(v[ed]));
| ^~~~
cake3.cpp:68:29: warning: operation on 'st' may be undefined [-Wsequence-point]
68 | while(st>l)s.update(pos[--st],Node(v[st]));
| ^~~~
cake3.cpp: In function 'int main()':
cake3.cpp:92:40: warning: unused variable 'cnt' [-Wunused-variable]
92 | for(ll i=max(mid+m-1,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... |