单调栈练习题

· · 题解

明显是单调栈的题目嘛

#include<bits/stdc++.h>
using namespace std;
typedef long long b64;
b64 stk[300010], top;
b64 h[300010];
int main()
{
  b64 n, i, ans=0;
  cin>>n;
  for(i=1;i<=n;i++)
  {
    cin>>h[i];
    while(top&&h[stk[top]]<h[i])
    {
      ans = ans + i - stk[top] + 1;
      top--;
    }
    if(top) ans = ans + i - stk[top] + 1;
    stk[++top] = i;
  }
  cout<<ans;
  return 0;
}