-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsparce_table
52 lines (51 loc) · 1.2 KB
/
sparce_table
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
#include <bits/stdc++.h>
using namespace std;
struct sparse_table
{
// 0 indexed everything
vector<vector<long long>> st;
int n;
long long f(long long x, long long y)
{
return min(x, y);
}
void init(vector<long long> &a)
{
n = a.size();
int K = log2(n) + 1;
st.resize(n + 1);
for (int i = 0; i <= n; i++)
st[i].resize(K + 1);
for (int i = 0; i < n; i++)
st[i][0] = a[i];
for (int j = 1; j <= K; j++)
for (int i = 0; i + (1 << j) <= n; i++)
st[i][j] = f(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
long long getSum(int l, int r)
{
long long sum = 0;
for (int j = log(n) + 1; j >= 0; j--)
{
while ((1 << j) <= (r - l + 1))
{
sum += st[l][j];
l += (1 << j);
}
}
return sum;
}
long long getMin(int l, int r)
{
int p = log2(r - l + 1);
return min(st[l][p], st[r - (1 << p) + 1][p]);
}
long long getMax(int l, int r)
{
int p = log2(r - l + 1);
return max(st[l][p], st[r - (1 << p) + 1][p]);
}
};
int main()
{
}