-
Notifications
You must be signed in to change notification settings - Fork 2
/
1083 - Histogram(Seg Tree).cpp
67 lines (67 loc) · 1.47 KB
/
1083 - Histogram(Seg Tree).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
using namespace std;
#define mx 30005
int arr[mx];
struct node
{
int val;
int ind;
}tree[mx*4],zero;
void build(int pos, int b, int e)
{
if(b>e) return;
if(b==e){
tree[pos].val = arr[b];
tree[pos].ind = b;
return;
}
int m = (b+e)/2;
int l = pos*2;
int r = l + 1;
build(l,b,m);
build(r,m+1,e);
tree[pos].val = min(tree[l].val, tree[r].val);
tree[pos].ind = (tree[l].val == tree[pos].val) ? tree[l].ind : tree[r].ind;
}
node query(int pos, int b, int e, int i, int j)
{
if(b>e || b>j || e<i) return zero;
if(b>=i && e<=j) return tree[pos];
int m = (b+e)/2;
int l = pos*2;
int r = l + 1;
node p1 = query(l,b,m,i,j);
node p2 = query(r,m+1,e,i,j);
if(p1.val == 0) return p2;
if(p2.val == 0) return p1;
if(p1.val<p2.val)
return p1;
else
return p2;
}
int solve(int b, int e,int n)
{
if(b>e) return 0;
if(b==e){
return arr[b];
}
node x = query(1,1,n,b,e);
int r1 = x.val * (e-b+1);
int r2 = solve(b,x.ind-1,n);
int r3 = solve(x.ind+1,e,n);
return max(r1, max(r2,r3));
}
int main()
{
int n,tc,cs=1;
scanf("%d", &tc);
zero.ind = 0;
zero.val = 0;
while(tc--){
scanf("%d",&n);
for(int i=1; i<=n; i++) scanf("%d",&arr[i]);
build(1,1,n);
printf("Case %d: %d\n",cs++,solve(1,n,n));
}
return 0;
}