还是傻叉单调栈

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define ll long long
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x;
}
const int nmax=1e5+5;
const int inf=0x7f7f7f7f;
int a[nmax],l[nmax],R[nmax],q[nmax];
struct node{
ll sm;int x;
bool operator<(const node&rhs)const{
return x<rhs.x;}
};
node ns[nmax];
void solve(int x,int l,int r){
int mid,ans=r+1,t=r;
while(l<=r){
mid=(l+r)>>1;
if(ns[mid].x>=x) ans=mid,r=mid-1;
else l=mid+1;
}
printf("%lld\n",ns[t].sm-ns[ans-1].sm);
}
int main(){
int n=read();rep(i,1,n) a[i]=read();
int r=0;
rep(i,1,n){
while(r&&a[q[r]]<a[i]) --r;
l[i]=q[r]+1;q[++r]=i;
}
r=0;q[0]=n+1;
dwn(i,n,1){
while(r&&a[q[r]]<=a[i]) --r;
R[i]=q[r]-1;q[++r]=i;
}
rep(i,1,n) ns[i].sm=(ll)(R[i]-i+1)*(i-l[i]+1),ns[i].x=a[i];
sort(ns+1,ns+n+1);
rep(i,1,n) ns[i].sm+=ns[i-1].sm;
int t=read(),u,v,d,tmp,temp;
while(t--) u=read(),solve(u,1,n);
return 0;
}

  

基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
 收藏
 关注

有一天,小a给了小b一些数字,让小b帮忙找到其中最大的数,由于小b是一个程序猿,当然写了一个代码很快的解决了这个问题。

这时,邪恶的小c又出现了,他问小b,假如我只需要知道这些数字中的某个区间的最大值,你还能做嘛?

小b经过七七四十九天的思考,终于完美的解决了这道题目,这次,他想也让小c尝尝苦头,于是他问小c,我现在想知道存在多少不同的区间的最大值大于等于k,你还能做吗?

这次,小c犯了难,他来请教身为程序猿的你。

Hint:一个区间指al,al+1,…,ar这一段的数且l<=r,一个区间的最大值指max{al,al+1,…,ar},两个区间不同当且仅当[l1,r1],[l2,r2]中l1不等于l2或r1不等于r2

Input
第一行读入一个正整数n(1<=n<=100000),表示有n个数字。
接下来一行读入n个正整数ai(1<=ai<=100000)
接下来一行一个正整数Q(1<=Q<=100000),表示有Q组询问。
接下来Q行,每行一个正整数k(1<=k<=100000)
Output
Q行,每行一个正整数,表示存在多少区间大于等于k。
Input示例
3
1 2 3
3
1
2
3
Output示例
6
5
3

51nod1349 最大值的更多相关文章

  1. C语言 · 最大值与最小值计算

    输入11个整数,计算它们的最大值和最小值. 样例输入 0 1 2 3 4 5 6 7 8 9 10 样例输出 10 0   #include<stdio.h> int main(){ ]; ...

  2. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  3. [LeetCode] Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  4. 整理:Javascript获取数组中的最大值和最小值的方法汇总

    方法一: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 //最小值 Array.prototype.min = function ...

  5. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  6. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an ...

  7. 不需要sql进行计算数据的平均值、最大值、最小值、和

    介绍下SqlServer.前端js.后台C#三个阶段对均值.最大值.最小值.和计算int[] jisuan = {0, 1, 3, 5, 7,8 }; List<int> jisuan2 ...

  8. 在一个SQL Server表中的多个列找出最大值

    在一个SQL Server表中一行的多个列找出最大值 有时候我们需要从多个相同的列里(这些列的数据类型相同)找出最大的那个值,并显示 这里给出一个例子 IF (OBJECT_ID('tempdb..# ...

  9. C语言 · 寻找数组中的最大值

    问题描述 对于给定整数数组a[],寻找其中最大值,并返回下标. 输入格式 整数数组a[],数组元素个数小于1等于100.输出数据分作两行:第一行只有一个数,表示数组元素个数:第二行为数组的各个元素. ...

随机推荐

  1. MVC中 ViewBag、ViewData和TempData区别

    MVC3中 ViewBag.ViewData和TempData的使用和区别 public dynamic ViewBag { get; } public ViewDataDictionary View ...

  2. 【C语言】二维数组做形参

    二维数组有两种形式: ①在栈上:         int a[4][4] = {...}; ②在堆堆上:          int ** a = new int *[4];           for ...

  3. HDU 1217 Arbitrage (Floyd)

    Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...

  4. 全国DNS汇总

    全国DNS汇总 来路不明的DNS服务器可能导致你的帐号密码轻易被盗,请谨慎使用!在中国大陆,最科学的方法是将首选DNS服务器设置为114.114.114.114,备用DNS服务器设置为当地电信运营商的 ...

  5. (2)在vs2010上配置opengl

    参考自http://www.yakergong.net/nehe/ 在Visual Studio 2003 中创建基于Nehe SDK的应用程序分为以下几个步骤: 创建一个空白的工程文件 包含Nehe ...

  6. 内存分析_.Net垃圾回收介绍

    垃圾回收 1.       .Net垃圾回收中涉及的名称 1.1.什么是代? 垃圾回收器为了提升性能使用了代的机制,共分为三代(Gen0.Gen1.Gen2).GC工作机制基于以下假设, 1)  对象 ...

  7. ffmpeg转码时对编码率和固定码率的处理

    http://www.rosoo.net/a/201107/14663.html 一般fps在代码里这样表示 Fps = den/num 如果den = 15,num=1,则fps = 15. 如果帧 ...

  8. Java多线程-线程的调度(合并)

    线程的合并的含义就是将几个并行线程的线程合并为一个单线程执行,应用场景是当一个线程必须等待另一个线程执行完毕才能执行时可以使用join方法. join为非静态方法,定义如下:void join(): ...

  9. React组件生命周期-初始化阶段的函数执行顺序

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...

  10. 修改webapp底图

    从webapp目录可以看出地图归mapManager处理,所以在MapManager.js中找关于加载地图的方法, 很容易在里面找到showMap方法: 下面有另一个方法_showMap方法,查看定义 ...