fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie()
  4.  
  5.  
  6.  
  7. using namespace std;
  8. typedef long long ll;
  9.  
  10.  
  11. void File(){
  12. #ifndef ONLINE_JUDGE
  13. freopen("input.txt", "r", stdin);
  14. freopen("output.txt", "w", stdout);
  15. #endif
  16. }
  17. ll largestRectangleArea(vector <ll> heights) {
  18. stack<int> st;
  19.  
  20. ll area,mxarea=-1,top;
  21. int i=0,n=heights.size();
  22.  
  23. while(i<n){
  24. if(st.empty() || heights[i]>=heights[st.top()]){
  25. st.push(i++);
  26. }else{
  27.  
  28. top=st.top();
  29. st.pop();
  30.  
  31. area=heights[top]*(st.empty()?i:i-st.top()-1);
  32.  
  33. mxarea=max(mxarea,area);
  34.  
  35. }
  36. }
  37. while(!st.empty()){
  38. top=st.top();
  39. st.pop();
  40. area=heights[top]*(st.empty()?i:i-st.top()-1);
  41. mxarea=max(mxarea,area);
  42. }
  43. return mxarea;
  44. }
  45. int solve() {
  46. int n,i=0;
  47. cin>>n;
  48. vector<ll> v(n);
  49. for(int i=0;i<n;i++){
  50. cin>>v[i];
  51. }
  52. cout<<largestRectangleArea(v)<<endl;
  53.  
  54. return 0;
  55. }
  56.  
  57. int main() {
  58. fastio();
  59. File();
  60. int32_t t=1;
  61. cin>>t;
  62. for(auto i=t;i--;){
  63. solve();
  64. }
  65. }
  66.  
Success #stdin #stdout 0.01s 5284KB
stdin
2
7
2 1 4 5 1 3 3
4
1000 1000 1000 1000
stdout
8
4000