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

#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first;
#define snd second;
#define ALL(obj) (obj).begin(), (obj).end();
#define debug(x)                                                               \\
  cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\\n';
#define debugpair(x, y)                                                        \\
  cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y                \\
       << ") (line:" << __LINE__ << ")" << '\\n';
typedef long long lint;
typedef priority_queue<int> p_que;
typedef priority_queue<int, vector<int>, greater<int>()> p_que_rev;
const int INF = INT_MAX;
const lint LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector<lint> a(n);
  lint x;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int t = 0; t < q; ++t) {
    cin >> x;
    lint res = 0;
    int r = 0;
    lint sum = 0;
    for (int l = 0; l < n; ++l) {
      while (r < n && sum + a[r] <= x) {
        sum += a[r];
        ++r;
      }
      res += r - l;
      if (r == l) {
        ++r;
      } else {
        sum -= a[l];
      }
    }
    cout << res << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first;
#define snd second;
#define ALL(obj) (obj).begin(), (obj).end();
#define debug(x)                                                               \\
  cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\\n';
#define debugpair(x, y)                                                        \\
  cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y                \\
       << ") (line:" << __LINE__ << ")" << '\\n';
typedef long long lint;
typedef priority_queue<int> p_que;
typedef priority_queue<int, vector<int>, greater<int>()> p_que_rev;
const int INF = INT_MAX;
const lint LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const int di[]{0, -1, 0, 1, -1, -1, 1, 1};
const int dj[]{1, 0, -1, 0, 1, -1, -1, 1};

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector<lint> a(n);
  lint x;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < q; ++i) {
    cin >> x;
    lint ret = 0;
    // 区間の左端で場合わけ
    for (int l = 0; l < n; ++l) {
      lint sum = 0;
      for (int r = l; r <= n; ++r) {
        if (r != n) {
          sum += a[r];
        }
        if (sum <= x) {
          if (r == n) {
            ret += r - l;
          }
          continue;
        } else {
          ret += r - l;
          break;
        }
      }
    }
    cout << ret << endl;
  }
  return 0;
}