#define PREFIX

#include <solution/303.h>
#include <stdlib.h>

#ifdef PREFIX
#else
#include <string.h>
#endif

NumArray *numArrayCreate(int *nums, int numsSize)
{
    NumArray *ret = malloc(sizeof(NumArray));
    ret->size = numsSize;
#ifdef PREFIX
    ret->data = malloc(sizeof(int) * (numsSize + 1));
    ret->data[0] = 0;
    for (int i = 0; i < numsSize; i++)
        ret->data[i + 1] = ret->data[i] + nums[i];
#else
    ret->data = malloc(sizeof(int) * numsSize);
    memcpy(ret->data, nums, sizeof(int) * numsSize);
#endif
    return ret;
}

int numArraySumRange(NumArray *obj, int left, int right)
{
#ifdef PREFIX
    return obj->data[right + 1] - obj->data[left];
#else
    int sum = 0;
    for (int i = left; i <= right; i++)
        sum += obj->data[i];

    return sum;
#endif
}

void numArrayFree(NumArray *obj)
{
    free(obj->data);
    free(obj);
}
