Codeforces Round #343 (Div. 2) 629D. Babaei and Birthday Cake 线段树

题意:给你一些蛋糕的高度和半径,蛋糕是圆柱体的。每个蛋糕有编号1~n。一个人要放置一些蛋糕遵循以下规则,
1.i只剩放在i之前的蛋糕的下边
2.只能体积大的蛋糕放在体积小的蛋糕下面

思路:就是最大上升子序列 类似HDU 1087 不过数据比较大
之前我们是通过双重for来跑,但是数据大的时候只能用线段树了
先离散化成节点,然后就是普通线段树XXXX….

心情无比sad 比赛结束后四分钟才敲完,测完了交了一发A了 Sad

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
#define maxn 100005
#define f(x) (x*1.0)
#define inf 0x3f3f3f3f
#define maxm maxn*maxn
#define lowbit(x) (x&(-x))
#define cheak(i) printf("%d ",i)
#define lson(x) (splay[x].son[0])
#define rson(x) (splay[x].son[1])
#define rfor(i,a,b) for(i=a;i<=b;++i)
#define lfor(i,a,b) for(i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define mec(a,b) memcpy(a,b,sizeof(b))
const double PAI=acos(-1);
LL A[maxn],B[maxn];
LL dp[maxn];
LL tree[maxn*4],lazy[maxn*4];
map<LL,LL>Map;
void Push_up(int index)
{
    tree[index]=max(tree[index*2],tree[index*2+1]);
}
void Push_down(int index)
{
    if(lazy[index]!=-1)
    {
        tree[index*2]=tree[index*2+1]=tree[index];
        lazy[index]=-1;
        lazy[index*2]=lazy[index*2+1]=1;
    }
}
void build(int l,int r,int index)
{
    lazy[index]=-1;
    if(l==r)
    {
        tree[l]=0;
        return ;
    }
    int mid=(l+r)/2;
    build(l,mid,index*2);
    build(mid+1,r,index*2+1);
    Push_up(index);
}
void insert(int l,int r,int x,LL val,int index)
{
    if(l==x&&r==x)
    {
        if(tree[index]<val)
        {
            tree[index]=val;
            lazy[index]=1;
        }
        return ;
    }
    Push_down(index);
    int mid=(l+r)/2;
    if(x<=mid) insert(l,mid,x,val,index*2);
    else insert(mid+1,r,x,val,index*2+1);
    Push_up(index);
}
LL query(int l,int r,int left,int right,int index)
{
    if(right<left) return 0;
    //printf("****%d %d %d\n",l,r,index);
    if(left<=l&&r<=right) return tree[index];
    Push_down(index);
    int mid=(l+r)/2;
    LL ans=0;
    if(left<=mid) ans=query(l,mid,left,right,index*2);
    if(right>mid) ans=max(ans,query(mid+1,r,left,right,index*2+1));
    Push_up(index);
    return ans;
}
int main( )
{
    int i,j,n;
    LL r,h;
    scanf("%d",&n);
    rfor(i,1,n)
    {
        scanf("%lld%lld",&r,&h);
        B[i]=dp[i]=A[i]=r*r*h;
    }
    sort(B+1,B+n+1);
    int k=1;
    Map[B[1]]=1;
    rfor(i,2,n)
    if(B[i]!=B[i-1])
    {
        B[++k]=B[i];
        Map[B[k]]=k;
    }
    build(1,k,1); 
    LL ans=0;
    rfor(i,1,n)
    {
        dp[i]+=query(1,k,1,Map[A[i]]-1,1);
        //printf("&&%lld\n",query(1,k,1,Map[A[i]]-1,1));
        insert(1,k,Map[A[i]],dp[i],1);
        ans=max(dp[i],ans);
    }
    //rfor(i,1,n) printf("%lld ",dp[i]);printf("\n");
    printf("%.9lf\n",ans*PAI);
    return 0;
}

你可能感兴趣的:(codeforces)