I used Square root decomposition and divided the whole array into buckets where each row in arr represents one bucket. Then I stored the prefix xor’s of all the buckets in another 2D array dp. But it’s giving wrong answer. Please help!
It is always giving right answer for the first query and even update is working fine. But it’s giving wrong answer from second query if it is of type 2.
#include #include #include using namespace std; int a[100005]; int main() { int n,q; scanf("%d%d",&n,&q); for(int i=0;i< n;i++) scanf("%d",&a[i]); int bsize=(int)floor(sqrt(n)); int nbuck=n/bsize; if(n%bsize!=0) nbuck+=1; int arr[nbuck][bsize]; memset(arr,0,sizeof(arr)); for(int i=0;i< n;i++) { arr[i/bsize][i%bsize]=a[i]; } int dp[nbuck][bsize]; memset(dp,0,sizeof(dp)); int exor; for(int i=0;i< nbuck;i++) { exor=0; for(int j=0;j< bsize;j++) { exor^=arr[i][j]; dp[i][j]=exor; } } int ty,i,x,k,count,buck,end,ans; while(q--) { scanf("%d",&ty); if(ty==1) { scanf("%d%d",&i,&x); i--; arr[i/bsize][i%bsize]=x; exor=0; for(int j=0;j< bsize;j++) { exor^=arr[i/bsize][j]; dp[i/bsize][j]=exor; } } else { scanf("%d%d",&i,&k); i--; ans=0; buck=i/bsize; end=bsize-1; count=0; for(int x=0;x<=buck;x++) { if(x==buck) { end=i%bsize; for(int j=0;j<=end;j++) { ans=ans^dp[x][j]; if(ans==k) count++; } break; } for(int y=0;y<=end;y++) { if(dp[x][y]==(ans^k)) count++; } ans^=dp[x][bsize-1]; } printf("%d\n",count); } } return 0; } ```