题目链接:
Time Limit: 2000/1000 MS (Java/Others)
 Memory Limit: 262144/131072 K (Java/Others)
/************************************************
┆  ┏┓   ┏┓ ┆   
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆ 
┆┃   ┻   ┃ ┆
┆┗━┓    ┏━┛ ┆
┆  ┃    ┃  ┆      
┆  ┃    ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃           ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆      
************************************************ */ 
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
//#include <bits/stdc++.h>
#include <stack>
#include <map>
 
using namespace std;
 
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
 
typedef  long long LL;
 
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar());
    for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + ‘0‘);
    putchar(‘\n‘);
}
 
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=2e3+14;
const double eps=1e-12;
int a[N],b[N];
struct node
{
    int l,r,s;
}tr[4*N];
void build(int o,int L,int R)
{
    tr[o].l=L;tr[o].r=R;
    if(L>=R)
    {
        tr[o].s=b[L];
        return ;
    }
    int mid=(L+R)>>1;
    build(2*o,L,mid);
    build(2*o+1,mid+1,R);
    tr[o].s=max(tr[2*o].s,tr[2*o+1].s);
}
void update(int o,int x,int num)
{
    if(tr[o].l==tr[o].r&&tr[o].l==x)
    {
        tr[o].s=num;
        return ;
    }
    int mid=(tr[o].l+tr[o].r)>>1;
    if(x<=mid)update(2*o,x,num);
    else update(2*o+1,x,num);
    tr[o].s=max(tr[2*o+1].s,tr[2*o].s);
}
int query(int o,int L,int R)
{
    if(tr[o].l>=L&&tr[o].r<=R)return tr[o].s;
    int mid=(tr[o].r+tr[o].l)>>1;
    if(R<=mid)return query(2*o,L,R);
    else if(L>mid)return query(2*o+1,L,R);
    else return max(query(2*o,L,mid),query(2*o+1,mid+1,R));
}
int main()
{      
        int t;
        read(t);
        while(t--)
        {
            int n;
            read(n);
            For(i,1,n)read(a[i]);
            For(i,1,n-1)b[i]=abs(a[i+1]-a[i]);
            build(1,1,n-1);
            LL ans=0;
            For(i,1,n)
            {
                if(i==1)
                {
                    update(1,1,0);
                    ans=ans+query(1,1,n-1);
                    update(1,1,b[1]);
                }
                else if(i==n)
                {
                    update(1,n-1,0);
                    ans=ans+query(1,1,n-1);
                    update(1,n-1,b[n-1]);
                }
                else 
                {
                    update(1,i-1,0);
                    update(1,i,abs(a[i+1]-a[i-1]));
                    ans=ans+query(1,1,n-1);
                    update(1,i,b[i]);
                    update(1,i-1,b[i-1]);
                }
            }
            cout<<ans<<"\n";
        }      
        return 0;
}
hdu-5805 NanoApe Loves Sequence(线段树+概率期望)
原文:http://www.cnblogs.com/zhangchengc919/p/5745163.html