forked from pranavanurag/SPOJSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHISTOGRA.cpp
More file actions
85 lines (69 loc) · 1.29 KB
/
Copy pathHISTOGRA.cpp
File metadata and controls
85 lines (69 loc) · 1.29 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define INF 1000000001
int SegTree[400001], H[100001], N;
ull Max(ull x, ull y)
{
if (x < y)
return y;
return x;
}
void Build(int i, int STx, int STy)
{
if (STx > STy)
return;
if (STx == STy)
SegTree[i] = STx;
else
{
int STm = (STx + STy)/2;
Build(2*i, STx, STm);
Build(2*i+1, STm+1, STy);
if (H[SegTree[2*i]] < H[SegTree[2*i+1]])
SegTree[i] = SegTree[2*i];
else
SegTree[i] = SegTree[2*i+1];
}
}
int QueryT(int i, int STx, int STy, int x, int y)
{
if (x > y || STx > STy || x > STy || y < STx)
return 0;
if (STx >= x && STy <= y)
return SegTree[i];
int STm = (STx + STy)/2;
int AnsL = QueryT(2*i, STx, STm, x, y),
AnsR = QueryT(2*i+1, STm+1, STy, x, y);
if (H[AnsL] < H[AnsR])
return AnsL;
return AnsR;
}
int Query(int x, int y)
{
return QueryT(1, 1, N, x, y);
}
ull LargestArea(int x, int y)
{
if (x > y)
return 0;
if (x == y)
return H[x];
int p = Query(x, y);
ull AnsL = LargestArea(x, p-1);
ull AnsR = LargestArea(p+1, y);
ull AnsM = (ull)H[p]*(ull)(y - x + 1);
return Max(AnsL, Max(AnsR, AnsM));
}
int main()
{
H[0] = INF;
while (cin>>N && N != 0)
{
for (int i = 1; i <= N; i++)
cin>>H[i];
Build(1, 1, N);
cout<<LargestArea(1, N)<<endl;
}
return 0;
}