Code : Hungry Games
#include <bits/stdc++.h>
using namespace std;
void solve(vector<int> &a, int x) {
int n = a.size();
vector<int> cp_count(n, 0);
for (int L = n - 1; L >= 0; L--) {
long long g = 0;
for (int R = L; R < n; R++) {
g += a[R];
if (g > x) {
cp_count[L] = 1 + (R + 1 < n ? cp_count[R + 1] : 0);
break;
}
}
}
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += (n - i) - cp_count[i];
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
for (int zz = 0; zz < t; zz++) {
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
solve(a, x);
}
}
#include <bits/stdc++.h>
using namespace std;
void solve(vector<int> &a, int x) {
int n = a.size();
vector<long long> pref(n);
pref[0] = a[0];
for (int i = 1; i < n; i++) {
pref[i] = a[i] + pref[i - 1];
}
vector<int> cp_count(n, 0);
for (int L = n - 1; L >= 0; L--) {
long long offset = L ? pref[L - 1] : 0;
int R = upper_bound(pref.begin() + L, pref.end(), x + offset) -
pref.begin();
if (R < n) {
cp_count[L] = 1 + (R + 1 < n ? cp_count[R + 1] : 0);
}
}
long long ans = 0;
for (int L = 0; L < n; L++) {
ans += (n - L) - cp_count[L];
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
for (int zz = 0; zz < t; zz++) {
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
solve(a, x);
}
}