-
Notifications
You must be signed in to change notification settings - Fork 3
/
Square_Root_Decomposition.cpp
59 lines (56 loc) · 1.07 KB
/
Square_Root_Decomposition.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
#include <bits/stdc++.h>
using namespace std;
#define ll long int
//SQUARE ROOT DECOMPOSITION
//RANGE SOME PROBLEM
int query(int * blocks,int* arr,int l,int r,int rn){
int ans=0;
//left part
while(l<r && l!=0 && l%rn!=0){
ans+=arr[l];
l++;
}
//middle part
while(l+rn<=r){
int block_id=l/rn;
ans+=blocks[block_id];
l+=rn;
}
//right part
while(l<=r){
ans+=arr[l];
++l;
}
return ans;
}
//update function
void update(int* blocks,int i,int val,int * arr,int rn){
int block_id=i/rn;
blocks[block_id]+=val-arr[i];
arr[i]=val;
}
int main() {
int t,n,m;
int a[]={1,3,5,2,7,6,3,1,4,8};
n=10;
int rn=sqrt(n);
int* blocks=new int[n+1];
//build function take o(n) time
int block_id=-1;
for(int i=0;i<n;i++){
if(i%rn==0){
block_id++;
}
blocks[block_id]+=a[i];
}
//print the blocks array
for(int i=0;i<=rn;i++){
cout<<blocks[i]<<" , "<<endl;
}
//queries
int l,r;
cin>>l>>r;
update(blocks,2,15,a,rn);
cout<< query(blocks,a,l,r,rn)<<endl;
return 0;
}