Hey @ramini,
To solve this question, ALATE from August Loc you need to do some precomputation so as to fit the answer in specified time-limit.
Since "func" calculates the sum of a given index x and all it's multiple in the given array(squares). So we can precompute all the values for x.
> for(i=1;i<=n;i++)
> {
>
> s=0;
>
> for(j=i;j<=n;j+=i){
>
> s = ( s + (arr[i]*arr[i]) % 1000000007) % 1000000007 ;
>
> }
>
> sum[i] = s ;
>
> }
Since you have all the pre-computed values than your query of type 1 can be answered in O(1) but the problem arises in the update query for that you need to know which values are actually going to be affected by the change of that particular x. (this is also pre-computed) so whenever a query of 2nd type occurs than you have to iterate over the values of sum[i] which are going to be affected and update their new values which are sum[i]-arr[i]*arr[i]+newValue *newValue;
**EDIT:-** First we will pre-compute which are going to affected by the index x.
> vector <long> v[100001];
> for(ii=1;ii<100001;ii++) {
> for(jj=ii;jj<100001;jj+=ii)
> v[jj].push_back(ii); }
After that we just need to iterate over the affected values and update them as they are stored in v[x]
> for(i=0;i<v[x].size();i++)
> {
>
> sum[v[x][i]] = (sum[v[x][i]] - sq[x] +
> nsq+1000000007) %1000000007 ;
>
>
> }
where nsq is new square value.
Hope this helps!
([Code][1])
[1]: https://www.codechef.com/viewsolution/15155591