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

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

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

 1 #include <stack>
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #define pii pair<long long,long long>
 6 using namespace std;
 7 typedef long long LL;
 8 const int maxn = 100010;
 9 int d[maxn];
10 LL bgl[maxn],bgr[maxn],lel[maxn],ler[maxn];
11 stack< pii >stk;
12 int main(){
13     int T,n,cs = 1;
14     scanf("%d",&T);
15     while(T--){
16         scanf("%d",&n);
17         LL ret = (LL)n*(n+1)>>1;
18         for(int i = 0; i < n; ++i) scanf("%d",d+i);
19         memset(bgl,0,sizeof bgl);
20         memset(bgr,0,sizeof bgr);
21         memset(lel,0,sizeof lel);
22         memset(ler,0,sizeof ler);
23         while(!stk.empty()) stk.pop();
24         for(int i = 0; i < n; ++i){
25             pii now = pii(d[i],1);
26             while(!stk.empty() && stk.top().first >= d[i]){
27                 now.second += stk.top().second;
28                 stk.pop();
29             }
30             stk.push(now);
31             bgl[i] = now.second;
32         }
33         while(!stk.empty()) stk.pop();
34         for(int i = n-1; i >= 0; --i){
35             pii now = pii(d[i],1);
36             while(!stk.empty() && stk.top().first > d[i]){
37                 now.second += stk.top().second;
38                 stk.pop();
39             }
40             stk.push(now);
41             bgr[i] = now.second;
42         }
43         while(!stk.empty()) stk.pop();
44         for(int i = 0; i < n; ++i){
45             pii now = pii(d[i],1);
46             while(!stk.empty() && stk.top().first <= d[i]){
47                 now.second += stk.top().second;
48                 stk.pop();
49             }
50             stk.push(now);
51             lel[i] = now.second;
52         }
53         while(!stk.empty()) stk.pop();
54         for(int i = n-1; i >= 0; --i){
55             pii now = pii(d[i],1);
56             while(!stk.empty() && stk.top().first < d[i]){
57                 now.second += stk.top().second;
58                 stk.pop();
59             }
60             ler[i] = now.second;
61             stk.push(now);
62         }
63         for(int i = 0; i < n; ++i){
64             ret += bgl[i]*bgr[i]*-d[i];
65             ret += lel[i]*ler[i]*d[i];
66         }
67         printf("Case %d: %I64d\n",cs++,ret);
68     }
69     return 0;
70 }

时间: 2024-12-22 09:19:25

XTUOJ 1205 Range的相关文章

2014湘潭邀请赛 C题 湘大OJ 1205 Range (单调栈)

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 ran

XTU 1205 Range

还是五月湘潭赛的题目,当时就是因为我坑...连个银牌都没拿到,擦. 这个题目枚举区间是不可能的,明显是要考虑每个数对全局的影响,即找到每个数最左和最右能满足是最大的位置 以及 最小的时候,相乘即为该数字影响的区间总数.当时想到的是用线段树,建树的时候求出最大和最小值,然后在每个数往里面搜索,比赛的时候敲挫了,那个时候真的线段树写的很挫,而且没考虑过一个问题,就是相同的时候怎么办,按刚刚的算法,会算重复的,所以一个好的方法是如果有相同的,往左搜的时候搜到等于该数值的时候停止,往右搜的时候搜到大于该

range()用法

来源:http://www.cnblogs.com/wangwp/p/4535299.html 例子:http://www.cnblogs.com/hongten/p/hongten_python_range.html 函数原型:range(start, end, scan): 参数含义:start:计数从start开始.默认是从0开始.例如range(5)等价于range(0, 5); end:技术到end结束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有

Leetcode 34. Search for a Range

34. Search for a Range Total Accepted: 91570 Total Submissions: 308037 Difficulty: Medium Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(l

Swift 中的Range和NSRange不同

Swift中的Ranges和Objective-C中的NSRange有很大的不同,我发现在处理Swift中Ranges相关的问题的时候,总是要花费比我想象的更多的时间.不过,现在回过头来看看,发现Swift中的Ranges的使用还是比较合理的,但是想要正确的使用Ranges真的需要一些特别的技巧. 看一个例子,下面这段代码展示的是截取以指定的字符开头和以指定的字符结尾的子字符串: ? 1 2 3 4 5 6 var str = "Hello, playground"   let ran

swift -- 定义空字符串 hasPrefix hasSuffix trim split join range

// 定义空的字符串 var str1 = "" var str2 = String() str1.isEmpty      // 判断字符串是否为空 // 输出字符串中所有的字符 var str3 = "As god name" for c in str3{ println(c) } Int.max   // Int类型的最大值 Int.min   // Int类型的最小值 var arr1 = ["c", "oc", &q

qvalue: Check that you have valid p-values or use a different range of lambda

ERROR: The estimated pi0 <= 0. Check that you have valid p-values or use a different range of lambda. 重现错误的代码: ps <- runif(3e5)library(qvalue)ps <- ps[ps < 0.75]qs <- qvalue(ps) Error in pi0est(p, ...) :  ERROR: The estimated pi0 <= 0. C

SAP程序代码中RANGE表的用法注意点

前几天写了个程序,在读SQL代码的时候,选择条件 in 一张range table,结果导致程序DUMP,SAP的LOG如下: 错误原因:RANGE表当用于WHERE条件是,只限较小的数据量的情况(约2000条左右): 若为大数据量应该用FOR ALL ENTRIES IN的语法,或者其它方式来改写.否则会DUMP

[LeetCode]Count of Range Sum

题目:Count of Range Sum Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive. Note:A naive algorithm