XTUOJ 1205 Range
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的更多相关文章
- XTU 1205 Range
还是五月湘潭赛的题目,当时就是因为我坑...连个银牌都没拿到,擦. 这个题目枚举区间是不可能的,明显是要考虑每个数对全局的影响,即找到每个数最左和最右能满足是最大的位置 以及 最小的时候,相乘即为该数 ...
- SQL Server 合并复制遇到identity range check报错的解决
最近帮一个客户搭建跨洋的合并复制,由于数据库非常大,跨洋网络条件不稳定,因此只能通过备份初始化,在初始化完成后向海外订阅端插入数据时发现报出如下错误: Msg 548, Level 16, S ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- [LeetCode] Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [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 ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [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 ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
随机推荐
- MapReduce 的类型与格式【编写最简单的mapreduce】(1)
hadoop mapreduce 中的map 和reduce 函数遵循下面的形式 map: (K1, V1) → list(K2, V2) reduce: (K2, list(V2)) → list( ...
- glm编译错误问题解决 formal parameter with __declspec(align('16')) won't be aligned
參考:http://stackoverflow.com/questions/25300116/directxxmmatrix-error-c2719-declspecalign16-wont-be-a ...
- iOS数据持久化 -- Core Data
Core Data是一个功能强大的层,位于SQLite数据库之上,它避免了SQL的复杂性,能让我们以更自然的方式与数据库进行交互.Core Data将数据库行转换为OC对象(托管对象)来实现,这样无需 ...
- Intellij使用"easyexplore"
刚开始接触Intellij,里面有很多东西还不太会用,平时在eclipse里面用的很方便的easyexplore能帮助快速打开文件目录,Intellij中本身就有这样的功能,只是默认没有开启,需要我们 ...
- Spring控制反转容器的使用例子
详细代码如下: spring-config.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
- JS中的switch case
function GetDepartmentName(type) { switch (type) { case DepartMentQian: alert($('#DepartMentQian').v ...
- CF 965 B. Battleship
Arkady is playing Battleship. The rules of this game aren't really important.There is a field of n×n ...
- CF 986C AND Graph(建模+DFS)
#include<stdio.h> ],v[]; ],n,al; void dfs(int x){ if(v[x])return; v[x]=; if(ex[x])dfs(al^x); ; ...
- P2420 让我们异或吧(树链剖分)
题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中-xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...
- Unity Shader (四)片段程序示例
1.环境光+漫反射+高光+点光源 Shader "Custom/Example_Frag_1" { properties { _MainColor(,,,) _Specular ...