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

Friday, October 17, 2014

MSE06H - Japan

Japan

given below code is for mse06h spoj or japan spoj .

Hint :->use BIT .




#include <bits/stdc++.h>
using namespace std;
struct par
{
    int f,s;
};
int tree[1009];
long long read(int pos)
{
    long long count = 0;
    while(pos)
    {
        count += tree[pos];
        pos -= (pos & -pos);
    }
    return count;
}
void update(int pos ,int MAX)
{
    while(pos <= MAX)
    {
        tree[pos]+=1;
        pos += (pos & -pos);
    }
}
bool compare(const par &a ,const par &b)
{
    return a.f == b.f ? a.s < b.s : a.f < b.f;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int l  =1 ; l<=t ; l++)
    {
        int n , m ,k ,a,b ;
        scanf("%d%d%d",&n,&m,&k);
        par p[1000009];
        memset(tree,0,sizeof tree);
        for(int i = 0; i < k ; i++){
            scanf("%d%d",&a , &b);
            p[i].f = a;
            p[i].s = b;
        }
        sort(p,p+k,compare);
        long long  res = 0;
        for(int i = 0 ; i < k ; i++)
        {
            res += (read(m) - read(p[i].s));
            update(p[i].s , m);
        }
        printf("Test case %d: %lld\n",l,res);
    }
    return 0;
}