-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSQRT_TREE.cpp
54 lines (44 loc) · 933 Bytes
/
SQRT_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
inline int op(int val1, int val2)
{
return (1LL*val1*val2)%prime;
}
vector<int>arr;
vector< vector<int> >prefix,suffix;
void build(int n)
{
int layers = 0;
while( (1<<layers) <n) layers++;
layers++;
suffix.resize(layers);
prefix.resize(layers);
for(int i=0;i<layers;i++)
{
suffix[i].resize(n);
prefix[i].resize(n);
}
for(int sz=2,lev=1; sz<=n; sz<<=1,lev++)
{
for(int i=0; i<n; i+=sz)
{
suffix[lev][i]=arr[i];
for(int j=1;j<sz;j++) suffix[lev][i+j] = op(suffix[lev][i+j-1], arr[i+j]);
prefix[lev][i+sz-1]=arr[i+sz-1];
for(int j=1;j<sz;j++) prefix[lev][i+sz-1-j] = op(prefix[lev][i+sz-j], arr[i+sz-1-j]);
}
}
}
int query(int l, int r)
{
if(l==r) return arr[l];
if(l+1==r) return op(arr[l],arr[r]);
int bit = (l^r);
int msb = 31 - __builtin_clz(bit);
if((l+(1<<msb-1)==r) && !((r+1)%(1<<msb)))
{
return prefix[msb][l];
}
else
{
return op(prefix[msb][l], suffix[msb][r]);
}
}