In this HackerEarth Help Ashu problem solution Ashu and Shanu are best buddies. One day Shanu gives Ashu a problem to test his intelligence.He gives him an array of N natural numbers and asks him to solve the following queries:-
  1. Query 0:- modify the element present at index i to x.
  2. Query 1:- count the number of even numbers in range l to r inclusive.
  3. Query 2:- count the number of odd numbers in range l to r inclusive.

HackerEarth Help Ashu problem solution


HackerEarth Help Ashu problem solution.

#include<bits/stdc++.h>
using namespace std;
#define MAX 100005
int BIT[MAX];
void upd(int idx,int val){
    for(int i=idx;i<MAX;i+=i&(-i))
    BIT[i]+=val;
}
int query(int val){
    int ret=0;
    while(val){
        ret+=BIT[val];
        val-=val&(-val);
    }
    return ret;
}
int a[MAX];
int main(){
    int n,i;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(a[i]%2==0)
        upd(i,1);
    }
    int q,l,r,ch;
    cin>>q;
    while(q--){
        cin>>ch>>l>>r;
        if(ch==0){
            if(r%2==1&&a[l]%2==0)
            upd(l,-1);
            if(r%2==0&&a[l]%2==1)
            upd(l,1);
            a[l]=r;
        }
        else if(ch==1){
            int cnt=query(r)-query(l-1);
            cout<<cnt<<endl;
        }
        else{
            int cnt=query(r)-query(l-1);
            cout<<(r-l+1)-cnt<<endl;
        }
    }
    return 0;
}