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;
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 = M;

    auto it = upper_bound(accB.begin(), accB.end(), tmp);
    long long n = it - accB.begin();
    ans = max(ans, i + n - 1);
    /*
    while (r>l) {
        long long mid = (r-l) / 2;
        //cout << "mid=" << mid << endl;
        if (tmp > accB[mid]) {
            l = mid + 1;
            //cout << "l=" << l << endl;
            //cout << "r=" << r << endl;
        }
        else {
            r = mid - 1;
            //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;
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];
  }

  for (int i = 0; i <= N; i++) {
    // cout << "accA[i]=" << accA[i] << endl;
  }
  for (int i = 0; i <= M; i++) {
    // cout << "accB[i]=" << accB[i] << endl;
  }

  long long ans = 0;
  for (int i = 1; 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 = -1;
    r = M + 1;
    while (r > l) {
      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, result);
  }

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