Untitled

 avatar
unknown
plain_text
a year ago
1.0 kB
3
Indexable
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
 
const double PI = 3.14159265358979;
const ll INF =  1e18 + 7;
const ll MOD = 1e9 + 7;
const ll nax = 1000006;
const int LOG = 25;



void solve() {
    int n, k;
    cin >> n >> k;

    int a[n];
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    ll ans = 0;
    ll tail = 0, head = -1, currentSum = 0;

    while(tail < n) {
        while(head + 1 < n && currentSum + a[head + 1] <= k) {
            head++;
            currentSum += a[head];
        }

        if (head - tail + 1 > 0) {
            ans += (head - tail + 1);
        }

        if (tail <= head) {
            currentSum -= a[tail];
            tail++;
        } else {
            tail++;
            head = tail - 1;
        }
    }

    cout << ans << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t; cin >> t; while(t--)
    solve();
    return 0;
}
Editor is loading...
Leave a Comment