https://www.codechef.com/viewsolution/48355945
please tell me whats wrong with it ?
I know that brute force solution will passed for weak test case.
I tried to do it without brute force. Can anyone tell me why I get WA??
Solution link : CodeChef: Practical coding for everyone
TIA.
you use 0 based index in array.
but you are running a loop from L to R.
Therefore, it gives you WA. Take input as 1 based index array.
I am using brute force, but it is still giving TLE
Can you please check it out
#include<bits/stdc++.h>
using namespace std;int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long n, q, l, r, x, y, index;
cin>>n>>q;
long a[n+1];
for(long i=1 ; i<=n ; i++)
cin>>a[i];while(q--) { cin>>index; if(index==1) { cin>>l>>r>>x; for(long i=l ; i<=r ; i++) a[i]=a[i]+pow(x+i-l, 2); } else { cin>>y; cout<<a[y]<<"\n"; } } return 0;
}
You have declared the size of the vector as n, however in the loop, you are taking it from 1 to n, while the indexing of the vector starts from 0, so either increase the size of the vector to n+1 or take the loop from 0 to n-1(inclusive) .
And the query loop you used is also completely wrong