QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#543371#7523. Partially Free MealYoralenTL 1ms7884kbC++141.4kb2024-09-01 16:19:562024-09-01 16:19:59

Judging History

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

  • [2024-09-01 16:19:59]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:7884kb
  • [2024-09-01 16:19:56]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=200005,Lim=1e9;
const ll inf=1e16;
struct node{
	int x,y;
	bool operator <(node A)const{return y<A.y;}
}a[N];
struct Tree{ll s;int c,ls,rs;}t[N*40];
ll ans[N];
int n,tot,Rt;
void Add(int &u,int l,int r,int v,int c){
	if(!u)u=++tot;int mid((l+r)>>1);
	t[u].c+=c;t[u].s+=(ll)(c*v);
	if(l==r)return;
	if(v<=mid)Add(t[u].ls,l,mid,v,c);
	else Add(t[u].rs,mid+1,r,v,c);
}
ll Ask(int u,int l,int r,int k){
	int mid((l+r)>>1);
	if(k==0)return 0;
	if(l==r)return k*l;
	if(t[t[u].ls].c>k)return Ask(t[u].ls,l,mid,k);
	else return t[t[u].ls].s+Ask(t[u].rs,mid+1,r,k-t[t[u].ls].c);
}
void Deal(int l,int r,int L,int R){
	if(L>R){return;}
	//printf("%d %d\n",L,R);
	int mid((L+R)>>1),pos,i;ll res=inf;
	for(i=l;i<=r;i++){
		if(i>=mid){
		//	printf("!%d %d\n",i,t[Rt].c);
			ll v=a[i].y+a[i].x+Ask(Rt,1,Lim,mid-1);
			if(res>v){res=v,pos=i;}
		}
		Add(Rt,1,Lim,a[i].x,1);
	}
	ans[mid]=res;
	for(i=l;i<=r;i++)Add(Rt,1,Lim,a[i].x,-1);
	Deal(l,pos,L,mid-1);
	for(i=l;i<pos;i++)Add(Rt,1,Lim,a[i].x,1);
	Deal(pos,r,mid+1,R);
	for(i=l;i<pos;i++)Add(Rt,1,Lim,a[i].x,-1);
}
int main(){
	int i;scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+1+n);
	//for(i=1;i<=n;i++)printf("%d %d\n",a[i].x,a[i].y);
	
	Deal(1,n,1,n);
	for(i=1;i<=n;i++)printf("%lld\n",ans[i]); 
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: