-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloc2.cpp
120 lines (83 loc) · 2.02 KB
/
loc2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <stdio.h>
#include <math.h>
#define ll long long int
#define s 1000000007
using namespace std;
ll getMid(ll f, ll e) { return f + (e -f)/2; }
ll getSumUtil(ll *st, ll ss, ll se, ll qs, ll qe, ll si,ll x)
{
if ((si)%x==0)
return ((st[si])*(st[si]));
if (se < qs || ss > qe)
return 0;
ll mid = getMid(ss, se);
return (getSumUtil(st, ss, mid, qs, qe, 2*si+1,x)+
getSumUtil(st, mid+1, se, qs, qe, 2*si+2,x));
}
void updateValueUtil(ll *st, ll ss, ll se, ll i, ll diff, ll si)
{
if (i < ss || i > se)
return;
st[si] = st[si] + diff;
if (se != ss)
{
ll mid = getMid(ss, se);
updateValueUtil(st, ss, mid, i, diff, 2*si + 1);
updateValueUtil(st, mid+1, se, i, diff, 2*si + 2);
}
}
void updateValue(ll arr[], ll *st, ll n, ll i, ll new_val)
{
ll diff = new_val - arr[i];
arr[i] = new_val;
updateValueUtil(st, 0, n-1, i, diff, 0);
}
ll getSum(ll *st, ll n, ll qs, ll qe,ll x)
{
return getSumUtil(st, 0, n-1, qs, qe, qs,x);
}
ll constructSTUtil(ll arr[], ll ss, ll se, ll *st, ll si)
{
if (ss == se)
{
st[si] = arr[ss];
return arr[ss];
}
ll mid = getMid(ss, se);
st[si] = constructSTUtil(arr, ss, mid, st, si*2+1) +
constructSTUtil(arr, mid+1, se, st, si*2+2);
return st[si];
}
ll *constructST(ll arr[], ll n)
{
ll x = (ll)(ceil(log2(n)));
ll max_size = 2*(ll)pow(2, x) - 1;
ll *st = new ll[max_size];
constructSTUtil(arr, 0, n-1, st, 0);
return st;
}
int main()
{
ll no,n,arr[100000],q,type,l,r,i,x;
cin>>no;
while(no--)
{
cin>>n>>q;
for(i=0;i<n;i++)
cin>>arr[i];
ll *st = constructST(arr, n);
while(q--)
{
cin>>type>>l;
if(type==1)
cout<<getSum(st, n, 1, n,x)<<endl;
if(type==2)
{
cin>>l>>r;
updateValue(arr, st, n, l,r);
}
}
}
return 0;
}