Created
September 4, 2019 20:42
-
-
Save Thiago4532/be2f6db80d1c6a82410b03e1767aa378 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int maxn = 5e5 + 10; | |
int n, k; | |
int v[maxn], pref[maxn]; | |
int main() { | |
ios::sync_with_stdio(false), cin.tie(0); // OTIMIZAÇÃO CIN E COUT | |
cin >> n >> k; | |
for (int i = 1; i <= n; i++) { | |
cin >> v[i]; | |
} | |
pref[1] = v[1]; // Inicializando o primeiro prefixo | |
for (int i = 2; i <= n; i++) { | |
pref[i] = pref[i-1] + v[i]; | |
} | |
for(int i=1;i<=n;i++) | |
cout << pref[i] << " \n"[i==n]; | |
long long ans = 0; // LONG LONG | |
for (int i = 1; i <= n; i++) { | |
// Subtrair o upper_bound pelo lower_bound retorna o numero de elementos que a gente deseja. | |
int resp = upper_bound(pref+i, pref+n+1, pref[i-1] + k) - lower_bound(pref+i, pref+n+1, pref[i-1] + k); | |
// cout << i << ": " << lower_bound(pref+i, pref+n+1, pref[i-1] + k) - pref << " " << upper_bound(pref+i, pref+n+1, pref[i-1] + k) - pref << " " << resp << "\n"; | |
ans += resp; | |
} | |
cout << ans << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment