Advertisement
Saleh127

Light OJ 1083/SPOJ HISTOGRA - Largest Rectangle in a Histogram

Jul 18th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. /// Light OJ 1083
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. #define ll long long
  6. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  7. ll a[200005];
  8. pair<ll,ll> tree[800005];
  9.  
  10. void treeconstruct(ll node,ll b,ll e)
  11. {
  12.  
  13. if(b==e)
  14. {
  15. tree[node]={a[b],b};
  16. return;
  17. }
  18.  
  19. ll left = node*2;
  20. ll right = node*2 + 1ll;
  21.  
  22. ll mid=(b+e)/2;
  23.  
  24. treeconstruct(left,b,mid);
  25. treeconstruct(right,mid+1,e);
  26.  
  27. tree[node]=min(tree[left],tree[right]);
  28.  
  29. }
  30.  
  31. pair<ll,ll> queries(ll node,ll b,ll e,ll i,ll j)
  32. {
  33. if(i>e || j<b) return {INT_MAX,INT_MAX};
  34.  
  35. if(b>=i && e<=j) return tree[node];
  36.  
  37. ll left = node*2;
  38. ll right = node*2 + 1ll;
  39.  
  40. ll mid=(b+e)/2;
  41.  
  42. //pair<ll,ll> x=queries(left,b,mid,i,j);
  43.  
  44. //pair<ll,ll> y=queries(right,mid+1,e,i,j);
  45.  
  46. //return min(x,y);
  47.  
  48. return min(queries(left,b,mid,i,j),queries(right,mid+1,e,i,j));
  49. }
  50.  
  51. ll solve(ll b,ll e,ll n)
  52. {
  53. if(b>e) return 0ll;
  54.  
  55. pair<ll,ll>x=queries(1,1,n,b,e);
  56.  
  57. ll k=(e-b+1)*x.first;
  58. ll l=max(solve(b,x.second-1,n),solve(x.second+1,e,n));
  59.  
  60. return max(k,l);
  61. }
  62.  
  63. int main()
  64. {
  65. ios_base::sync_with_stdio(0);
  66. cin.tie(0);
  67. cout.tie(0);
  68.  
  69.  
  70. test
  71. {
  72. ll n,m,i,j,k,l;
  73.  
  74. cin>>n;
  75.  
  76. for(i=1; i<=n; i++)
  77. {
  78. cin>>a[i];
  79. }
  80.  
  81. treeconstruct(1ll,1ll,n);
  82.  
  83. cout<<"Case "<<cs<<": "<<solve(1ll,n,n)<<endl;
  84.  
  85. }
  86.  
  87. return 0;
  88. }
  89.  
  90. /*
  91.  
  92. ///SPOJ HISTOGRA - Largest Rectangle in a Histogram
  93.  
  94. #include <bits/stdc++.h>
  95. using namespace std;
  96. #define ll long long
  97. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  98. ll a[200005];
  99. pair<ll,ll> tree[800005];
  100.  
  101. void treeconstruct(ll node,ll b,ll e)
  102. {
  103.  
  104. if(b==e)
  105. {
  106. tree[node]={a[b],b};
  107. return;
  108. }
  109.  
  110. ll left = node*2;
  111. ll right = node*2 + 1ll;
  112.  
  113. ll mid=(b+e)/2;
  114.  
  115. treeconstruct(left,b,mid);
  116. treeconstruct(right,mid+1,e);
  117.  
  118. tree[node]=min(tree[left],tree[right]);
  119.  
  120. }
  121.  
  122. pair<ll,ll> queries(ll node,ll b,ll e,ll i,ll j)
  123. {
  124. if(i>e || j<b) return {INT_MAX,INT_MAX};
  125.  
  126. if(b>=i && e<=j) return tree[node];
  127.  
  128. ll left = node*2;
  129. ll right = node*2 + 1ll;
  130.  
  131. ll mid=(b+e)/2;
  132.  
  133. //pair<ll,ll> x=queries(left,b,mid,i,j);
  134.  
  135. //pair<ll,ll> y=queries(right,mid+1,e,i,j);
  136.  
  137. //return min(x,y);
  138.  
  139. return min(queries(left,b,mid,i,j),queries(right,mid+1,e,i,j));
  140. }
  141.  
  142. ll solve(ll b,ll e,ll n)
  143. {
  144. if(b>e) return 0ll;
  145.  
  146. pair<ll,ll>x=queries(1,1,n,b,e);
  147.  
  148. ll k=(e-b+1)*x.first;
  149. ll l=max(solve(b,x.second-1,n),solve(x.second+1,e,n));
  150.  
  151. return max(k,l);
  152. }
  153.  
  154. int main()
  155. {
  156. ios_base::sync_with_stdio(0);
  157. cin.tie(0);
  158. cout.tie(0);
  159.  
  160. ll n;
  161. while(cin>>n && n)
  162. {
  163. ll m,i,j,k,l;
  164.  
  165. // cin>>n;
  166.  
  167. for(i=1; i<=n; i++)
  168. {
  169. cin>>a[i];
  170. }
  171.  
  172. treeconstruct(1ll,1ll,n);
  173.  
  174. cout<<solve(1ll,n,n)<<endl;
  175.  
  176. }
  177.  
  178. return 0;
  179. }
  180.  
  181. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement