Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Tuesday, June 3, 2014

AKVQLD03-How to Handle the Fans

How to Handle the Fans

Given below c++ code for akvqld03 spoj or How to Handle the Fans.
This is simple implementation of BIT(binary index tree) . If want to read about this you can


#include <bits/stdc++.h>
using namespace std;
#define LL long long
int MAX=0;
LL a[1000009];

void update(LL a[],int idx,int val)
{
    while(idx <= MAX)
    {
        a[idx]+=val;
        idx+=(idx & -idx);
    }
}
LL query(LL a[],int idx)
{
    LL sum = 0;
    while(idx>0)
    {
        sum+=a[idx];
        idx-=(idx & -idx);
    }
    return sum;
}
int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    MAX = n;
    while(q--)
    {
        char s[10];
        int i,j;
        scanf("%s",s);
        if(s[0] == 'f')
        {
            scanf("%d%d",&i,&j);
            printf("%lld\n",query(a,j)-query(a,i-1));
        }
        else{
            scanf("%d%d",&i,&j);
            update(a,i,j);
        }
    }
    return 0;
}

No comments:

Post a Comment

Your comment is valuable to us