Pagini recente » Cod sursa (job #3231515) | Cod sursa (job #2311506) | Cod sursa (job #2658592) | Cod sursa (job #2526777) | Cod sursa (job #2654923)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("br.in");
ofstream fout("br.out");
int N;
vector <int> V, sum;
int solve(int k, int x) {
int low = k - 1, high = k + N, mid;
while (high - low > 1) {
mid = (low + high) / 2;
if (sum[mid] < sum[k - 1] + x) {
low = mid;
} else {
high = mid;
}
}
if (high == k + N || V[high] != sum[k - 1] + x) {
return low - k + 1;
} else {
return high - k + 1;
}
}
int main() {
int T, s = 0;
V.push_back(s);
sum.push_back(s);
fin >> N >> T;
for (int x, i = 0; i < N; ++i) {
fin >> x;
s += x;
sum.push_back(s);
V.push_back(x);
}
for (int i = 1; i < N; ++i) {
s += V[i];
sum.push_back(s);
V.push_back(V[i]);
}
for (int k, x, i = 0; i < T; ++i) {
fin >> k >> x;
fout << solve(k, x) << '\n';
}
return 0;
}