Description

Mirko is hungry as a bear, scratch that, programmer and has stumbled upon a local restaurant. The restaurant offers N meals and has an interesting pricing policy: each meal i

has two assigned prices, Ai and Bi . Mirko pays A only for the first ordered meal, while B prices apply for all other meals.

Mikro can't decide how many meals to order. In order to make his decision easier, he has asked you to compute, for each k between 1 i N (inclusive), the minimum total price

for k ordered meals. Mirko doesn't care which particular meals he orders or in which order he orders them, however he won't order the same meal twice. Order, order,

order.

Input

The first line of input contains the positive integer N (2 ≤ N ≤ 500 000), the number of different meals offered by the restaurant. Each of the following N lines contains two

positive integers, Ai and Bi (1 ≤ Ai , Bi ≤ 1 000 000 000), the prices for meal i as described above.

Output

Output must consist of N lines, where line k contains the minimum price for ordering exactly k different meals.

Sample Input


3
10 5
9 3
10 5

2
100 1
1 100

5
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000

Sample Output


9
13
18

1
2

1000000000
2000000000
3000000000
4000000000
5000000000

Hint

Clarification of the first example:

k = 1: Mirko pays A2 = 9 for the starting meal 2.

k = 2: Mirko pays A1 = 10 for the starting meal 1, then B2 = 3 for meal 2.

k = 3: Mirko pays A1 = 10 for the starting meal 1, then B2 = 3 for meal 2, and finally B3= 5 for meal 3.

题意:有n道菜,每次选择吃菜的时候,第一道菜的价值是a[i],之后每一道选的菜的价值为b[i],问选择k道菜,最少花的价钱是多少。

思路:题解的思路很巧妙,因为要使得选k道菜的价钱最少,只有两种可能,一种是选择前k道b[i]价值最少的,然后把这k道菜中其中的一道菜的价值变为a[i];另一种是先选择前k-1道菜,然后在不属于这k-1道菜中的其他的菜选择一道a[i]价值最少的,作为第1道菜。前一种可以用set做,后一种用线段树做,然后取小就行了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 500050
ll sum[maxn];
set<ll>myset;
set<ll>::iterator it;
ll ans[maxn];
struct edge{
ll x,y;
}a[maxn]; bool cmp(edge a,edge b){
if(a.y==b.y)return a.x<b.x;
return a.y<b.y;
}
struct node{
int l,r,minx;
}b[4*maxn];
void build(int l,int r,int th)
{
int mid;
b[th].l=l;b[th].r=r;
if(l==r){
b[th].minx=a[l].x;
return;
}
mid=(l+r)/2;
build(l,mid,th*2);
build(mid+1,r,th*2+1);
b[th].minx=min(b[th*2].minx,b[th*2+1].minx);
}
int question(int l,int r,int th)
{
int mid;
if(b[th].l==l && b[th].r==r){
return b[th].minx;
}
mid=(b[th].l+b[th].r)/2;
if(r<=mid)return question(l,r,th*2);
else if(l>mid)return question(l,r,th*2+1);
else{
return min(question(l,mid,th*2),question(mid+1,r,th*2+1) );
}
}
int main()
{
int n,m,i,j,k;
while(scanf("%d",&n)!=EOF)
{
sum[0]=0;
for(i=1;i<=n;i++){
scanf("%lld%lld",&a[i].x,&a[i].y);
}
sort(a+1,a+1+n,cmp);
build(1,n,1);
myset.clear();
for(k=1;k<=n;k++){
ans[k]=sum[k-1]+question(k,n,1);
myset.insert(a[k].x-a[k].y);
sum[k]=sum[k-1]+a[k].y;
ans[k]=min(ans[k],sum[k]+(*myset.begin()) );
printf("%lld\n",ans[k]);
}
}
return 0;
}

zjnu1762 U (线段树)的更多相关文章

  1. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

  2. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  3. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  4. codevs 1080 线段树点修改

    先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...

  5. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  6. PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树

    #44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...

  7. CF719E(线段树+矩阵快速幂)

    题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...

  8. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

  9. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

随机推荐

  1. 【SpringBoot1.x】SpringBoot1.x 日志

    SpringBoot1.x 日志 日志框架 市面上有很多日志框架,一个日志框架一般包括抽象层和实现. SpringBoot,它的底层是 Spring,而 Spring 框架默认是用 JCL(java. ...

  2. 【ORA】ORA-00257 archiver error. 错误的处理方法

    今天连接数据库,结果报错,ora-00257查看 [oracle@exam oracle]$ oerr ora 00257 00257, 00000, "archiver error. Co ...

  3. poj-Decoding Morse Sequences(动态规划)

    Description Before the digital age, the most common "binary" code for radio communication ...

  4. python optparse模块的用法

    引用原博主文章链接: https://www.cnblogs.com/darkpig/p/5717902.html

  5. DOS的FOR命令用法总结

    鉴于dos自带的关于for命令的帮助信息看起来太简单,自己总结了一下,并增加了必要的实例,以备日后自己查阅.其中一些地方可能存在理解错误,敬请指出. [转发请注明出处]

  6. merge join pg伪代码

    Join { get initial outer and inner tuples INITIALIZE do forever { while (outer != inner) { SKIP_TEST ...

  7. [从源码学设计]蚂蚁金服SOFARegistry之配置信息

    [从源码学设计]蚂蚁金服SOFARegistry之配置信息 目录 [从源码学设计]蚂蚁金服SOFARegistry之配置信息 0x00 摘要 0x01 业务范畴 1.1 配置作用 1.2 学习方向 0 ...

  8. (02)-Python3之--列表(list)操作

    1.定义 列表的关键字:list 列表以[]括起来,数据之间用 , 隔开.列表当中的数据,可以是任意类型.数值是可以重复的. 列表元素是 可变的,顺序是 有序的. 例如: b = ["萝卜& ...

  9. (012)每日SQL学习:TO_CHAR(DATE,FORMAT)

    SYSDATE 2009-6-16 15:25:10 TRUNC(SYSDATE) 2009-6-16 TO_CHAR(SYSDATE,'YYYYMMDD') 20090616 到日 TO_CHAR( ...

  10. 一个关于ExecutorService shutdownNow时很奇怪的现象

    我们知道很多类库中的阻塞方法在抛出InterruptedException后会清除线程的中断状态(例如 sleep. 阻塞队列的take),但是今天却发现了一个特别奇怪的现象,先给出代码: publi ...