# Minimum Sum(二分+RMQ查找下标) AtCoder Grand Contest 005

## 题目描述

One day, Snuke was given a permutation of length N, a1,a2,…,aN, from his friend.

Find the following:

Constraints
1≤N≤200,000
(a1,a2,…,aN) is a permutation of (1,2,…,N).

## 输入

The input is given from Standard Input in the following format:
N
a1 a2 … aN

## 输出

Note that the answer may not fit into a 32-bit integer.

## 样例输入

3
2 1 3


## 样例输出

9

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
ll a[200105];
ll sum=0;
int d[200105][35],n;

void init()
{
int i,j;
for(i=0;i<n;i++)
d[i][0]=i;
for(j=1;(1<<j)<=n;j++)
for(i=0;i+(1<<j)-1<n;i++)
d[i][j]=a[d[i][j-1]] < a[d[i+(1<<(j-1))][j-1]]? d[i][j-1]:d[i+(1<<(j-1))][j-1];
}
int RMQ_indx_min(int s,int v)
{
int k=(int)(log((v-s+1)*1.0)/log(2.0));
return a[d[s][k]]<a[d[v-(1<<k)+1][k]]? d[s][k]:d[v-(1<<k)+1][k];
}

void dfs(int l,int r)
{
if(l==r){
sum+=(ll)a[l];
return ;
}
int minn=RMQ_indx_min(l,r);
sum=sum+(ll)(minn-l+1)*(r-minn+1)*a[minn];
if(l<=minn-1)
dfs(l,minn-1);
if(r>=minn+1)
dfs(minn+1,r);
}
int main (){
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lld",&a[i]);
init();
dfs(0,n-1);
printf("%lld\n",sum);
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120