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 ...
随机推荐
- HDOJ 5087 Revenge of LIS II DP
DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- SPOJ 题目705 New Distinct Substrings(后缀数组,求不同的子串个数)
SUBST1 - New Distinct Substrings no tags Given a string, we need to find the total number of its di ...
- c++命名规范与代码风格
http://blog.sina.com.cn/s/blog_a3a8d0b1010100uw.html http://www.cnblogs.com/len3d/archive/2008/02/01 ...
- 安卓开发,adb shell 调试sqlite3数据库
安卓开发,adb shell 调试sqlite3数据库 在安卓中创建了sqlite3数据库,想要调试怎么办? 通过adb shell来进行查看. 第一步,将adb加入到系统变量中. 这样就可以在命令行 ...
- MDNS的漏洞报告——mdns的最大问题是允许广域网的mdns单播查询,这会暴露设备信息,或者被利用用于dns放大攻击
Vulnerability Note VU#550620 Multicast DNS (mDNS) implementations may respond to unicast queries ori ...
- java连接操作Oracle
package com.sp.test; import java.sql.*; import java.util.*; public class Text_lianxi extends Thread ...
- 【DNN引用包】
<%@ Register TagPrefix="dnn" TagName="address" Src="~/controls/address.a ...
- 文件IO流总结
文件在网络上或不同设备之间是怎么传输的,在Java程序中又是怎么来实现文件的传输,带着这两个问题,来了解一下Java中的IO流相关类及操作. 一.什么是流及流的用途 流是一组有顺序,有起点和终点的字节 ...
- Spring:dispatchservlet
DispatcherServlet 是 Spring MVC 中负责请求调度的核心引擎,所有的请求将由此 Servlet 根据配置分发至各个逻辑处理单元.其内部同时也维护了一个ApplicationC ...
- Mojo For Chromium Developers1
Mojo For Chromium Developers Overview This document contains the minimum amount of information neede ...