QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#546066 | #7523. Partially Free Meal | Liangsheng298 | RE | 0ms | 0kb | C++14 | 2.2kb | 2024-09-03 19:25:42 | 2024-09-03 19:25:44 |
answer
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read(){
T x=0;char ch=getchar();bool fl=false;
while(!isdigit(ch)){if(ch=='-')fl=true;ch=getchar();}
while(isdigit(ch)){
x=(x<<3)+(x<<1)+(ch^48);ch=getchar();
}
return fl?-x:x;
}
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define LL long long
#define read() read<int>()
const int maxn = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
int cnt;
struct tree{
int ls,rs,sz;
LL val;
}t[maxn<<5];
#define mid ((l+r)>>1)
void build(int &p,int l,int r){
p=++cnt;
if(l==r)return ;
build(t[p].ls,l,mid);
build(t[p].rs,mid+1,r);
}
void update(int &p,int pre,int l,int r,int pos,LL val){
p=++cnt;
t[p].ls=t[pre].ls;t[p].rs=t[pre].rs;
t[p].sz=t[pre].sz+1;
t[p].val=t[pre].val+val;
if(l==r)return ;
if(pos<=mid)update(t[p].ls,t[pre].ls,l,mid,pos,val);
else update(t[p].rs,t[pre].rs,mid+1,r,pos,val);
}
LL query(int p,int l,int r,int k){
if(t[p].sz<k)return INF;
if(!k)return 0;
if(l==r)return t[p].val;
if(t[t[p].ls].sz>=k)return query(t[p].ls,l,r,k);
LL res=t[t[p].ls].val;
res+=query(t[p].rs,mid+1,r,k-t[t[p].ls].sz);
return res;
}
int rt[maxn],n;
LL f[maxn],b[maxn],tot,mp[maxn];
pair<LL,LL> a[maxn];
inline LL w(int k,int x){
return a[x].first+mp[a[x].second]+query(rt[x-1],1,tot,k-1);
}
void DP(int l,int r,int k_l,int k_r){
assert(k_l>k_r);
int k=max(k_l,mid);
for(int i=max(k_l,mid);i<=k_r;i++)if(w(mid,i)<w(mid,k))k=i;
f[mid]=w(mid,k);
if(l<mid)DP(l,mid-1,k_l,k);
if(r>mid)DP(mid+1,r,k,k_r);
}
int main(){
FASTIO;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i].second>>a[i].first;
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)b[i]=a[i].second;
sort(b+1,b+1+n);
tot=unique(b+1,b+1+n)-(b+1);
build(rt[0],1,tot);
for(int i=1;i<=n;i++){
int pos=lower_bound(b+1,b+1+tot,a[i].second)-b;
mp[pos]=a[i].second;
a[i].second=pos;
update(rt[i],rt[i-1],1,tot,a[i].second,mp[a[i].second]);
}
DP(1,n,1,n);
for(int i=1;i<=n;i++)cout<<f[i]<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
3 2 5 4 3 3 7