Range

Time Limit : 1000 MS Memory Limit : 65536 KB

Problem Description

For an array, the range function is defined below: Range(A)=Max(A)-Min(A)+1; For example, suppose A={1,2,3,4,5}, then Range(A)=5-1+1=5. Now, given an array A(length≤100000), you are going to calcalute the sum of all subarray's range. i.e sigma(i,j){Range(A[i,j])}.

Input

First line contain an integer T, there are T(1≤T≤100) cases. For each case T. The length N(1≤N≤100000), and N integers A[i](1≤A[i]≤109).

Output

Output case number first, then the answer.

Sample Input

1
5
1 2 3 4 5

Sample Output

Case 1: 35

Source

daizhenyang

解题:单调栈求出以该元素为最小元素的区间个数和以该元素为最大元素的区间个数

注意存在相同的元素的情况

 #include <stack>
#include <iostream>
#include <cstdio>
#include <cstring>
#define pii pair<long long,long long>
using namespace std;
typedef long long LL;
const int maxn = ;
int d[maxn];
LL bgl[maxn],bgr[maxn],lel[maxn],ler[maxn];
stack< pii >stk;
int main(){
int T,n,cs = ;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
LL ret = (LL)n*(n+)>>;
for(int i = ; i < n; ++i) scanf("%d",d+i);
memset(bgl,,sizeof bgl);
memset(bgr,,sizeof bgr);
memset(lel,,sizeof lel);
memset(ler,,sizeof ler);
while(!stk.empty()) stk.pop();
for(int i = ; i < n; ++i){
pii now = pii(d[i],);
while(!stk.empty() && stk.top().first >= d[i]){
now.second += stk.top().second;
stk.pop();
}
stk.push(now);
bgl[i] = now.second;
}
while(!stk.empty()) stk.pop();
for(int i = n-; i >= ; --i){
pii now = pii(d[i],);
while(!stk.empty() && stk.top().first > d[i]){
now.second += stk.top().second;
stk.pop();
}
stk.push(now);
bgr[i] = now.second;
}
while(!stk.empty()) stk.pop();
for(int i = ; i < n; ++i){
pii now = pii(d[i],);
while(!stk.empty() && stk.top().first <= d[i]){
now.second += stk.top().second;
stk.pop();
}
stk.push(now);
lel[i] = now.second;
}
while(!stk.empty()) stk.pop();
for(int i = n-; i >= ; --i){
pii now = pii(d[i],);
while(!stk.empty() && stk.top().first < d[i]){
now.second += stk.top().second;
stk.pop();
}
ler[i] = now.second;
stk.push(now);
}
for(int i = ; i < n; ++i){
ret += bgl[i]*bgr[i]*-d[i];
ret += lel[i]*ler[i]*d[i];
}
printf("Case %d: %I64d\n",cs++,ret);
}
return ;
}

XTUOJ 1205 Range的更多相关文章

  1. XTU 1205 Range

    还是五月湘潭赛的题目,当时就是因为我坑...连个银牌都没拿到,擦. 这个题目枚举区间是不可能的,明显是要考虑每个数对全局的影响,即找到每个数最左和最右能满足是最大的位置 以及 最小的时候,相乘即为该数 ...

  2. SQL Server 合并复制遇到identity range check报错的解决

        最近帮一个客户搭建跨洋的合并复制,由于数据库非常大,跨洋网络条件不稳定,因此只能通过备份初始化,在初始化完成后向海外订阅端插入数据时发现报出如下错误: Msg 548, Level 16, S ...

  3. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  4. [LeetCode] Range Addition 范围相加

    Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...

  5. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  6. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  7. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  8. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  9. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

随机推荐

  1. MySql基础总结(1)

    对于MySql已经是第二次看了,这次基本的目的是将MySql系统的归纳一边,之前学MySql是将笔记写在本子上,以后不想总带着本子,所以再花点时间把笔记归纳在博客中. 下午已将安装步骤具体的写了.如今 ...

  2. [android]DES/3DES/AES加密方式

    DES 支持8位加密解密,3Des支持24位,Aes支持32位.3Des是Des算法做三次.位数的单位是字节byte.不是bits. 3Des是把24位分成3组.第一组八位用来加密,第二组8位用于解密 ...

  3. Anaconda升级

    Anaconda是可以进行升级的, 这样就省的重装一遍python全家桶了, 比如:   conda update conda conda install anaconda=2018.12   就可以 ...

  4. 在shell脚本中使用代理

    设置所有的代理走socks5 export ALL_PROXY="socks5://127.0.0.1:1080" 取消代理 unset ALL_PROXY  

  5. Kali linux 2016.2(Rolling)中metasploit的搜集特定网站的目录结构

    不多说,直接上干货! parent directory site: testfire.net 至于这里怎么FQ,很简单,请见我下面的博客! kali 2.0安装 lantern(成功FQ) shado ...

  6. 机器学习规则:ML工程最佳实践----rules_of_ml section 1【翻译】

    作者:黄永刚 机器学习规则:ML工程最佳实践 本文旨在指引具有机器学习基础知识的工程师等人,更好的从机器学习的实践中收益.介绍一些应用机器学习需要遵循的规则,类似于Google C++ 风格指南等流行 ...

  7. Spring MVC 核心架构图

    架构图对应的DispatcherServlet核心代码如下: //前端控制器分派方法 protected void doDispatch(HttpServletRequest request, Htt ...

  8. vuejs helloworld

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. HDU 5214 Movie【贪心】

    题意:给出n,l[1],r[1],a,b,c,d 其中 l[i]=l[i-1]*a+b,r[i]=r[i-1]*c+d; 问能否从这n个区间中选出不相交不重叠的三个区间 看的题解 先考虑最左边的区间, ...

  10. .net Web获取域用户账号

    HttpContext.Current.Request.LogonUserIdentity.Name //可以获取出域账号 HttpContext.Current.Request.LogonUserI ...