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

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
2
100 30
40 10

output
942477.796077000

input
4
1 1
9 7
1 4
10 7

output
3983.539484752


Note

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.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
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);
pushup(cnt);
}
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);
pushup(cnt);
}
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;
if(l<=mid)
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;
while(cin>>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++)
{
if(num[i]-1==0)
dp[i] = v[i];
else dp[i] = query(1, num[i]-1, 1) + v[i];
//cout<<dp[i]<<endl;
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;
}