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. iOS RegexKitLite 提取匹配的内容

            使用RegexKitLite正则表达式需要以下工作: 1.RegexKitLite官方网址(内含使用教程):http://regexkit.sourceforge.net/RegexK ...

  2. sublime搜索和替换--多文件搜索替换

    Search and Replace - Multiple Files Searching To open the search panel for files, press Ctrl + Shift ...

  3. oracle新手随记10

    1. unpivot注意点:select new_col from (select ename,job,to_char(sal) as sal,null as c from emp)         ...

  4. jsp登录会话

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. 使用Visual Studio2012调试Redis源码

    Redis是一款C语言编写Key-Value存储系统,基于BSD协议开放源码,其源码托管在github上,大概有三万行. 源码地址:https://github.com/antirez/redis 源 ...

  6. 把asp:CheckBoxList 变成单选框

    单选框代码 <asp:CheckBoxList runat="server" RepeatDirection="Horizontal" ID=" ...

  7. 添加本地 yum源

    添加本地 yum源 yum-updatesd.conf yum的主配置文件   [root@zhou ~]# cd /etc/yum.repos.d/ [root@zhou yum.repos.d]# ...

  8. DedeCMS让channelartlist支持currentstyle属性

    dedecms默认模板的channelartlist是不支持currentstyle属性的.currentstyle属性在导航中应用的比较多,可以实现循环调用栏目后,当前页<li>标签获得 ...

  9. POJ-2142 The Balance 扩展欧几里德(+绝对值和最小化)

    题目链接:https://cn.vjudge.net/problem/POJ-2142 题意 自己看题吧,懒得解释 思路 第一部分就是扩展欧几里德 接下来是根据 $ x=x_0+kb', y=y_0- ...

  10. LNMP安装部署开源IP管理工具phpipam

    1.数据库 mariadb 安装 //依赖安装 yum install -y apr* autoconf automake bison bzip2 bzip2* compat* \ cpp curl ...