Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(离散化+线段树) - dblank

Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(离散化+线段树)

D. Babaei and Birthday Cake
time limit per test

2 seconds

memory limit per test

256 megabytes


standard input


standard output

As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.

Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.

However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.

Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value.


The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has.

Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake.


Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

100 30
40 10
1 1
9 7
1 4
10 7


In first sample, the optimal way is to choose the cake number 1.

In second sample, the way to get the maximum volume is to use cakes with indices 12 and 4.


思路:先把体积离散化,对于每个蛋糕,可以知道肯定是放在它前面比它小的最大体积额的蛋糕上,这样可以保证以i结尾的和最大,于是dp[i]表示以i结尾的最大蛋糕体积和,线段树每次查询离散化后的区间[1, num[i]-1]的最大值,就是找到那个比它小最大的,然后加上去就可以了,更新线段树,最后过一遍就可以得到ans。

using namespace std;  
const double eps = 1e-10;  
const int inf = 0x3f3f3f3f, N = 1e5 +10;  
typedef long long ll;  
const double pi = acos(-1);  
using namespace std;  
struct Node  
    double r, h;  
} p[N];  
double V(Node a)  
    return a.r*a.r*a.h*pi;  
double dp[N], now[N], v[N], f[N];  
int num[N];  
struct node  
    int l, r;  
    double maxn;  
} s[100010 * 4];  
int n, m;  
void pushup(int cnt)  
    s[cnt].maxn = max(s[cnt<<1].maxn, s[cnt<<1|1].maxn);  
void build_tree(int l, int r,int cnt)  
    s[cnt].l = l;  
    s[cnt].r = r;  
    if(l == r)  
        s[cnt].maxn = 0.0;  
        return ;  
    int mid = (l + r)>>1;  
    build_tree(l, mid, cnt*2);  
    build_tree(mid+1, r, cnt*2+1);  
void update(int x, double numx, int cnt)  
    if(s[cnt].l == x && s[cnt].r == x)  
        s[cnt].maxn = max(s[cnt].maxn, numx);  
        return ;  
    int mid = (s[cnt].l + s[cnt].r)>>1;  
    if(x<=mid) update(x, numx, cnt*2);  
    if(x>mid) update(x, numx, cnt*2+1);  
double query(int l , int r, int cnt)  
    if(s[cnt].l >= l && s[cnt].r <= r)  
        return s[cnt].maxn;  
    int mid = (s[cnt].l + s[cnt].r)>>1;  
    double res = 0.0;  
        res = max(query(l, r, cnt*2), res);  
    if(r>mid) res = max(query(l, r, cnt*2+1), res);  
    return res;  
int main()  
    int n;  
        build_tree(1, n, 1);  
        memset(dp, 0, sizeof(dp));  
        for(int i = 0; i<n; i++)  
            scanf("%lf%lf", &p[i].r, &p[i].h);  
            v[i] = V(p[i]);  
            f[i] = v[i];  
        sort(f, f+n);  
        for(int i = 0; i<n; i++)  
            num[i] = lower_bound(f , f+n, v[i]) - f + 1;  
        for(int i = 0; i<n; i++)  
                dp[i] = v[i];  
            else dp[i] = query(1, num[i]-1, 1) + v[i];  
            update(num[i], dp[i], 1);  
        double ans = 0.0;  
        for(int i = 0; i<n; i++)  
            ans = max(dp[i], ans);  
        printf("%.10f\n", ans);  
    return 0;