QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#518425#7523. Partially Free MealKLPP#TL 1ms5744kbC++173.0kb2024-08-13 20:26:062024-08-13 20:26:06

Judging History

你现在查看的是最新测评结果

  • [2024-08-13 20:26:06]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:5744kb
  • [2024-08-13 20:26:06]
  • 提交

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;

class FT2{
	int arr[1'000'000];
	int N;
	public:
	void init(){
		N=n;
		rep(i,0,N+1)arr[i]=0;
	}
	void upd(int pos,int val){
		pos+=1;
		for(;pos<=N;pos+=(pos&(-pos)))arr[pos]+=val;
	}
	int query(int k){
		//cout<<k<<"\n";
		//rep(i,0,N+1)cout<<arr[i]<<" ";
		//cout<<endl;
		int pot=(1<<30);
		int pos=0;
		int cur=k;
		while(pot>0){
			if(pot+pos<=N){
				int nxt=cur-arr[pot+pos];
				if(nxt>=0){
					cur=nxt;
					pos+=pot;
				}
			}
			pot/=2;
		}
		return pos;
	}
};
FT2 F2;

void adv(){
	ptr++;
	O.insert(per[ptr]);
	//cout<<"+ "<<per[ptr]<<" "<<V[ptr].second<<endl;
	//F2.upd(per[ptr],1);
	F.upd(per[ptr],V[ptr].second);
}
void ret(){
	//cout<<"- "<<per[ptr]<<" "<<V[ptr].second<<endl;
	F.upd(per[ptr],-V[ptr].second);
	O.erase(per[ptr]);
	//F2.upd(per[ptr],-1);
	ptr--;
}
lld query(int k){
	int x=*O.find_by_order(k-1);
	//int x=F2.query(k-1);
	//cout<<k<<" "<<x<<" "<<F.query(x)<<endl;
	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();
		assert(ptr==pos);
		if(pos+1>=mid){
			//cout<<query(mid)<<" X "<<V[pos].first<<endl;
			pair<lld,int> can={query(mid)+V[pos].first,pos};
			ans=min(ans,can);
		}
	}
	
	for(int pos=optl;pos<=optr;pos++){
		ret();
	}
	//cout<<ans.first<<" F "<<ans.second<<" F "<<mid<<"\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();
	F2.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);
	lld sum=0;
	//rep(i,0,10){
		//sum+=V[i].second;
	//}cout<<sum<<"\n";
	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();
	}
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5744kb

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:


result: