QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#518366 | #7523. Partially Free Meal | KLPP# | TL | 0ms | 3660kb | C++17 | 2.1kb | 2024-08-13 19:53:02 | 2024-08-13 19:53:02 |
Judging History
answer
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long int lld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
int n;
vector<pair<lld,lld> >V;
vector<int> per;
ordered_set O;
int ptr;
class FT{
lld arr[1'000'000];
int N;
public:
void init(){
N=n;
rep(i,0,N+1)arr[i]=0;
}
void upd(int pos,lld val){
pos+=1;
for(;pos<=N;pos+=(pos&(-pos)))arr[pos]+=val;
}
lld query(int pos){
pos++;
lld ans=0;
for(;pos>0;pos-=(pos&(-pos))){
ans+=arr[pos];
}
return ans;
}
};
FT F;
void adv(){
ptr++;
O.insert(per[ptr]);
F.upd(per[ptr],V[ptr].second);
}
void ret(){
F.upd(per[ptr],-V[ptr].second);
O.erase(per[ptr]);
ptr--;
}
lld query(int k){
int x=*O.find_by_order(k-1);
return F.query(x);
}
vector<lld> res;
void divideandconquer(int l, int r, int optl,int optr){
if(l>r){
while(ptr<optr)adv();
assert(ptr==optr);
return;
}
//pointer at optl-1
int mid=(l+r)/2;
pair<lld,int> ans={1e18,-1};
for(int pos=optl;pos<=optr;pos++){
adv();
if(pos+1>=mid){
pair<lld,int> can={query(mid)+V[pos].first,mid};
ans=min(ans,can);
}
}
for(int pos=optl;pos<=optr;pos++){
ret();
}
//cout<<ans.first<<" "<<ans.second<<"\n";
res[mid]=ans.first;
int opt=ans.second;
divideandconquer(l,mid-1,optl,opt);
ret();
divideandconquer(mid+1,r,opt,optr);
assert(ptr==optr);
}
void solve(){
cin>>n;
F.init();
V.resize(n);
rep(i,0,n){
cin>>V[i].second>>V[i].first;
}
sort(V.begin(),V.end());
vector<pair<lld,lld>> X(n);
rep(i,0,n){
X[i].first=V[i].second;
X[i].second=i;
}
sort(X.begin(),X.end());
per.resize(n);
rep(i,0,n){
per[X[i].second]=i;
}
ptr=-1;
res.resize(n+1);
divideandconquer(1,n,0,n-1);
rep(i,1,n+1)cout<<res[i]<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
//cin>>tt;
while(tt--){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3660kb
input:
3 2 5 4 3 3 7
output:
7 11 16
result:
ok 3 lines
Test #2:
score: -100
Time Limit Exceeded
input:
200000 466436993 804989151 660995237 756645598 432103296 703610564 6889895 53276988 873617076 822481192 532911431 126844295 623111499 456772252 937464699 762157133 708503076 786039753 78556972 5436013 582960979 398984169 786333369 325119902 930705057 615928139 924915828 506145001 164984329 208212435...
output:
192284522 465420967 205645515 478779682 828875051 584812649 1321756978 1969208842 1559060466 2121195265 2768647129 2137300252 3340465826 3987917690 4327066230 4974518094 5793273016 5305948960 6969858475 7911863623 7129983474 7948738396 8890743544 4170096709 9491859694 10433864842 9015567317 99562244...