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

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ?????¢
// XCode??§???EOF??\\??????Ctrl+D
// ?\\???Alt+\\
// ans???????§?INT?????????2,147,483,647????¶????????????¨??????????????§long
// long?????£???????????????????????????
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////
//
////////////////////////////////////////

// #define debug
// //*******************************************************************************************************************************************
#ifdef debug
#include <chrono>
#endif

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <cstring> //memcpy
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <map>
// #include <unordered_map> //hash func.
#include <fstream>  //ifstream, ofstream
#include <iterator> //insert_iterator::inserter

// #define NDEBUG //If NDEBUG is defined before #include <cassert>, assert will
// be ignored. You had better define NDEBUG when u submit the code.
#include <cassert> //assert

using namespace std;

#define dout cout
// If u wanna output to a text file instead of standard output, plz define
// OUTPUTFILE. #define OUTPUTFILE "output.txt"
// //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath                                                         \\
  "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/"      \\
  "output.txt"
#endif

#define din cin
// If u wanna input from a text file instead of standard input, plz define
// INPUTFROMTEXTFILE???. #define INPUTFILE "input.txt"
// //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif

#define scand(A) scanf("%d", &(A))
#define scans(A) scanf("%s", (A))
#define disp(A) dout << #A << " = " << setw(3) << (A) << endl
#define disP(A) dout << setw(3) << (A) << " "
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl
// #define dispAll(A,n) cout << #A << " = "; rep(j, 0, (n)) {cout << setw(3) <<
// A[j] << " ";} cout << endl

#define sign(x) ((x) > 0) - ((x) < 0) // x<0: -1, x=0: 0, x>0: +1
#define p(i) (i) / 2
#define l(i) (i) * 2
#define r(i) (i) * 2 + 1

int dx[] = {
    1, -1, 0, 0, 1,
    1, -1, -1}; //???????????????????????????????¨??????????????????????????????????
int dy[] = {0, 0, -1, 1, -1, 1, 1, -1};

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long ull;

const int INF = (1 << 31) - 1;
const ll INF_LL = (ll)9e18 - 1LL; // Be careful for overflow.
const ull INF_ULL = (ull)1e19 - 1ULL;
const int NONE = -1;
const ll MOD =
    (ll)1e9 + 7; //??§???????´???°?????£??¨??¨????????°????????????10???7??????

const int N_MAX = 100010; // num of vertex or element
const int M_MAX = 100010; // num of edge
const int DATA_MAX = 1010;

int N;
int a[N_MAX];

int B = 316;  // floor(sqrt(N_MAX))
int b[N_MAX]; // B+1 elements
int tmp[N_MAX];

void initilize() {
  // #ifdef debug
  //     N = 16;
  //     a[0] = 1;
  //     a[1] = 3;
  //     a[2] = 2;
  //     a[3] = 6;
  //     a[4] = 7;
  //     a[5] = 4;
  //     a[6] = 3;
  //     a[7] = 0;
  //     a[8] = 8;
  //     a[9] = 4;
  //     a[10] = 3;
  //     a[11] = 5;
  //     a[12] = 9;
  //     a[13] = 2;
  //     a[14] = 1;
  //     a[15] = 5;
  //     //a[16] = 10;
  // #endif

  rep(i, 0, B + 1) {
    memcpy(tmp, &a[i * B], B * sizeof(int));
    b[i] = *min_element(tmp, tmp + B);
  }
}

int find(int from, int to) {
  assert(from <= to);
  assert(to < N);

  int i = from / B;
  int j = to / B;

  int cnt = 0;
  int k;

  for (k = from; k < (i + 1) * B and k <= to; k++) {
    tmp[cnt++] = a[k];
    //        dout << "1 :" << a[k] << endl;
  }

  for (k = i + 1; (k + 1) * B - 1 <= to; k++) {
    tmp[cnt++] = b[k];
    //        dout << "2 :" << b[k] << endl;
  }

  for (k = k * B; k <= to; k++) {
    tmp[cnt++] = a[k];
    //        dout << "3 :" << a[k] << endl;
  }

  int min = *min_element(tmp, tmp + cnt);

#ifdef debug
  dout << "------ find( " << from << ", " << to << ")\\n";

  disp(i);
  disp(j);

  dispAll(tmp, cnt);
  disp(min);
#endif

  return min;
}

void update(int i, int x) {
  a[i] = x;

  i /= B;

  memcpy(tmp, &a[i * B], B * sizeof(int));
  b[i] = *min_element(tmp, tmp + B);
}

void display() {
  dout << "---------------------\\n";
  dout << "i = ";
  rep(i, 0, N) {
    if (i % B == 0)
      dout << "|" << setw(2) << i << " ";
    else
      dout << setw(3) << i << " ";
  }
  dout << endl;
  dispAll(a, N);
  dout << endl;
  dout << "b = ";
  rep(i, 0, B + 1) { dout << "|" << setw(14) << b[i] << " "; }
  dout << endl;
  dout << "---------------------\\n";
  dout << endl;
}

int main() {

  // cin, cout????????????
  // ?????¨??????cin?????????????????¨??¨cin??§???scanf?????????????????¨??¨scanf??§??±?????????????????????
  cin.tie(0);                  // cin??¨cout??????????????????
  ios::sync_with_stdio(false); // iostream??¨stdio??????????????????

  // read input data
  scand(N);
  int Q;
  scand(Q);

  //------------------------------------------------------------------------------------------
#ifdef debug
  // start timer
  auto startTime = chrono::system_clock::now();
#endif
  //------------------------------------------------------------------------------------------

  rep(i, 0, N_MAX) a[i] = INF;
  initilize();

  int command, x, y;
  rep(i, 0, Q) {
    scanf("%d%d%d", &command, &x, &y);

    if (command) {
      printf("%d\\n", find(x, y));
    } else {
      update(x, y);
    }
  }

  //------------------------------------------------------------------------------------------
#ifdef debug
  // stop timer
  auto endTime = chrono::system_clock::now();
  auto dur = endTime - startTime;
  auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
  dout << fixed << setprecision(4) << (double)msec / 1000 << " sec \\n";
#endif
  //------------------------------------------------------------------------------------------

#ifdef INPUTFILE
  inputfile.close();
#endif

#ifdef OUTPUTFILE
  outputfile.close();
  cout << "\\"" << OutputFilePath << "\\"" << endl;
#endif

  return 0;
}

Error: TLE

Fixed submission:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ?????¢
// XCode??§???EOF??\\??????Ctrl+D
// ?\\???Alt+\\
// ans???????§?INT?????????2,147,483,647????¶????????????¨??????????????§long
// long?????£???????????????????????????
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////
//
////////////////////////////////////////

// #define debug
// //*******************************************************************************************************************************************
#ifdef debug
#include <chrono>
#endif

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <cstring> //memcpy
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <map>
// #include <unordered_map> //hash func.
#include <fstream>  //ifstream, ofstream
#include <iterator> //insert_iterator::inserter

// #define NDEBUG //If NDEBUG is defined before #include <cassert>, assert will
// be ignored. You had better define NDEBUG when u submit the code.
#include <cassert> //assert

using namespace std;

#define dout cout
// If u wanna output to a text file instead of standard output, plz define
// OUTPUTFILE. #define OUTPUTFILE "output.txt"
// //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath                                                         \\
  "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/"      \\
  "output.txt"
#endif

#define din cin
// If u wanna input from a text file instead of standard input, plz define
// INPUTFROMTEXTFILE???. #define INPUTFILE "input.txt"
// //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif

#define scand(A) scanf("%d", &(A))
#define scans(A) scanf("%s", (A))
#define disp(A) dout << #A << " = " << setw(3) << (A) << endl
#define disP(A) dout << setw(3) << (A) << " "
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl
// #define dispAll(A,n) cout << #A << " = "; rep(j, 0, (n)) {cout << setw(3) <<
// A[j] << " ";} cout << endl

#define sign(x) ((x) > 0) - ((x) < 0) // x<0: -1, x=0: 0, x>0: +1
#define p(i) (i) / 2
#define l(i) (i) * 2
#define r(i) (i) * 2 + 1

int dx[] = {
    1, -1, 0, 0, 1,
    1, -1, -1}; //???????????????????????????????¨??????????????????????????????????
int dy[] = {0, 0, -1, 1, -1, 1, 1, -1};

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long ull;

const int INF = (1 << 31) - 1;
const ll INF_LL = (ll)9e18 - 1LL; // Be careful for overflow.
const ull INF_ULL = (ull)1e19 - 1ULL;
const int NONE = -1;
const ll MOD =
    (ll)1e9 + 7; //??§???????´???°?????£??¨??¨????????°????????????10???7??????

const int N_MAX = 100010; // num of vertex or element
const int M_MAX = 100010; // num of edge
const int DATA_MAX = 1010;

int N;
int a[N_MAX];

int find(int from, int to) {
  assert(from <= to);

  int b[to - from + 1];
  memcpy(b, &a[from], sizeof(b));

  return *min_element(b, b + to - from + 1);
}

void update(int i, int x) { a[i] = x; }

int main() {

  // cin, cout????????????
  // ?????¨??????cin?????????????????¨??¨cin??§???scanf?????????????????¨??¨scanf??§??±?????????????????????
  cin.tie(0);                  // cin??¨cout??????????????????
  ios::sync_with_stdio(false); // iostream??¨stdio??????????????????

  // read input data
  scand(N);
  int Q;
  scand(Q);

  //------------------------------------------------------------------------------------------
#ifdef debug
  // start timer
  auto startTime = chrono::system_clock::now();
#endif
  //------------------------------------------------------------------------------------------

  rep(i, 0, N) a[i] = INF;

  int command, x, y;
  rep(i, 0, Q) {
    scanf("%d%d%d", &command, &x, &y);

    if (command) {
      printf("%d\\n", find(x, y));
    } else {
      update(x, y);
    }
  }

  //------------------------------------------------------------------------------------------
#ifdef debug
  // stop timer
  auto endTime = chrono::system_clock::now();
  auto dur = endTime - startTime;
  auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
  dout << fixed << setprecision(4) << (double)msec / 1000 << " sec \\n";
#endif
  //------------------------------------------------------------------------------------------

#ifdef INPUTFILE
  inputfile.close();
#endif

#ifdef OUTPUTFILE
  outputfile.close();
  cout << "\\"" << OutputFilePath << "\\"" << endl;
#endif

  return 0;
}