Problem p02351 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
        DSL: domain-specific language
        http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_F
        12/5/2017
        TLE: 15/22, doing the query(use loop for (i,j))
        solution: add another ele in node to keep element, look liks add more
   element is hard to update the Final solution1.0: use the recusive and
*/
// why long is not enough
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// #define N 131072 //100000
#define MAXVAL ((long int)((unsigned long)(1 << 31) - 1)) //*2
typedef long long int ll;
ll min(int a, int b) { return a > b ? b : a; }
ll max(int a, int b) { return a > b ? a : b; }
ll query(int search_left, int search_right, int node_id, int node_left,
         int node_right);
void update(int update_left, int update_right, int new_value, int node_id,
            int node_left, int node_right);
void update2(int x, int node_id, int node_left, int node_right);
ll data[2 * 131072 + 100]; // nodes represent the add of current node(for leaf
                           // it means, for interal node: it has children)
ll sum_children[2 * 131072 +
                100]; // nodes represent the sum of children(leaf are zero);
// BEGIN FROM 0;
int main() {
  int n = 0, q = 0;
  int N = 1;
  int a, b, c;
  // read from the file
  // FILE *fp;
  // fp = fopen("in.txt","r");
  scanf("%d %d", &n, &q);

  // how to calculate the beginning index (the last layer of the tree)
  // how many nodes in last layer
  while (N < n)
    N = N << 1;
  // printf("N%d\\n",N);
  for (int i = 0; i <= 2 * N - 2; i++) {
    data[i] = 0;
    sum_children[i] = 0;
  }

  // printf("count:%d",count);

  for (int i = 1; i <= q; i++) {
    int temp = 0;
    scanf("%d", &temp);
    if (temp == 0) {
      scanf("%d %d %d", &a, &b, &c);
      a = a - 1;
      b = b - 1;
      update(a, b, c, 0, 0, N - 1);
      /*for(int i = 0 ;i<=2*N-2; i++){
              printf("%d ",sum_children[i]);
      }printf("\\n");*/
    } else if (temp == 1) {
      scanf("%d %d", &a, &b);
      a = a - 1;
      b = b - 1;
      printf("%lld\\n", query(a, b, 0, 0, N - 1));
    }
  }
  return 0;
}

/*
        update the value in the array, no return
        foramtion: if() return;
                                f();
*/
void update(int update_left, int update_right, int new_value, int node_id,
            int node_left, int node_right) {
  if (update_right < node_left || update_left > node_right)
    return;
  else if (update_left <= node_left && update_right >= node_right) {
    data[node_id] += new_value; // to update the value(if in the right rage or a
                                // single point;
  } else {
    sum_children[node_id] +=
        (min(update_right, node_right) - max(update_left, node_left) + 1) *
        new_value;
    // update with update_left and update_right
    update(update_left, update_right, new_value, 2 * node_id + 1, node_left,
           (node_left + node_right) / 2);
    update(update_left, update_right, new_value, 2 * node_id + 2,
           (node_left + node_right) / 2 + 1, node_right);
  }
}
ll query(int search_left, int search_right, int node_id, int node_left,
         int node_right) {
  if (search_right < node_left || search_left > node_right) {
    return 0; // not in the range
  } else if (search_left <= node_left &&
             search_right >= node_right) { // unfold until left==right
    return ((node_right - node_left + 1) * data[node_id] +
            sum_children[node_id]);
  } else {
    // printf("wei");
    ll sum = (min(search_right, node_right) - max(search_left, node_left) + 1) *
             (data[node_id]);
    ll left = query(search_left, search_right, 2 * node_id + 1, node_left,
                    (node_left + node_right) / 2);
    ll right = query(search_left, search_right, 2 * node_id + 2,
                     (node_left + node_right) / 2 + 1, node_right);
    return (sum + left + right);
  }
}

Error: TLE

Fixed submission:

/*
        DSL: domain-specific language
        http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_F
        12/5/2017
        TLE: 15/22, doing the query(use loop for (i,j))
        solution: add another ele in node to keep element, look liks add more
   element is hard to update the Final solution1.0: use the recusive and
*/
// why long is not enough
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// #define N 131072 //100000
#define MAXVAL ((long int)((unsigned long)(1 << 31) - 1)) //*2
typedef long long int ll;
ll min(int a, int b) { return a > b ? b : a; }
ll query(int search_left, int search_right, int node_id, int node_left,
         int node_right);
void update(int update_left, int update_right, int new_value, int node_id,
            int node_left, int node_right);
void update2(int x, int node_id, int node_left, int node_right);
ll data[2 * 131072 + 100]; // the size of array, for present the tree
// BEGIN FROM 0;
int main() {
  int n = 0, q = 0;
  int N = 1;
  int a, b, c;
  // read from the file
  // FILE *fp;
  // fp = fopen("in.txt","r");
  scanf("%d %d", &n, &q);

  // how to calculate the beginning index (the last layer of the tree)
  // how many nodes in last layer
  while (N < n)
    N = N << 1;
  // printf("N%d\\n",N);
  for (int i = 0; i <= 2 * N - 2; i++) {
    data[i] = 0;
  }

  // printf("count:%d",count);

  for (int i = 1; i <= q; i++) {
    int temp = 0;
    scanf("%d", &temp);
    if (temp == 0) {
      scanf("%d %d %d", &a, &b, &c);
      a = a - 1;
      b = b - 1;
      update(a, b, c, 0, 0, N - 1);
    } else if (temp == 1) {
      scanf("%d %d", &a, &b);
      a = a - 1;
      b = b - 1;
      // update2(data[0],1,0,(N-1)/2);update2(data[0],2,(N-1)/2+1,N-1);
      // test print
      /*for(int i = 0 ;i<=2*N-2; i++){
              printf("%d ",data[i]);
      }printf("\\n");*/
      printf("%lld\\n", query(a, b, 0, 0, N - 1));
      // getchar();
      // test print
      /*for(int i = 0 ;i<=2*N-2; i++){
              printf("%d ",data[i]);
      }printf("\\n");*/
    }
  }
  return 0;
}

/*
        update the value in the array, no return
        foramtion: if() return;
                                f();
*/
void update(int update_left, int update_right, int new_value, int node_id,
            int node_left, int node_right) {
  if (update_right < node_left || update_left > node_right)
    return;
  else if (update_left <= node_left && update_right >= node_right) {
    data[node_id] += new_value; // to update the value(if in the right rage or a
                                // single point;
  } else {
    if (data[node_id] >= 0) { // deal with previous node (to check the if there
                              // is previous vlue to update)
      update(node_left, node_right, data[node_id], 2 * node_id + 1, node_left,
             (node_left + node_right) / 2);
      update(node_left, node_right, data[node_id], 2 * node_id + 2,
             (node_left + node_right) / 2 + 1, node_right);
      data[node_id] = 0; // why set -1 (unfold it)
    }
    // update with update_left and update_right
    update(update_left, update_right, new_value, 2 * node_id + 1, node_left,
           (node_left + node_right) / 2);
    update(update_left, update_right, new_value, 2 * node_id + 2,
           (node_left + node_right) / 2 + 1, node_right);
  }
}
ll query(int search_left, int search_right, int node_id, int node_left,
         int node_right) {
  if (search_right < node_left || search_left > node_right) {
    return 0; // not in the range
  } else {
    if (node_left != node_right && data[node_id] != 0) {
      data[node_id * 2 + 1] += data[node_id];
      data[node_id * 2 + 2] += data[node_id];
      data[node_id] = 0;
    }
    if (node_left == node_right) {
      return data[node_id]; // not unfold
    } else {
      ll left = query(search_left, search_right, 2 * node_id + 1, node_left,
                      (node_left + node_right) / 2);
      ll right = query(search_left, search_right, 2 * node_id + 2,
                       (node_left + node_right) / 2 + 1, node_right);
      return (left + right);
    }
  }
}