Problem p02348 (C++) — buggy submission, verdict: Runtime Error

/*
        DSL: domain-specific language
        http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_D
        12/5/2017
        TLE: 17/22 10^10 must change the data structure--> binary tree
        update the data: set the priority
        WA 2/22
*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define N 131072 // 100000
#define MAXVAL ((long int)((unsigned long)(1 << 31) - 1))
typedef struct Node {
  int val;
  int priority;
} node;
void find(int i);
void add(int s, int t, int x, int i);
node array[2 * N + 100]; // the size of array, for present the tree
// BEGIN FROM 1;
int main() {
  int n = 0, q = 0;
  int a, b, c;
  // FILE *fp;
  // fp = fopen("in.txt","r");
  scanf("%d %d", &n, &q);

  // how to calculate the beginning index (the last layer of the tree)
  int count = 0;
  int temp = n;
  while (n != 1) {
    n = n / 2;
    count++;
  }
  if (pow(2, count) != temp)
    count++;
  // printf("count:%d",count);
  int B = pow(2, count) - 1;
  n = temp;
  for (int i = 0; i <= B; i++) { // special case forn==1, need i = 0;
    array[i].val = MAXVAL;
    array[i].priority = 0;
  }
  for (int i = 1; i <= n; i++) {
    array[i + B].val =
        MAXVAL; // e.g.begin from 8 (the last element of previous layer is 7)
    array[i + B].priority = 0;
  } // printf("%d\\n", array[B].val);
  B = B + 1;
  for (int i = 1; i <= q; i++) {
    int temp = 0;
    scanf("%d", &temp);
    if (temp == 0) {
      scanf("%d %d %d", &a, &b, &c);
      add(a + B, b + B, c, i);
    } else if (temp == 1) {
      scanf("%d", &a);
      find(a + B);
    }
  }
  return 0;
}
// add elements
// s means: left pointer
// t means : right pointer
void add(int s, int t, int x, int i) {
  // signle node
  if (s == t) {
    array[s].val = x;
    array[s].priority = i;
    return;
  }
  // range update
  // check the left pointer
  while (s < t) {
    // printf("inside loop:s%d,t%d,s%d\\n",s,t,x);
    if (s % 2 == 0) { // left child, level up
      s = s / 2;
    } else { // right child: mark and level up + 1
      array[s].val = x;
      array[s].priority = i;
      s = s / 2;
      s++;
    }

    if (t % 2 == 0) { // left child:
      array[t].val = x;
      array[t].priority = i;
      t = t / 2;
      t--;
    } else { // right child
      t /= 2;
    }
    if (s == t) {
      array[s].val = x;
      array[s].priority = i;
    } // works for 1-4,1-3, 1-5
      // printf("inside loop:s%d,t%d,s%d\\n",s,t,x);
  }

  // test for
  /*for(int i = 1; i<20; i++){
          printf("%2d ", i);
  }printf("\\n");
  for(int i = 1; i<20; i++){
          printf("%2d ", array[i]);
  }*/
}

void find(int i) { // the index of the tree
  int res = array[i].val;
  int max = 0; // the priority
  // printf("%d\\n", array[i].val);
  while (i >= 1) {
    if (array[i].priority > max) {
      res = array[i].val;
      // printf("array:%d",res);
      max = array[i].priority;
    }
    i = i / 2;
  }

  printf("%d\\n", res);
}

Error: -11

Fixed submission:

/*
        DSL: domain-specific language
        http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_D
        12/5/2017
        TLE: 17/22 10^10 must change the data structure--> binary tree
        update the data: set the priority
        WA 2/22
*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define N 131072 // 100000
typedef struct Node {
  int val;
  int priority;
} node;
void find(int i);
void add(int s, int t, int x, int i);
node array[2 * N + 100]; // the size of array, for present the tree
// BEGIN FROM 1;
int main() {
  int n = 0, q = 0;
  int a, b, c;
  FILE *fp;
  fp = fopen("in.txt", "r");
  fscanf(fp, "%d %d", &n, &q);

  // how to calculate the beginning index (the last layer of the tree)
  int count = 0;
  int temp = n;
  while (n != 1) {
    n = n / 2;
    count++;
  }
  if (pow(2, count) != temp)
    count++;
  // printf("count:%d",count);
  int B = pow(2, count) - 1;
  for (int i = 0; i <= B; i++) { // special case forn==1, need i = 0;
    array[i].val = pow(2, 31) - 1;
    array[i].priority = 0;
  }
  for (int i = 1; i <= n; i++) {
    array[i + B].val =
        pow(2, 31) -
        1; // e.g.begin from 8 (the last element of previous layer is 7)
    array[i].priority = 0;
  } // printf("%d\\n", array[B].val);
  for (int i = 0; i < q; i++) {
    int temp = 0;
    fscanf(fp, "%d", &temp);
    if (temp == 0) {
      fscanf(fp, "%d %d %d", &a, &b, &c);
      add(a + B, b + B, c, i);
    } else if (temp == 1) {
      fscanf(fp, "%d", &a);
      find(a + B);
    }
  }
  return 0;
}
// add elements
// s means: left pointer
// t means : right pointer
void add(int s, int t, int x, int i) {
  // signle node
  if (s == t) {
    array[s].val = x;
    array[s].priority = i;
    return;
  }
  // range update
  // check the left pointer
  while (s < t) {
    // printf("inside loop:s%d,t%d,s%d\\n",s,t,x);
    if (s % 2 == 0) { // left child, level up
      s = s / 2;
    } else { // right child: mark and level up + 1
      array[s].val = x;
      array[s].priority = i;
      s = s / 2;
      s++;
    }

    if (t % 2 == 0) { // left child:
      array[t].val = x;
      array[s].priority = i;
      t = t / 2;
      t--;
    } else { // right child
      t /= 2;
    }
    if (s == t) {
      array[s].val = x;
      array[s].priority = i;
    } // works for 1-4,1-3, 1-5
      // printf("inside loop:s%d,t%d,s%d\\n",s,t,x);
  }

  // test for
  /*for(int i = 1; i<20; i++){
          printf("%2d ", i);
  }printf("\\n");
  for(int i = 1; i<20; i++){
          printf("%2d ", array[i]);
  }*/
}

void find(int i) { // the index of the tree
  int res = array[i].val;
  int max = 0; // the priority
  // printf("%d\\n", array[i].val);
  while (i >= 1) {
    if (array[i].priority > max) {
      res = array[i].val;
      // printf("array:%d",res);
      max = array[i].priority;
    }
    i = i / 2;
  }

  printf("%d\\n", res);
}