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

// Tsundoku
#include <algorithm>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int INF = 1 << 29;

template <class T> bool isOK(vector<T> a, int index, T key) {
  if (a[index] > key) {
    return true;
  } else {
    return false;
  }
}

template <class T> T binarysearch(vector<T> a, T key) {
  int left = -1;
  int right = (int)a.size();
  while (abs(right - left) > 1) {
    int mid = left + (right - left) / 2;
    /*
    cout << "left " << left << endl;
    cout << "right " << right << endl;
    cout << "mid " << mid << endl;
    */
    if (isOK(a, mid, key)) {
      right = mid;
    } else {
      left = mid;
    }
  }
  // cout << "return " << right << endl;
  return right;
}

int main() {
  unsigned long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N);
  vector<long long> accA(N + 1, 0);
  // stack<int>A;

  // 累積和をつくる
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    accA[i + 1] = accA[i] + A[i];
  }
  vector<long long> B(M);
  vector<long long> accB(M + 1, 0);
  // stack<int> B;
  for (int j = 0; j < M; j++) {
    cin >> B[j];
    accB[j + 1] = accB[j] + B[j];
  }
  long long ans = 0;
  for (int i = 0; i <= N; i++) {
    // cout << "i=" << i << endl;
    long long tmp = K - accA[i];
    if (tmp < 0) {
      break;
    }
    // cout << "tmp=" << tmp << endl;
    long long result = 0;
    long long l, r;
    l = 0;
    r = accB.size();

    /*
    auto it = upper_bound(accB.begin(), accB.end(), tmp);
    long long n = it - accB.begin();
    ans = max(ans, i + n-1);
    */

    // long long res = binarysearch(accB, tmp) + i -1;
    // cout << res << endl;

    while (r - l > 1) {
      long long mid = l + (r - l) / 2;
      // cout << "mid=" << mid << endl;
      if (accB[mid] > tmp) {
        r = mid;
        // cout << "l=" << l << endl;
        // cout << "r=" << r << endl;
      } else {
        l = mid;
        // cout << "l=" << l << endl;
        // cout << "r=" << r << endl;
      }
    }
    result = i + l;

    // cout << "result=" << result << endl;
    ans = max(ans, result);
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

// Tsundoku
#include <algorithm>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int INF = 1 << 29;

template <class T> bool isOK(vector<T> a, int index, T key) {
  if (a[index] > key) {
    return true;
  } else {
    return false;
  }
}

template <class T> T binarysearch(vector<T> a, T key) {
  int left = -1;
  int right = (int)a.size();
  while (abs(right - left) > 1) {
    int mid = left + (right - left) / 2;
    /*
    cout << "left " << left << endl;
    cout << "right " << right << endl;
    cout << "mid " << mid << endl;
    */
    if (isOK(a, mid, key)) {
      right = mid;
    } else {
      left = mid;
    }
  }
  // cout << "return " << right << endl;
  return right;
}

int main() {
  unsigned long long N, M, K;
  cin >> N >> M >> K;
  vector<long long> A(N);
  vector<long long> accA(N + 1, 0);
  // stack<int>A;

  // 累積和をつくる
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    accA[i + 1] = accA[i] + A[i];
  }
  vector<long long> B(M);
  vector<long long> accB(M + 1, 0);
  // stack<int> B;
  for (int j = 0; j < M; j++) {
    cin >> B[j];
    accB[j + 1] = accB[j] + B[j];
  }
  long long ans = 0;
  for (int i = 0; i <= N; i++) {
    // cout << "i=" << i << endl;
    long long tmp = K - accA[i];
    if (tmp < 0) {
      break;
    }
    // cout << "tmp=" << tmp << endl;
    long long result = 0;
    long long l, r;
    l = 0;
    r = accB.size();

    /*
    auto it = upper_bound(accB.begin(), accB.end(), tmp);
    long long n = it - accB.begin();
    ans = max(ans, i + n-1);
    */

    long long res = binarysearch(accB, tmp) + i - 1;
    // cout << res << endl;
    /*
    while (r-l>1) {
        long long mid = (r+l) / 2;
        //cout << "mid=" << mid << endl;
        if (tmp > accB[mid]) {
            l = mid;
            //cout << "l=" << l << endl;
            //cout << "r=" << r << endl;
        }
        else {
            r = mid ;
            //cout << "l=" << l << endl;
            //cout << "r=" << r << endl;
        }
    }
    result = i + l;
    */
    // cout << "result=" << result << endl;
    ans = max(ans, res);
  }

  cout << ans << endl;
  return 0;
}