Thursday, November 13, 2014

Spoj Solution of 16487. Update the array ! Problem code: UPDATEIT

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
long long array[10005];

int main()
{
    long long  i, j, k;
    long long test, n, u, qur, last, first, value, index;

    scanf("%lld", &test);
    while(test--)
    {
        scanf("%lld%lld", &n, &u);

        for(i=0; i<n; i++)
        {
            array[i]=0;
        }
        while(u--)
        {
            scanf("%lld%lld%lld", &first, &last, &value);
            array[first]+=value;
            array[last+1]-=value;
        }
        for(i=1; i<n; i++)
        {
            array[i] += array[i-1];
        }
        scanf("%lld", &qur);
        while(qur--)
        {
            scanf("%lld", &index);
            printf("%lld\n", array[index]);
        }
    }

    return 0;
}

2 comments:

  1. Awesome solution and it was a bit humiliating.How did you come up with this when segment and bit trees are the usual ways to solve?

    ReplyDelete
  2. can you explain me why this method works ?

    ReplyDelete

Compare equality of two string in C

#include <stdio.h> #include<string.h> int main() {     char* country = "Bangladesh";     char* country2;     ...