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 ...
随机推荐
- Java transientkeyword使用小记
1. transient的作用及用法 我们都知道一个对象仅仅要实现了Serilizable接口,这个对象就能够被序列化,java的这样的序列化模式为开发人员提供了非常多便利.我们能够不必关系详细序列化 ...
- Android--Fragment与Activity通信
package com.example.testfragment; import com.example.testfragment.MainFargment.BackString; import an ...
- 继承—people
public class People { private double height;//身高 private double weight;//体重 public double getHeight( ...
- php八大设计模式之装饰器模式
我们都知道,得到一匹布需要大概这么几步: 1.染色 2.印花 3.裁剪 这种形式在面向对象中怎么实现呢? 面向过程[继承模式]实现: 继承模式得到需要的布料,一步一步的加工. 继承的层次越来越深,扩展 ...
- [POI2015]WIL-Wilcze doły(单调队列)
题意 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段连续区间,使得该区间内所有数字之和不超过p. (1<=d<=n< ...
- poj2728 Desert King(最小生成树+01分数规划=最优比率生成树)
题意 n个点完全图,每个边有两个权值,求分数规划要求的东西的最小值. (n<=1000) 题解 心态炸了. 堆优化primT了. 普通的就过了. 我再也不写prim了!!!! 咳咳 最优比率生成 ...
- Centos7:yum安装apache,编译安装php5.6,不解析php的解决方法
首先,说一下问题发生的场景: 因为懒,所以用 yum 安装 apache ,因为 centos 的源自带 php 5.4 不能符合环境要求,而不想用其他源,所以选择源码编译安装 php 5.6 安装完 ...
- P2420 让我们异或吧(树链剖分)
题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中-xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...
- 浅谈Python Web的五大框架
说到Web Framework,Ruby的世界Rails一统江湖,而Python则是一个百花齐放的世界.各种micro-framework.framework不可胜数. 尽管还有一大脚本语言PHP也有 ...
- ecnu 1244
SERCOI 近期设计了一种积木游戏.每一个游戏者有N块编号依次为1 ,2,-,N的长方体积木. 对于每块积木,它的三条不同的边分别称为"a边"."b边"和&q ...