output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; int arr[(int)1e5 + 1]; int q[(int)1e5 + 1]; int sol[(int)1e5 + 1]; int main() { int n, m; cin >> n; cin >> arr[0]; for (int i = 1; i < n; i++) { int x; cin >> x; arr[i] = arr[i - 1] + x; } cin >> m; for (int i = 0; i < m; i++) { cin >> q[i]; } for (int i = 0; i < m; i++) { int lo = 0, hi = n - 1, res = -1; while (lo <= hi) { int md = (hi + lo) >> 1; if (arr[md] >= q[i] && q[i] >= arr[md - 1]) { hi = md - 1; res = md; } else if (arr[md] >= q[i]) hi = md - 1; else lo = md + 1; } sol[i] = res + 1; } for (int i = 0; i < m; i++) { cout << sol[i] << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int arr[(int)1e5 + 1]; int q[(int)1e5 + 1]; int sol[(int)1e5 + 1]; int main() { int n, m; cin >> n; cin >> arr[0]; for (int i = 1; i < n; i++) { int x; cin >> x; arr[i] = arr[i - 1] + x; } cin >> m; for (int i = 0; i < m; i++) { cin >> q[i]; } for (int i = 0; i < m; i++) { int lo = 0, hi = n - 1, res = -1; while (lo <= hi) { int md = (hi + lo) >> 1; if (arr[md] >= q[i] && q[i] >= arr[md - 1]) { hi = md - 1; res = md; } else if (arr[md] >= q[i]) hi = md - 1; else lo = md + 1; } sol[i] = res + 1; } for (int i = 0; i < m; i++) { cout << sol[i] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int q[n], m, sum = 0; q[0] = 0; for (long long int i = 1; i <= n; i++) { cin >> m; q[i] = q[i - 1] + m; sum += m; } long long int arr[sum + 1], index = 1; for (long long int i = 1; i <= sum; i++) { if (i > q[index]) index++; arr[i] = index; } long long int r; cin >> r; while (r--) { long long int s; cin >> s; cout << arr[s] << endl; } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int q[n], m, sum = 0; q[0] = 0; for (long long int i = 1; i <= n; i++) { cin >> m; q[i] = q[i - 1] + m; sum += m; } long long int arr[sum + 1], index = 1; for (long long int i = 1; i <= sum; i++) { if (i > q[index]) index++; arr[i] = index; } long long int r; cin >> r; while (r--) { long long int s; cin >> s; cout << arr[s] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long n, m, temp, p, q; int main() { cin >> n; long a[n]; for (int i = 0; i < n; i++) { cin >> p; temp += p; a[i] = temp; } cin >> m; long b[m]; for (int i = 0; i < m; i++) { cin >> p; q = lower_bound(a, a + n, p) - a; b[i] = q + 1; } for (int i = 0; i < m; i++) { cout << b[i] << endl; } }
### Prompt Please formulate a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long n, m, temp, p, q; int main() { cin >> n; long a[n]; for (int i = 0; i < n; i++) { cin >> p; temp += p; a[i] = temp; } cin >> m; long b[m]; for (int i = 0; i < m; i++) { cin >> p; q = lower_bound(a, a + n, p) - a; b[i] = q + 1; } for (int i = 0; i < m; i++) { cout << b[i] << endl; } } ```
#include <bits/stdc++.h> using namespace std; int num[100007]; int main() { vector<pair<int, int> > data; int curr = 0; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> num[i]; data.push_back(make_pair(curr + 1, curr + num[i])); curr = curr + num[i]; } int m; cin >> m; int x; for (int i = 0; i < m; i++) { cin >> x; int s = 0, e = n - 1; int ans; while (s <= e) { int mid = s + (e - s) / 2; if (x >= data[mid].first && x <= data[mid].second) { ans = mid + 1; break; } else if (x >= data[mid].first && x >= data[mid].second) { s = mid + 1; } else e = mid - 1; } cout << ans << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int num[100007]; int main() { vector<pair<int, int> > data; int curr = 0; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> num[i]; data.push_back(make_pair(curr + 1, curr + num[i])); curr = curr + num[i]; } int m; cin >> m; int x; for (int i = 0; i < m; i++) { cin >> x; int s = 0, e = n - 1; int ans; while (s <= e) { int mid = s + (e - s) / 2; if (x >= data[mid].first && x <= data[mid].second) { ans = mid + 1; break; } else if (x >= data[mid].first && x >= data[mid].second) { s = mid + 1; } else e = mid - 1; } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; int main() { { ios_base::sync_with_stdio(false); cin.tie(NULL); } int n; cin >> n; int inp, prev = 1; int *table = (int *)malloc(sizeof(int) * 1000001); for (long long i = 0; i < n; i++) { cin >> inp; for (int j = prev; j <= prev + inp + 1; j++) { table[j] = i + 1; } prev += inp; } cin >> n; for (long long i = 0; i < n; i++) { cin >> inp; cout << table[inp] << "\n"; } free(table); return 0; }
### Prompt Create a solution in CPP for the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; int main() { { ios_base::sync_with_stdio(false); cin.tie(NULL); } int n; cin >> n; int inp, prev = 1; int *table = (int *)malloc(sizeof(int) * 1000001); for (long long i = 0; i < n; i++) { cin >> inp; for (int j = prev; j <= prev + inp + 1; j++) { table[j] = i + 1; } prev += inp; } cin >> n; for (long long i = 0; i < n; i++) { cin >> inp; cout << table[inp] << "\n"; } free(table); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); long long location = 1; long long a_aux; int array[n][2]; for (int i = 0; i < n; i++) { scanf("%d", &a_aux); array[i][0] = location; array[i][1] = a_aux + location - 1; location += a_aux; } int m, start, end, mid; scanf("%d", &m); location = 0; int nasli = n; for (int i = 0; i < m; i++) { scanf("%d", &location); mid = (nasli / 2); start = 0; end = nasli - 1; while (true) { if (location >= array[mid][0] && location <= array[mid][1]) { printf("%d\n", mid + 1); break; } else if (location > array[mid][1]) { start = mid; } else { end = mid; } if ((start + end) % 2 == 0) { mid = (start + end) / 2; } else if ((start + end) / 2 == mid) { mid = (start + end + 1) / 2; } else { mid = (start + end - 1) / 2; } } } }
### Prompt Your challenge is to write a Cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); long long location = 1; long long a_aux; int array[n][2]; for (int i = 0; i < n; i++) { scanf("%d", &a_aux); array[i][0] = location; array[i][1] = a_aux + location - 1; location += a_aux; } int m, start, end, mid; scanf("%d", &m); location = 0; int nasli = n; for (int i = 0; i < m; i++) { scanf("%d", &location); mid = (nasli / 2); start = 0; end = nasli - 1; while (true) { if (location >= array[mid][0] && location <= array[mid][1]) { printf("%d\n", mid + 1); break; } else if (location > array[mid][1]) { start = mid; } else { end = mid; } if ((start + end) % 2 == 0) { mid = (start + end) / 2; } else if ((start + end) / 2 == mid) { mid = (start + end + 1) / 2; } else { mid = (start + end - 1) / 2; } } } } ```
#include <bits/stdc++.h> int arr[1100000]; using namespace std; int main() { memset(arr, 0, sizeof arr); int n; cin >> n; int x = 1, pre = 1; for (int i = 1; i <= n; i++) { int c; cin >> c; arr[x] = i; x += c; } x = arr[1]; for (int i = 1; i <= 1000000; i++) { if (!arr[i]) arr[i] = x; else x = arr[i]; } int q; cin >> q; while (q--) { cin >> x; cout << arr[x] << endl; } }
### Prompt Develop a solution in CPP to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> int arr[1100000]; using namespace std; int main() { memset(arr, 0, sizeof arr); int n; cin >> n; int x = 1, pre = 1; for (int i = 1; i <= n; i++) { int c; cin >> c; arr[x] = i; x += c; } x = arr[1]; for (int i = 1; i <= 1000000; i++) { if (!arr[i]) arr[i] = x; else x = arr[i]; } int q; cin >> q; while (q--) { cin >> x; cout << arr[x] << endl; } } ```
#include <bits/stdc++.h> using namespace std; int dp[1000000]; int main() { int n; cin >> n; int arr[n]; int sum = 0; int count = 1; int k; for (int i = 0; i < n; i++) cin >> k, sum += k, dp[i + 1] = sum; int q; cin >> q; while (q--) { int left = 0, right = n; int a; cin >> a; while (left <= right) { int mid = (left + right) / 2; if (dp[mid] < a) left = mid + 1; else right = mid - 1; } cout << left << "\n"; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dp[1000000]; int main() { int n; cin >> n; int arr[n]; int sum = 0; int count = 1; int k; for (int i = 0; i < n; i++) cin >> k, sum += k, dp[i + 1] = sum; int q; cin >> q; while (q--) { int left = 0, right = n; int a; cin >> a; while (left <= right) { int mid = (left + right) / 2; if (dp[mid] < a) left = mid + 1; else right = mid - 1; } cout << left << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> arr(n); cin >> arr[0]; for (int i = 1; i < n; ++i) { cin >> arr[i]; arr[i] += arr[i - 1]; } sort(arr.begin(), arr.end()); int m; cin >> m; for (int i = 0; i < m; ++i) { int a; cin >> a; cout << lower_bound(arr.begin(), arr.end(), a) - arr.begin() + 1 << endl; } }
### Prompt Develop a solution in CPP to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> arr(n); cin >> arr[0]; for (int i = 1; i < n; ++i) { cin >> arr[i]; arr[i] += arr[i - 1]; } sort(arr.begin(), arr.end()); int m; cin >> m; for (int i = 0; i < m; ++i) { int a; cin >> a; cout << lower_bound(arr.begin(), arr.end(), a) - arr.begin() + 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; long long int a[1000001]; int main() { long long int n; cin >> n; int _count = 1; for (int i = 0; i < n; i++) { long long int x; cin >> x; for (int j = 0; j < x; j++) { a[_count++] = i; } } long long int x; cin >> x; while (x--) { long long int m; cin >> m; cout << a[m] + 1 << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int a[1000001]; int main() { long long int n; cin >> n; int _count = 1; for (int i = 0; i < n; i++) { long long int x; cin >> x; for (int j = 0; j < x; j++) { a[_count++] = i; } } long long int x; cin >> x; while (x--) { long long int m; cin >> m; cout << a[m] + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; while (TESTS--) { int n; cin >> n; vector<long long int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; int m; cin >> m; vector<long long int> b(m); for (long long int i = 0; i < m; i++) cin >> b[i]; vector<long long int> c(n); c[0] = a[0]; for (long long int i = 1; i < n; i++) { c[i] = c[i - 1] + a[i]; } for (long long int i = 0; i < m; i++) { if (binary_search((c).begin(), (c).end(), b[i])) { cout << lower_bound((c).begin(), (c).end(), b[i]) - c.begin() + 1 << '\n'; } else { cout << upper_bound((c).begin(), (c).end(), b[i]) - c.begin() + 1 << '\n'; } } } return 0; }
### Prompt In Cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; while (TESTS--) { int n; cin >> n; vector<long long int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; int m; cin >> m; vector<long long int> b(m); for (long long int i = 0; i < m; i++) cin >> b[i]; vector<long long int> c(n); c[0] = a[0]; for (long long int i = 1; i < n; i++) { c[i] = c[i - 1] + a[i]; } for (long long int i = 0; i < m; i++) { if (binary_search((c).begin(), (c).end(), b[i])) { cout << lower_bound((c).begin(), (c).end(), b[i]) - c.begin() + 1 << '\n'; } else { cout << upper_bound((c).begin(), (c).end(), b[i]) - c.begin() + 1 << '\n'; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { vector<vector<int> > quest(100006, vector<int>(2)); int n, i, a[100006], m, num, ans[100006], j = 0, sum = 0; cin >> n; for (i = 0; i < n; ++i) cin >> a[i]; cin >> m; for (i = 0; i < m; ++i) { cin >> num; quest[i][0] = num; quest[i][1] = i; } sort(quest.begin(), quest.begin() + m); for (i = 0; i < m; ++i) { if (sum < quest[i][0]) { --i; sum += a[j]; ++j; } else { ans[quest[i][1]] = j; } } for (i = 0; i < m; ++i) cout << ans[i] << endl; return 0; }
### Prompt Develop a solution in cpp to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { vector<vector<int> > quest(100006, vector<int>(2)); int n, i, a[100006], m, num, ans[100006], j = 0, sum = 0; cin >> n; for (i = 0; i < n; ++i) cin >> a[i]; cin >> m; for (i = 0; i < m; ++i) { cin >> num; quest[i][0] = num; quest[i][1] = i; } sort(quest.begin(), quest.begin() + m); for (i = 0; i < m; ++i) { if (sum < quest[i][0]) { --i; sum += a[j]; ++j; } else { ans[quest[i][1]] = j; } } for (i = 0; i < m; ++i) cout << ans[i] << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, a[100050], m, q, in = 0; map<int, int> b; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 1; i < n; ++i) a[i] += a[i - 1]; for (int i = 0; i < a[n - 1] + 1; ++i) { if (i > a[in]) ++in; b[i] = in + 1; } cin >> m; for (int i = 0; i < m; ++i) { cin >> q; cout << b[q] << endl; } return 0; }
### Prompt Generate a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, a[100050], m, q, in = 0; map<int, int> b; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 1; i < n; ++i) a[i] += a[i - 1]; for (int i = 0; i < a[n - 1] + 1; ++i) { if (i > a[in]) ++in; b[i] = in + 1; } cin >> m; for (int i = 0; i < m; ++i) { cin >> q; cout << b[q] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); int n; cin >> n; int arr[n]; for (int &i : arr) cin >> i; for (long long int i = (1); i < (n); i++) { arr[i] += arr[i - 1]; } int q; cin >> q; while (q--) { int x; cin >> x; int ans = lower_bound(arr, arr + n, x) - arr; cout << ans + 1 << "\n"; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); int n; cin >> n; int arr[n]; for (int &i : arr) cin >> i; for (long long int i = (1); i < (n); i++) { arr[i] += arr[i - 1]; } int q; cin >> q; while (q--) { int x; cin >> x; int ans = lower_bound(arr, arr + n, x) - arr; cout << ans + 1 << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; pair<int, int> a[100005]; int n, q, x = 1, b[100005], h; int find(int k) { int l = 0, r = n - 1; while (l <= r) { h++; int m = (l + r) / 2; if ((a[m].first == k) || (a[m].second == k) || (a[m].first < k && a[m].second > k)) return m; if (a[m].first > k) r = m - 1; else l = m + 1; } } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> b[i]; a[i].first = x; a[i].second = x + b[i] - 1; x += b[i]; } for (cin >> q; q--;) { cin >> x; cout << find(x) + 1 << endl; } }
### Prompt Please provide a Cpp coded solution to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; pair<int, int> a[100005]; int n, q, x = 1, b[100005], h; int find(int k) { int l = 0, r = n - 1; while (l <= r) { h++; int m = (l + r) / 2; if ((a[m].first == k) || (a[m].second == k) || (a[m].first < k && a[m].second > k)) return m; if (a[m].first > k) r = m - 1; else l = m + 1; } } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> b[i]; a[i].first = x; a[i].second = x + b[i] - 1; x += b[i]; } for (cin >> q; q--;) { cin >> x; cout << find(x) + 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int n, m, a[100000], k, l, r, c, i; int main() { std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> a[0]; for (i = 1; i < n; i++) { cin >> a[i]; a[i] += a[i - 1]; } cin >> m; while (m--) { cin >> k; l = 0; r = n - 1; while (l < r) { c = (l + r) / 2; if (k == a[c]) { l = c; r = l; } else if (k < a[c]) r = c; else l = c + 1; } cout << l + 1 << endl; } return 0; }
### Prompt Develop a solution in CPP to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, a[100000], k, l, r, c, i; int main() { std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> a[0]; for (i = 1; i < n; i++) { cin >> a[i]; a[i] += a[i - 1]; } cin >> m; while (m--) { cin >> k; l = 0; r = n - 1; while (l < r) { c = (l + r) / 2; if (k == a[c]) { l = c; r = l; } else if (k < a[c]) r = c; else l = c + 1; } cout << l + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int A = 1e5 + 5; long long int n, a[A], s[A], q, x, l, r, mid; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> x; s[i] = (s[i - 1] + x); } cin >> q; for (int i = 1; i <= q; i++) { cin >> x; l = 1; r = n; while (l != r - 1) { mid = (l + r) / 2; if (x <= s[mid]) { r = mid; } else { l = mid; } } if (s[l] < x && s[l + 1] >= x) { cout << l + 1 << endl; } else { while (s[l] >= x) { l--; } cout << l + 1 << endl; } } }
### Prompt Construct a cpp code solution to the problem outlined: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int A = 1e5 + 5; long long int n, a[A], s[A], q, x, l, r, mid; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> x; s[i] = (s[i - 1] + x); } cin >> q; for (int i = 1; i <= q; i++) { cin >> x; l = 1; r = n; while (l != r - 1) { mid = (l + r) / 2; if (x <= s[mid]) { r = mid; } else { l = mid; } } if (s[l] < x && s[l + 1] >= x) { cout << l + 1 << endl; } else { while (s[l] >= x) { l--; } cout << l + 1 << endl; } } } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n, m, ans, a[N], sum, x; map<int, int> map1; vector<pair<int, int>> p; set<int> set1; int main() { cin >> n >> a[0]; p.push_back({1, a[0]}); for (int i = 1; i < n; i++) { cin >> a[i]; p.push_back({p[i - 1].second + 1, a[i] + p[i - 1].second}); } cin >> m; for (int i = 0; i < m; i++) { int L = 0, R = p.size(); cin >> x; while (R >= L) { int mid = L + (R - L) / 2; if (x >= p[mid].first && x <= p[mid].second) { ans = mid + 1; break; } else if (x > p[mid].second) { L = mid + 1; } else { R = mid - 1; } } cout << ans << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n, m, ans, a[N], sum, x; map<int, int> map1; vector<pair<int, int>> p; set<int> set1; int main() { cin >> n >> a[0]; p.push_back({1, a[0]}); for (int i = 1; i < n; i++) { cin >> a[i]; p.push_back({p[i - 1].second + 1, a[i] + p[i - 1].second}); } cin >> m; for (int i = 0; i < m; i++) { int L = 0, R = p.size(); cin >> x; while (R >= L) { int mid = L + (R - L) / 2; if (x >= p[mid].first && x <= p[mid].second) { ans = mid + 1; break; } else if (x > p[mid].second) { L = mid + 1; } else { R = mid - 1; } } cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int numOfPils, NumOfGood; cin >> numOfPils; int(*arrayOfPils)[3] = new int[numOfPils][3]; int temp = 0; for (int i = 0; i < numOfPils; i++) { cin >> arrayOfPils[i][0]; if (i == 0) { arrayOfPils[0][1] = 1; arrayOfPils[0][2] = arrayOfPils[0][0]; temp = arrayOfPils[0][0]; } else { arrayOfPils[i][1] = temp + 1; arrayOfPils[i][2] = arrayOfPils[i][0] + temp; temp = arrayOfPils[i][2]; } } cin >> NumOfGood; int* Marks = new int[NumOfGood]; for (int i = 0; i < NumOfGood; i++) { cin >> Marks[i]; } for (int j = 0; j < NumOfGood; j++) { int F = 0; int L = numOfPils; int M = (F + L) / 2; while (F <= L) { M = (F + L) / 2; if (Marks[j] < arrayOfPils[M][1]) { L = M - 1; } else if (Marks[j] > arrayOfPils[M][2]) { F = M + 1; } else if ((Marks[j] < arrayOfPils[M][2] || Marks[j] == arrayOfPils[M][2]) && (Marks[j] > arrayOfPils[M][1] || Marks[j] == arrayOfPils[M][1])) { cout << M + 1 << endl; break; } } } delete arrayOfPils; return 0; }
### Prompt Generate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int numOfPils, NumOfGood; cin >> numOfPils; int(*arrayOfPils)[3] = new int[numOfPils][3]; int temp = 0; for (int i = 0; i < numOfPils; i++) { cin >> arrayOfPils[i][0]; if (i == 0) { arrayOfPils[0][1] = 1; arrayOfPils[0][2] = arrayOfPils[0][0]; temp = arrayOfPils[0][0]; } else { arrayOfPils[i][1] = temp + 1; arrayOfPils[i][2] = arrayOfPils[i][0] + temp; temp = arrayOfPils[i][2]; } } cin >> NumOfGood; int* Marks = new int[NumOfGood]; for (int i = 0; i < NumOfGood; i++) { cin >> Marks[i]; } for (int j = 0; j < NumOfGood; j++) { int F = 0; int L = numOfPils; int M = (F + L) / 2; while (F <= L) { M = (F + L) / 2; if (Marks[j] < arrayOfPils[M][1]) { L = M - 1; } else if (Marks[j] > arrayOfPils[M][2]) { F = M + 1; } else if ((Marks[j] < arrayOfPils[M][2] || Marks[j] == arrayOfPils[M][2]) && (Marks[j] > arrayOfPils[M][1] || Marks[j] == arrayOfPils[M][1])) { cout << M + 1 << endl; break; } } } delete arrayOfPils; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, m, t, sum(0); cin >> n; int counts[n]; for (int i = 0; i < n; i++) { cin >> t; sum += t; counts[i] = sum; } int ind(0), worms[sum + 1]; for (int i = 1; i <= sum; i++) { if (i > counts[ind]) ind++; worms[i] = ind + 1; } cin >> m; for (int i = 0; i < m; i++) { cin >> t; cout << worms[t] << endl; } return 0; }
### Prompt Create a solution in Cpp for the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, t, sum(0); cin >> n; int counts[n]; for (int i = 0; i < n; i++) { cin >> t; sum += t; counts[i] = sum; } int ind(0), worms[sum + 1]; for (int i = 1; i <= sum; i++) { if (i > counts[ind]) ind++; worms[i] = ind + 1; } cin >> m; for (int i = 0; i < m; i++) { cin >> t; cout << worms[t] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; static int n, m, a, q; static int partial[100001]; int find_position(int k) { int a = 0; int b = n - 1; while (a < b) { if (partial[(a + b + 1) / 2] >= k) b = (a + b + 1) / 2 - 1; else a = (a + b + 1) / 2; } return a; } int main() { cin >> n; partial[0] = 0; for (int i = 0; i < n; i++) { cin >> a; partial[i + 1] = partial[i] + a; } cin >> m; for (int i = 0; i < m; i++) { cin >> q; cout << find_position(q) + 1 << '\n'; } return 0; }
### Prompt Create a solution in cpp for the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; static int n, m, a, q; static int partial[100001]; int find_position(int k) { int a = 0; int b = n - 1; while (a < b) { if (partial[(a + b + 1) / 2] >= k) b = (a + b + 1) / 2 - 1; else a = (a + b + 1) / 2; } return a; } int main() { cin >> n; partial[0] = 0; for (int i = 0; i < n; i++) { cin >> a; partial[i + 1] = partial[i] + a; } cin >> m; for (int i = 0; i < m; i++) { cin >> q; cout << find_position(q) + 1 << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, i, m, j, cnt = 0, ans, x, sum = 0; cin >> n; int a[n], c[n]; for (i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; c[i] = sum; } cin >> m; int b[m]; for (i = 0; i < m; i++) cin >> b[i]; for (j = 0; j < m; j++) { cout << lower_bound(c, c + n, b[j]) - c + 1 << endl; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, i, m, j, cnt = 0, ans, x, sum = 0; cin >> n; int a[n], c[n]; for (i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; c[i] = sum; } cin >> m; int b[m]; for (i = 0; i < m; i++) cin >> b[i]; for (j = 0; j < m; j++) { cout << lower_bound(c, c + n, b[j]) - c + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5 + 1; int a[MAX_N]; int q[MAX_N]; int c[2000005]; int main() { int n, m; cin >> n; int s = 0; int cnt = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; for (int j = 0; j < a[i]; j++) { c[++cnt] = i; } } vector<int> v; cin >> m; for (int i = 0; i < m; i++) { cin >> q[i]; cout << (c[q[i]]) << endl; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5 + 1; int a[MAX_N]; int q[MAX_N]; int c[2000005]; int main() { int n, m; cin >> n; int s = 0; int cnt = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; for (int j = 0; j < a[i]; j++) { c[++cnt] = i; } } vector<int> v; cin >> m; for (int i = 0; i < m; i++) { cin >> q[i]; cout << (c[q[i]]) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, m, b[100000], a[100000], i, j = 0, k = 1, ans = 1; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; cin >> m; for (i = 0; i < m; i++) cin >> b[i]; for (i = 1; i < n; i++) a[i] = a[i] + a[i - 1]; for (i = 0; i < m; i++) { k = b[i]; if (a[upper_bound(a, a + n, k) - a - 1] == k) cout << upper_bound(a, a + n, k) - a << endl; else cout << upper_bound(a, a + n, k) - a + 1 << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, b[100000], a[100000], i, j = 0, k = 1, ans = 1; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; cin >> m; for (i = 0; i < m; i++) cin >> b[i]; for (i = 1; i < n; i++) a[i] = a[i] + a[i - 1]; for (i = 0; i < m; i++) { k = b[i]; if (a[upper_bound(a, a + n, k) - a - 1] == k) cout << upper_bound(a, a + n, k) - a << endl; else cout << upper_bound(a, a + n, k) - a + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, a, m; cin >> n; long long int k[100001]; k[0] = 0; for (int i = 1; i <= n; i++) { cin >> a; k[i] = k[i - 1] + a; } cin >> m; for (int i = 0; i < m; i++) { cin >> a; int ml = 1, mr = n, mid; while (ml <= mr) { mid = ml + (mr - ml) / 2; if (a > k[mr - 1] && a <= k[mr]) { cout << mr << endl; break; } else if (a <= k[mid]) { mr = mid; } else { ml = mid; } } } return 0; }
### Prompt In cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, a, m; cin >> n; long long int k[100001]; k[0] = 0; for (int i = 1; i <= n; i++) { cin >> a; k[i] = k[i - 1] + a; } cin >> m; for (int i = 0; i < m; i++) { cin >> a; int ml = 1, mr = n, mid; while (ml <= mr) { mid = ml + (mr - ml) / 2; if (a > k[mr - 1] && a <= k[mr]) { cout << mr << endl; break; } else if (a <= k[mid]) { mr = mid; } else { ml = mid; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long binarySearch(int arr[], long long l, long long r, long long x) { if (r > l) { long long mid = l + (r - l) / 2; if (arr[mid] >= x) return binarySearch(arr, l, mid, x); else return binarySearch(arr, mid + 1, r, x); } else { return r; } } int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int dp[n]; dp[0] = arr[0]; for (int i = 1; i < n; i++) { dp[i] = arr[i] + dp[i - 1]; } int t; cin >> t; while (t--) { int x; cin >> x; cout << binarySearch(dp, 0, n - 1, x) + 1 << endl; } }
### Prompt Please formulate a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long binarySearch(int arr[], long long l, long long r, long long x) { if (r > l) { long long mid = l + (r - l) / 2; if (arr[mid] >= x) return binarySearch(arr, l, mid, x); else return binarySearch(arr, mid + 1, r, x); } else { return r; } } int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int dp[n]; dp[0] = arr[0]; for (int i = 1; i < n; i++) { dp[i] = arr[i] + dp[i - 1]; } int t; cin >> t; while (t--) { int x; cin >> x; cout << binarySearch(dp, 0, n - 1, x) + 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, x, sum = 0, q; vector<int> v; vector<int>::iterator it; scanf("%d", &n); while (n--) { scanf("%d", &x); sum += x; v.push_back(sum); } int sz = v.size(); scanf("%d", &q); while (q--) { scanf("%d", &x); it = lower_bound(v.begin(), v.end(), x); printf("%d\n", (it - v.begin()) + 1); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, x, sum = 0, q; vector<int> v; vector<int>::iterator it; scanf("%d", &n); while (n--) { scanf("%d", &x); sum += x; v.push_back(sum); } int sz = v.size(); scanf("%d", &q); while (q--) { scanf("%d", &x); it = lower_bound(v.begin(), v.end(), x); printf("%d\n", (it - v.begin()) + 1); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, m; int a[N]; set<pair<int, int> > s; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i - 1]; pair<int, int> p = {a[i], i}; s.insert(p); } cin >> m; for (int i = 1; i <= m; i++) { int k; cin >> k; pair<int, int> p = {k, 0}; auto it = s.lower_bound(p); cout << it->second << "\n"; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, m; int a[N]; set<pair<int, int> > s; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i - 1]; pair<int, int> p = {a[i], i}; s.insert(p); } cin >> m; for (int i = 1; i <= m; i++) { int k; cin >> k; pair<int, int> p = {k, 0}; auto it = s.lower_bound(p); cout << it->second << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m; int main() { int n; scanf("%d", &n); vector<int> v(n), sum(n); int prev = 0; for (int i = 0; i < n; i++) { scanf("%d", &v[i]); sum[i] = v[i] + prev; prev = sum[i]; } int q; scanf("%d", &q); for (int i = 0; i < q; i++) { int temp; scanf("%d", &temp); int loc = lower_bound(sum.begin(), sum.end(), temp) - sum.begin(); cout << loc + 1 << endl; } }
### Prompt Please provide a CPP coded solution to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m; int main() { int n; scanf("%d", &n); vector<int> v(n), sum(n); int prev = 0; for (int i = 0; i < n; i++) { scanf("%d", &v[i]); sum[i] = v[i] + prev; prev = sum[i]; } int q; scanf("%d", &q); for (int i = 0; i < q; i++) { int temp; scanf("%d", &temp); int loc = lower_bound(sum.begin(), sum.end(), temp) - sum.begin(); cout << loc + 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; struct ii { int l, r; ii() {} ii(int l, int r) : l(l), r(r) {} } v[100100]; int n; int bb(int val) { int l = 0, r = n - 1, mid = 0; while (l <= r) { mid = (l + r) / 2; if (val >= v[mid].l && val <= v[mid].r) break; if (val < v[mid].l) r = mid; else l = mid + 1; } return mid; } int main() { int val, m; scanf("%d", &n); int aux = 0, sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &val); sum += val; v[i] = ii(aux + 1, sum); aux = sum; } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &val); printf("%d\n", bb(val) + 1); } }
### Prompt Please provide a cpp coded solution to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct ii { int l, r; ii() {} ii(int l, int r) : l(l), r(r) {} } v[100100]; int n; int bb(int val) { int l = 0, r = n - 1, mid = 0; while (l <= r) { mid = (l + r) / 2; if (val >= v[mid].l && val <= v[mid].r) break; if (val < v[mid].l) r = mid; else l = mid + 1; } return mid; } int main() { int val, m; scanf("%d", &n); int aux = 0, sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &val); sum += val; v[i] = ii(aux + 1, sum); aux = sum; } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &val); printf("%d\n", bb(val) + 1); } } ```
#include <bits/stdc++.h> using namespace std; int n, i, a, b, j, x = 1, c, d[1000001]; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a; for (j = x; j <= x + a - 1; j++) { d[j] = i; } x = x + a; } cin >> b; for (i = 1; i <= b; i++) { cin >> a; cout << d[a] << endl; } }
### Prompt Generate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, i, a, b, j, x = 1, c, d[1000001]; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a; for (j = x; j <= x + a - 1; j++) { d[j] = i; } x = x + a; } cin >> b; for (i = 1; i <= b; i++) { cin >> a; cout << d[a] << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k, sum = 0, i, j, ara[100000], mid, low, high; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &j); sum += j; ara[i] = sum; } scanf("%d", &m); for (i = 0; i < m; i++) { scanf("%d", &k); low = 0; high = n - 1; for (j = 0;; j++) { mid = (high + low) / 2; if (ara[mid] == k) { printf("%d\n", mid + 1); break; } else if (ara[mid] > k) high = mid; else if (ara[mid] < k) low = mid + 1; if (high == low) { printf("%d\n", low + 1); break; } } } return 0; }
### Prompt In Cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, k, sum = 0, i, j, ara[100000], mid, low, high; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &j); sum += j; ara[i] = sum; } scanf("%d", &m); for (i = 0; i < m; i++) { scanf("%d", &k); low = 0; high = n - 1; for (j = 0;; j++) { mid = (high + low) / 2; if (ara[mid] == k) { printf("%d\n", mid + 1); break; } else if (ara[mid] > k) high = mid; else if (ara[mid] < k) low = mid + 1; if (high == low) { printf("%d\n", low + 1); break; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0) return 1; if (b == 1) return mod(a, m); long long k = mod(exp(a, b / 2, m), m); if (b & 1) { return mod(a * mod(k * k, m), m); } else return mod(k * k, m); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long cnt = 1; set<pair<long long, long long> > s; for (long long i = 0; i < n; i++) { long long x; cin >> x; for (long long j = 1; j <= x; j++) { s.insert({cnt, i + 1}); cnt++; } } long long m; cin >> m; for (long long i = 0; i < m; i++) { long long k; cin >> k; auto x = s.lower_bound({k, 0}); cout << (*x).second << "\n"; } }
### Prompt Create a solution in CPP for the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0) return 1; if (b == 1) return mod(a, m); long long k = mod(exp(a, b / 2, m), m); if (b & 1) { return mod(a * mod(k * k, m), m); } else return mod(k * k, m); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long cnt = 1; set<pair<long long, long long> > s; for (long long i = 0; i < n; i++) { long long x; cin >> x; for (long long j = 1; j <= x; j++) { s.insert({cnt, i + 1}); cnt++; } } long long m; cin >> m; for (long long i = 0; i < m; i++) { long long k; cin >> k; auto x = s.lower_bound({k, 0}); cout << (*x).second << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int chk[1000002] = {0}; int g; int main() { int n; cin >> n; int a[n], suf[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } suf[0] = a[0]; int k = 1, d; for (int j = 0; j < n; j++) { for (int i = 0; i < a[j]; i++) chk[k++] = j + 1; } cin >> d; while (d--) { cin >> g; int l = 1, h = 1000002, md; while (l <= h) { md = (l + h) >> 1; if (g > md) l = md + 1; else if (g < md) h = md - 1; else { cout << chk[g] << "\n"; break; } } } }
### Prompt Please create a solution in Cpp to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int chk[1000002] = {0}; int g; int main() { int n; cin >> n; int a[n], suf[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } suf[0] = a[0]; int k = 1, d; for (int j = 0; j < n; j++) { for (int i = 0; i < a[j]; i++) chk[k++] = j + 1; } cin >> d; while (d--) { cin >> g; int l = 1, h = 1000002, md; while (l <= h) { md = (l + h) >> 1; if (g > md) l = md + 1; else if (g < md) h = md - 1; else { cout << chk[g] << "\n"; break; } } } } ```
#include <bits/stdc++.h> const int INF = 500000001; const double EPS = 1e-9; const double PI = acos(-1.0); using namespace std; int main() { int n, ans[1000005]; int s, e, tmp, m, k, cnt; while (~scanf("%d", &n)) { k = cnt = 1; for (int i = 0; i < n; i++) { scanf("%d", &tmp); for (int j = k; j < k + tmp; j++) { ans[j] = cnt; } k = k + tmp; ++cnt; } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &tmp); printf("%d\n", ans[tmp]); } } return 0; }
### Prompt Develop a solution in cpp to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> const int INF = 500000001; const double EPS = 1e-9; const double PI = acos(-1.0); using namespace std; int main() { int n, ans[1000005]; int s, e, tmp, m, k, cnt; while (~scanf("%d", &n)) { k = cnt = 1; for (int i = 0; i < n; i++) { scanf("%d", &tmp); for (int j = k; j < k + tmp; j++) { ans[j] = cnt; } k = k + tmp; ++cnt; } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &tmp); printf("%d\n", ans[tmp]); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, m, sum = 0; cin >> n; vector<int> v(n); for (i = 0; i < n; i++) { cin >> v[i]; } for (i = 1; i < n; i++) { v[i] = v[i] + v[i - 1]; } cin >> m; long long b[m]; for (i = 0; i < m; i++) cin >> b[i]; for (i = 0; i < m; i++) { int a = lower_bound(v.begin(), v.end(), b[i]) - v.begin(); cout << a + 1 << endl; } }
### Prompt Create a solution in CPP for the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n, i, m, sum = 0; cin >> n; vector<int> v(n); for (i = 0; i < n; i++) { cin >> v[i]; } for (i = 1; i < n; i++) { v[i] = v[i] + v[i - 1]; } cin >> m; long long b[m]; for (i = 0; i < m; i++) cin >> b[i]; for (i = 0; i < m; i++) { int a = lower_bound(v.begin(), v.end(), b[i]) - v.begin(); cout << a + 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int presum[100005]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int a[n + 1]; for (int i = 0; i < n; i++) cin >> a[i]; int su = 0; for (int i = 0; i < n; i++) { presum[i] = su + a[i]; su = presum[i]; } int m; cin >> m; while (m--) { int q; cin >> q; int res = lower_bound(presum, presum + n, q) - presum; cout << res + 1 << "\n"; } return 0; }
### Prompt Develop a solution in CPP to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int presum[100005]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int a[n + 1]; for (int i = 0; i < n; i++) cin >> a[i]; int su = 0; for (int i = 0; i < n; i++) { presum[i] = su + a[i]; su = presum[i]; } int m; cin >> m; while (m--) { int q; cin >> q; int res = lower_bound(presum, presum + n, q) - presum; cout << res + 1 << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const int INF = 1e9; const int maxn = 1e5 + 100; struct node { int x, id; } q[maxn]; int n, m; int a[maxn][2]; bool cmpx(node a, node b) { return a.x < b.x; } bool cmpid(node a, node b) { return a.id < b.id; } int ans[maxn]; int main() { scanf("%d", &n); a[0][1] = 0; int x; for (int i = 1; i <= n; ++i) { scanf("%d", &x); a[i][0] = a[i - 1][1] + 1; a[i][1] = a[i - 1][1] + x; } scanf("%d", &m); for (int i = 1; i <= m; ++i) { scanf("%d", &q[i].x); q[i].id = i; } sort(q + 1, q + m + 1, cmpx); int i = 1; for (int j = 1; j <= n; ++j) { while (i <= m && a[j][0] <= q[i].x && q[i].x <= a[j][1]) { ans[q[i].id] = j; i++; } } for (int i = 1; i <= m; ++i) { printf("%d\n", ans[i]); } return 0; }
### Prompt In cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const int INF = 1e9; const int maxn = 1e5 + 100; struct node { int x, id; } q[maxn]; int n, m; int a[maxn][2]; bool cmpx(node a, node b) { return a.x < b.x; } bool cmpid(node a, node b) { return a.id < b.id; } int ans[maxn]; int main() { scanf("%d", &n); a[0][1] = 0; int x; for (int i = 1; i <= n; ++i) { scanf("%d", &x); a[i][0] = a[i - 1][1] + 1; a[i][1] = a[i - 1][1] + x; } scanf("%d", &m); for (int i = 1; i <= m; ++i) { scanf("%d", &q[i].x); q[i].id = i; } sort(q + 1, q + m + 1, cmpx); int i = 1; for (int j = 1; j <= n; ++j) { while (i <= m && a[j][0] <= q[i].x && q[i].x <= a[j][1]) { ans[q[i].id] = j; i++; } } for (int i = 1; i <= m; ++i) { printf("%d\n", ans[i]); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int special_binary_search(int ara[], int number, int n) { if (number <= ara[0]) return 0; int st = 0, fn = n - 1, i, middle; while (fn - st > 1) { middle = (st + fn) / 2; if (ara[middle] >= number) fn = middle; else st = middle; } return fn; } int main() { int n; cin >> n; int arr1[n]; for (int i = 0; i < n; i++) cin >> arr1[i]; for (int i = 1; i < n; i++) arr1[i] = arr1[i - 1] + arr1[i]; int m; cin >> m; int arr2[m]; for (int i = 0; i < m; i++) { cin >> arr2[i]; cout << special_binary_search(arr1, arr2[i], n) + 1 << endl; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int special_binary_search(int ara[], int number, int n) { if (number <= ara[0]) return 0; int st = 0, fn = n - 1, i, middle; while (fn - st > 1) { middle = (st + fn) / 2; if (ara[middle] >= number) fn = middle; else st = middle; } return fn; } int main() { int n; cin >> n; int arr1[n]; for (int i = 0; i < n; i++) cin >> arr1[i]; for (int i = 1; i < n; i++) arr1[i] = arr1[i - 1] + arr1[i]; int m; cin >> m; int arr2[m]; for (int i = 0; i < m; i++) { cin >> arr2[i]; cout << special_binary_search(arr1, arr2[i], n) + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); cin.sync_with_stdio(0); cout.tie(0); int n, valore, s[100002], m, worm; cin >> n; for (int i = 0; i < n; i++) { cin >> valore; if (i == 0) s[1] = valore; else s[i + 1] = s[i] + valore; } cin >> m; for (int i = 0; i < m; i++) { cin >> valore; cout << lower_bound(s, s + n, valore) - s << endl; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); cin.sync_with_stdio(0); cout.tie(0); int n, valore, s[100002], m, worm; cin >> n; for (int i = 0; i < n; i++) { cin >> valore; if (i == 0) s[1] = valore; else s[i + 1] = s[i] + valore; } cin >> m; for (int i = 0; i < m; i++) { cin >> valore; cout << lower_bound(s, s + n, valore) - s << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; long long int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 1; i < n; i++) arr[i] = arr[i] + arr[i - 1]; int q; cin >> q; while (q--) { long long int k; cin >> k; cout << (lower_bound(arr, arr + n, k) - arr) + 1 << endl; } return 0; }
### Prompt Generate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; long long int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 1; i < n; i++) arr[i] = arr[i] + arr[i - 1]; int q; cin >> q; while (q--) { long long int k; cin >> k; cout << (lower_bound(arr, arr + n, k) - arr) + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; int myf(int s, int e, int o, int sum1[]) { int mid = (s + e) / 2; if (mid == 0) { if (o <= sum1[0]) return 1; else return 2; } else { if ((o <= sum1[mid]) && (o > sum1[mid - 1])) return mid + 1; else if (o > sum1[mid]) return myf(mid, e, o, sum1); else return myf(0, mid, o, sum1); } } int main() { int m; cin >> n; int a[n]; int sum[n]; int initsum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; initsum = initsum + a[i]; sum[i] = initsum; } cin >> m; int q[m]; int l; for (int j = 0; j < m; j++) { cin >> q[j]; if (q[j] == q[j - 1]) l = l; else l = myf(0, n, q[j], sum); cout << l << endl; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; int myf(int s, int e, int o, int sum1[]) { int mid = (s + e) / 2; if (mid == 0) { if (o <= sum1[0]) return 1; else return 2; } else { if ((o <= sum1[mid]) && (o > sum1[mid - 1])) return mid + 1; else if (o > sum1[mid]) return myf(mid, e, o, sum1); else return myf(0, mid, o, sum1); } } int main() { int m; cin >> n; int a[n]; int sum[n]; int initsum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; initsum = initsum + a[i]; sum[i] = initsum; } cin >> m; int q[m]; int l; for (int j = 0; j < m; j++) { cin >> q[j]; if (q[j] == q[j - 1]) l = l; else l = myf(0, n, q[j], sum); cout << l << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[100005]; int main() { int n, m, k; while (scanf("%d", &n) != EOF) { a[0] = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); a[i] += a[i - 1]; } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &k); printf("%d\n", lower_bound(a, a + n, k) - a); } } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[100005]; int main() { int n, m, k; while (scanf("%d", &n) != EOF) { a[0] = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); a[i] += a[i - 1]; } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &k); printf("%d\n", lower_bound(a, a + n, k) - a); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; long poz[1000005]; int main() { long i, j, n, a, b; cin >> n; cin >> a; for (i = 1; i <= a; i++) poz[i] = 1; for (i = 1; i < n; i++) { cin >> b; for (j = a + 1; j <= a + b; j++) poz[j] = i + 1; a += b; } cin >> n; for (i = 0; i < n; i++) { cin >> a; cout << poz[a] << endl; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long poz[1000005]; int main() { long i, j, n, a, b; cin >> n; cin >> a; for (i = 1; i <= a; i++) poz[i] = 1; for (i = 1; i < n; i++) { cin >> b; for (j = a + 1; j <= a + b; j++) poz[j] = i + 1; a += b; } cin >> n; for (i = 0; i < n; i++) { cin >> a; cout << poz[a] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct Point { int X; int Y; }; void Search(int point, Point points[], int first, int last) { while (first <= last) { int mid = first + (last - first) / 2; if (point >= points[mid].X && point <= points[mid].Y) { cout << mid + 1 << endl; break; } else if (point < points[mid].X) last = mid - 1; else if (point > points[mid].Y) first = mid + 1; } } int main() { int prev = 0, piles, numberWorms, juicyNumber; cin >> piles; Point points[piles]; for (int i = 0; i < piles; i++) { cin >> numberWorms; points[i].X = 1 + prev; points[i].Y = prev + numberWorms; prev = points[i].Y; } cin >> juicyNumber; for (int i = 0; i < juicyNumber; i++) { int juicy; cin >> juicy; Search(juicy, points, 0, piles - 1); } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Point { int X; int Y; }; void Search(int point, Point points[], int first, int last) { while (first <= last) { int mid = first + (last - first) / 2; if (point >= points[mid].X && point <= points[mid].Y) { cout << mid + 1 << endl; break; } else if (point < points[mid].X) last = mid - 1; else if (point > points[mid].Y) first = mid + 1; } } int main() { int prev = 0, piles, numberWorms, juicyNumber; cin >> piles; Point points[piles]; for (int i = 0; i < piles; i++) { cin >> numberWorms; points[i].X = 1 + prev; points[i].Y = prev + numberWorms; prev = points[i].Y; } cin >> juicyNumber; for (int i = 0; i < juicyNumber; i++) { int juicy; cin >> juicy; Search(juicy, points, 0, piles - 1); } return 0; } ```
#include <bits/stdc++.h> int binary_search(long long int a[], int n, long long int x) { int left, right, mid; left = 0; right = n - 1; while (left <= right) { mid = (right + left) / 2; if (a[mid] == x) return mid; if (a[mid] > x) right = mid - 1; else left = mid + 1; } return left; } using namespace std; int main() { int n, m, i = 0; cin >> n; long long int a[n], s = 0, x; while (i != n) { cin >> x; s += x; a[i] = s; i++; } cin >> m; while (m--) { cin >> x; i = binary_search(a, n, x) + 1; cout << i << endl; } return 0; }
### Prompt Develop a solution in cpp to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> int binary_search(long long int a[], int n, long long int x) { int left, right, mid; left = 0; right = n - 1; while (left <= right) { mid = (right + left) / 2; if (a[mid] == x) return mid; if (a[mid] > x) right = mid - 1; else left = mid + 1; } return left; } using namespace std; int main() { int n, m, i = 0; cin >> n; long long int a[n], s = 0, x; while (i != n) { cin >> x; s += x; a[i] = s; i++; } cin >> m; while (m--) { cin >> x; i = binary_search(a, n, x) + 1; cout << i << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int power(long long int b, long long int e, long long int m) { if (e == 0) return 1; if (e % 2) return ((b % m) * power(((b % m) * (b % m)) % m, (e - 1) / 2, m)) % m; else return power((b % m) * (b % m), e / 2, m) % m; } long long int ncr(long long int n, long long int r) { long long int f1 = 1; for (long long int i = 1; i <= n; i++) { f1 = (f1 % 1000000007 * (i) % 1000000007) % 1000000007; } long long int f2 = 1; for (long long int i = 1; i <= r; i++) { f2 = (f2 % 1000000007 * (i) % 1000000007) % 1000000007; } long long int f3 = 1; for (long long int i = 1; i <= n - r; i++) { f3 = (f3 % 1000000007 * i % 1000000007) % 1000000007; } long long int k = (f1 % 1000000007 * power(f2, 1000000007 - 2, 1000000007) % 1000000007 * power(f3, 1000000007 - 2, 1000000007) % 1000000007) % 1000000007; return k; } void dpv(vector<long long int> v) { for (long long int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << '\n'; } void dpv(vector<pair<long long int, long long int> > v) { for (long long int i = 0; i < v.size(); i++) { cout << v[i].first << " " << v[i].second << '\n'; } } void dpv(set<long long int> v) { for (auto i : v) { cout << i << " "; } cout << '\n'; } long long int Logn(long long int n, long long int k) { return (n > k - 1) ? 1 + Logn(n / k, k) : 0; } long long int max(long long int a, long long int b) { if (a > b) { return a; } else { return b; } } bool sortbysecdec(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return (a.second < b.second); } long long int min(long long int a, long long int b) { if (a > b) { return b; } else { return a; } } void oblivious() { long long int n; cin >> n; long long int a[n]; for (long long int i = 0; i < n; i++) { cin >> a[i]; } vector<long long int> v; v.push_back(1); long long int c = 1; for (long long int i = 1; c <= n; i++) { if (i % 2 == 1) { v.push_back(v[i - 1] + a[c - 1] - 1); c++; } else { v.push_back(v[i - 1] + 1); } } long long int m; cin >> m; for (long long int i = 0; i < m; i++) { long long int x; cin >> x; auto itr = lower_bound(v.begin(), v.end(), x); if (itr != v.end()) { long long int y = itr - v.begin(); if (y % 2 == 1) { cout << (y + 1) / 2 << '\n'; } else { cout << (y / 2) + 1 << '\n'; } } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) { oblivious(); } return 0; }
### Prompt Create a solution in cpp for the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int power(long long int b, long long int e, long long int m) { if (e == 0) return 1; if (e % 2) return ((b % m) * power(((b % m) * (b % m)) % m, (e - 1) / 2, m)) % m; else return power((b % m) * (b % m), e / 2, m) % m; } long long int ncr(long long int n, long long int r) { long long int f1 = 1; for (long long int i = 1; i <= n; i++) { f1 = (f1 % 1000000007 * (i) % 1000000007) % 1000000007; } long long int f2 = 1; for (long long int i = 1; i <= r; i++) { f2 = (f2 % 1000000007 * (i) % 1000000007) % 1000000007; } long long int f3 = 1; for (long long int i = 1; i <= n - r; i++) { f3 = (f3 % 1000000007 * i % 1000000007) % 1000000007; } long long int k = (f1 % 1000000007 * power(f2, 1000000007 - 2, 1000000007) % 1000000007 * power(f3, 1000000007 - 2, 1000000007) % 1000000007) % 1000000007; return k; } void dpv(vector<long long int> v) { for (long long int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << '\n'; } void dpv(vector<pair<long long int, long long int> > v) { for (long long int i = 0; i < v.size(); i++) { cout << v[i].first << " " << v[i].second << '\n'; } } void dpv(set<long long int> v) { for (auto i : v) { cout << i << " "; } cout << '\n'; } long long int Logn(long long int n, long long int k) { return (n > k - 1) ? 1 + Logn(n / k, k) : 0; } long long int max(long long int a, long long int b) { if (a > b) { return a; } else { return b; } } bool sortbysecdec(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return (a.second < b.second); } long long int min(long long int a, long long int b) { if (a > b) { return b; } else { return a; } } void oblivious() { long long int n; cin >> n; long long int a[n]; for (long long int i = 0; i < n; i++) { cin >> a[i]; } vector<long long int> v; v.push_back(1); long long int c = 1; for (long long int i = 1; c <= n; i++) { if (i % 2 == 1) { v.push_back(v[i - 1] + a[c - 1] - 1); c++; } else { v.push_back(v[i - 1] + 1); } } long long int m; cin >> m; for (long long int i = 0; i < m; i++) { long long int x; cin >> x; auto itr = lower_bound(v.begin(), v.end(), x); if (itr != v.end()) { long long int y = itr - v.begin(); if (y % 2 == 1) { cout << (y + 1) / 2 << '\n'; } else { cout << (y / 2) + 1 << '\n'; } } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) { oblivious(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[100001]; int n, m; void bs(int k) { int p = -1; for (int i = n; i >= 1; i /= 2) { while (p + i < n && a[p + i] <= k) p += i; } if (a[p] == k) cout << p + 1 << endl; else cout << p + 2 << endl; } int main() { int k; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (i > 0) a[i] += a[i - 1]; } cin >> m; for (int i = 0; i < m; i++) { cin >> k; bs(k); } }
### Prompt Please create a solution in cpp to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[100001]; int n, m; void bs(int k) { int p = -1; for (int i = n; i >= 1; i /= 2) { while (p + i < n && a[p + i] <= k) p += i; } if (a[p] == k) cout << p + 1 << endl; else cout << p + 2 << endl; } int main() { int k; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (i > 0) a[i] += a[i - 1]; } cin >> m; for (int i = 0; i < m; i++) { cin >> k; bs(k); } } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int block_size = 320; const long long mod = 1e9 + 7; const long double eps = 1e-9; const int inf = mod; template <typename T> int sign(const T& a) { if (a < 0) return -1; if (a > 0) return 1; return 0; } void read(int& ans) { ans = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while ('0' <= c && c <= '9') ans = ans * 10 + c - '0', c = getchar(); } void read(long long& ans) { ans = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while ('0' <= c && c <= '9') ans = ans * 10 + c - '0', c = getchar(); } int prefix[100010]; int main() { int n; (read(n)); for (long long i = 1; i < n + 1; i++) { (read(prefix[i])); prefix[i] += prefix[i - 1]; } int q; (read(q)); for (long long i = 0; i < q; i++) { int c; (read(c)); cout << lower_bound(prefix, prefix + n + 1, c) - prefix << endl; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int block_size = 320; const long long mod = 1e9 + 7; const long double eps = 1e-9; const int inf = mod; template <typename T> int sign(const T& a) { if (a < 0) return -1; if (a > 0) return 1; return 0; } void read(int& ans) { ans = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while ('0' <= c && c <= '9') ans = ans * 10 + c - '0', c = getchar(); } void read(long long& ans) { ans = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while ('0' <= c && c <= '9') ans = ans * 10 + c - '0', c = getchar(); } int prefix[100010]; int main() { int n; (read(n)); for (long long i = 1; i < n + 1; i++) { (read(prefix[i])); prefix[i] += prefix[i - 1]; } int q; (read(q)); for (long long i = 0; i < q; i++) { int c; (read(c)); cout << lower_bound(prefix, prefix + n + 1, c) - prefix << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, m, a[(int)1e6], s[(int)1e6], x; long long ans() { long long st = 0, en = n - 1; while (st < en) { long long mid = (st + en) >> 1; if (s[mid] >= x) en = mid; else st = mid + 1; } return st; } int main() { scanf("%I64d", &n); for (int i = 0; i < n; i++) scanf("%I64d", a + i); scanf("%I64d", &m); s[0] = a[0]; for (int i = 1; i < n; i++) s[i] = s[i - 1] + a[i]; while (m--) { scanf("%I64d", &x); cout << ans() + 1 << '\n'; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, m, a[(int)1e6], s[(int)1e6], x; long long ans() { long long st = 0, en = n - 1; while (st < en) { long long mid = (st + en) >> 1; if (s[mid] >= x) en = mid; else st = mid + 1; } return st; } int main() { scanf("%I64d", &n); for (int i = 0; i < n; i++) scanf("%I64d", a + i); scanf("%I64d", &m); s[0] = a[0]; for (int i = 1; i < n; i++) s[i] = s[i - 1] + a[i]; while (m--) { scanf("%I64d", &x); cout << ans() + 1 << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int va[100010]; int main() { int n; while (scanf("%d", &n) != EOF) { for (int i = 1; i <= n; i++) { scanf("%d", &va[i]); va[i] += va[i - 1]; } int m, t; scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &t); printf("%d\n", lower_bound(va, va + n + 1, t) - va); } } return 0; }
### Prompt Develop a solution in cpp to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int va[100010]; int main() { int n; while (scanf("%d", &n) != EOF) { for (int i = 1; i <= n; i++) { scanf("%d", &va[i]); va[i] += va[i - 1]; } int m, t; scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &t); printf("%d\n", lower_bound(va, va + n + 1, t) - va); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int piles[100000], n, worms, t; int getPile(int h) { int minn = 0; int maxx = n; while (maxx - minn > 1) { int med = (maxx + minn) / 2; if ((piles[med] >= h && med == 0) || (piles[med] >= h && piles[med - 1] < h)) return med + 1; if (piles[med] > h) maxx = med; else minn = med; } } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> t; piles[i] = piles[i - 1] + t; } cin >> worms; for (int i = 0; i < worms; i++) { cin >> t; cout << getPile(t) << endl; } }
### Prompt Generate a Cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int piles[100000], n, worms, t; int getPile(int h) { int minn = 0; int maxx = n; while (maxx - minn > 1) { int med = (maxx + minn) / 2; if ((piles[med] >= h && med == 0) || (piles[med] >= h && piles[med - 1] < h)) return med + 1; if (piles[med] > h) maxx = med; else minn = med; } } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> t; piles[i] = piles[i - 1] + t; } cin >> worms; for (int i = 0; i < worms; i++) { cin >> t; cout << getPile(t) << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, arr[1000009], j = 1, m; memset(arr, 0, sizeof arr); cin >> n; for (int i = 1; i <= n; i++) { int a; cin >> a; for (int k = 0; k < a; k++) { arr[j++] = i; } } cin >> m; for (int i = 0; i < m; i++) { int a; cin >> a; cout << arr[a] << endl; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, arr[1000009], j = 1, m; memset(arr, 0, sizeof arr); cin >> n; for (int i = 1; i <= n; i++) { int a; cin >> a; for (int k = 0; k < a; k++) { arr[j++] = i; } } cin >> m; for (int i = 0; i < m; i++) { int a; cin >> a; cout << arr[a] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long cs[100001], a, q, i, t, s, e, m, mp, f, n; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> a; if (i > 0) cs[i] = cs[i - 1] + a; else cs[i] = a; } cin >> q; while (q--) { s = 0; e = n - 1; mp = 0; m = 0; cin >> t; f = 0; while (s <= e) { mp = m; m = (s + e) / 2; if (cs[m] == t) { cout << m + 1 << endl; f++; break; } if (cs[m] < t) s = m + 1; else e = m - 1; } if (f == 0) { if (t <= cs[m]) cout << m + 1 << endl; else cout << m + 2 << endl; } } return 0; }
### Prompt In cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long cs[100001], a, q, i, t, s, e, m, mp, f, n; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> a; if (i > 0) cs[i] = cs[i - 1] + a; else cs[i] = a; } cin >> q; while (q--) { s = 0; e = n - 1; mp = 0; m = 0; cin >> t; f = 0; while (s <= e) { mp = m; m = (s + e) / 2; if (cs[m] == t) { cout << m + 1 << endl; f++; break; } if (cs[m] < t) s = m + 1; else e = m - 1; } if (f == 0) { if (t <= cs[m]) cout << m + 1 << endl; else cout << m + 2 << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, m, n, val, t = 0, test, arr[100000] = {0}, sum = 0; cin >> n; for (i = 1; i <= n; i++) { scanf("%d", &arr[i]); arr[i] += arr[i - 1]; } cin >> m; for (i = 0; i < m; i++) { scanf("%d", &val); int maxi = n, mini = 1, mid; while (mini <= maxi) { mid = (mini + maxi) / 2; if (arr[mid] >= val && arr[mid - 1] < val) break; else if (arr[mid] < val) mini = mid + 1; else maxi = mid - 1; } printf("%d\n", mid); } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int i, j, k, m, n, val, t = 0, test, arr[100000] = {0}, sum = 0; cin >> n; for (i = 1; i <= n; i++) { scanf("%d", &arr[i]); arr[i] += arr[i - 1]; } cin >> m; for (i = 0; i < m; i++) { scanf("%d", &val); int maxi = n, mini = 1, mid; while (mini <= maxi) { mid = (mini + maxi) / 2; if (arr[mid] >= val && arr[mid - 1] < val) break; else if (arr[mid] < val) mini = mid + 1; else maxi = mid - 1; } printf("%d\n", mid); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { pair<long long int, long long int> vr[100001]; int n, sum = 1; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; int temp1, temp2; temp1 = sum; temp2 = (a[i] + sum) - 1; sum += a[i]; vr[i].first = temp1; vr[i].second = temp2; } long long int m; cin >> m; long long int q[m]; for (int i = 0; i < m; i++) { cin >> q[i]; int l = 0, r = n - 1, m; while (l <= r) { m = (l + r) / 2; if (q[i] > vr[m].first && q[i] > vr[m].second) { l = m + 1; } if (q[i] < vr[m].first && q[i] < vr[m].second) { r = m - 1; } else if (q[i] >= vr[m].first && q[i] <= vr[m].second) { cout << m + 1 << endl; break; } } } return 0; }
### Prompt In Cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { pair<long long int, long long int> vr[100001]; int n, sum = 1; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; int temp1, temp2; temp1 = sum; temp2 = (a[i] + sum) - 1; sum += a[i]; vr[i].first = temp1; vr[i].second = temp2; } long long int m; cin >> m; long long int q[m]; for (int i = 0; i < m; i++) { cin >> q[i]; int l = 0, r = n - 1, m; while (l <= r) { m = (l + r) / 2; if (q[i] > vr[m].first && q[i] > vr[m].second) { l = m + 1; } if (q[i] < vr[m].first && q[i] < vr[m].second) { r = m - 1; } else if (q[i] >= vr[m].first && q[i] <= vr[m].second) { cout << m + 1 << endl; break; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m, a[1111111]; int main() { scanf("%d", &n); a[0] = 0; for (int i = 1; i <= n; ++i) { int x; scanf("%d", &x); a[i] = a[i - 1] + x; } scanf("%d", &m); while (m--) { int x; int res = 0; scanf("%d", &x); int lef = 1, rig = n; while (lef <= rig) { int mid = (lef + rig) / 2; if (a[mid] < x) { res = mid; lef = mid + 1; } else rig = mid - 1; } printf("%d ", res + 1); } return 0; }
### Prompt Please create a solution in CPP to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, a[1111111]; int main() { scanf("%d", &n); a[0] = 0; for (int i = 1; i <= n; ++i) { int x; scanf("%d", &x); a[i] = a[i - 1] + x; } scanf("%d", &m); while (m--) { int x; int res = 0; scanf("%d", &x); int lef = 1, rig = n; while (lef <= rig) { int mid = (lef + rig) / 2; if (a[mid] < x) { res = mid; lef = mid + 1; } else rig = mid - 1; } printf("%d ", res + 1); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int n, m, i, j, k; long long int sum = 0; long long int b_search(long long int b[], long long int lo, long long int hi, long long int x, long long int pos) { if (lo > hi) { return pos; } long long int mid = lo + (hi - lo) / 2; if (b[mid] >= x) { pos = mid; return b_search(b, lo, mid - 1, x, pos); } else { return b_search(b, mid + 1, hi, x, pos); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; long long int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } long long int sum = 0; long long int pre[n]; for (i = 0; i < n; i++) { sum = sum + a[i]; pre[i] = sum; } cin >> m; while (m--) { long long int x; cin >> x; cout << b_search(pre, 0, n - 1, x, (n - 1) / 2) + 1 << endl; } }
### Prompt Generate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int n, m, i, j, k; long long int sum = 0; long long int b_search(long long int b[], long long int lo, long long int hi, long long int x, long long int pos) { if (lo > hi) { return pos; } long long int mid = lo + (hi - lo) / 2; if (b[mid] >= x) { pos = mid; return b_search(b, lo, mid - 1, x, pos); } else { return b_search(b, mid + 1, hi, x, pos); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; long long int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } long long int sum = 0; long long int pre[n]; for (i = 0; i < n; i++) { sum = sum + a[i]; pre[i] = sum; } cin >> m; while (m--) { long long int x; cin >> x; cout << b_search(pre, 0, n - 1, x, (n - 1) / 2) + 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; long long binary(long long a[], long long l, long long r, long long x) { long long mid, ans = -1; while (r >= l) { mid = (l + r) / 2; if (x <= a[mid]) { ans = mid; r = mid - 1; } else l = mid + 1; } return ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; cin >> m; long long b[m]; for (long long i = 0; i < m; i++) cin >> b[i]; for (long long i = 1; i < n; i++) a[i] += a[i - 1]; for (long long i = 0; i < m; i++) { long long x = binary(a, 0, n - 1, b[i]); cout << x + 1 << '\n'; } }
### Prompt Please create a solution in Cpp to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long binary(long long a[], long long l, long long r, long long x) { long long mid, ans = -1; while (r >= l) { mid = (l + r) / 2; if (x <= a[mid]) { ans = mid; r = mid - 1; } else l = mid + 1; } return ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; cin >> m; long long b[m]; for (long long i = 0; i < m; i++) cin >> b[i]; for (long long i = 1; i < n; i++) a[i] += a[i - 1]; for (long long i = 0; i < m; i++) { long long x = binary(a, 0, n - 1, b[i]); cout << x + 1 << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int binary(int st, int en, int s[], int k) { while (en >= st) { int mid = st + (en - st) / 2; if (k < s[0]) { return st; } if (k > s[en]) { return en; } if (k == s[mid]) { return mid; } if (k >= s[mid] && k <= s[mid + 1]) { return mid + 1; } if (s[mid] > k) { en = mid; } else { st = mid; } } } int main() { int n, x; int sum = 0; cin >> n; int s[n]; for (int j = 0; j < n; j++) { cin >> x; sum = sum + x; s[j] = sum; } int size = sizeof(s) / sizeof(s[0]); int m; cin >> m; while (m--) { int k; cin >> k; int ans = binary(0, size - 1, s, k); cout << ans + 1 << endl; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int binary(int st, int en, int s[], int k) { while (en >= st) { int mid = st + (en - st) / 2; if (k < s[0]) { return st; } if (k > s[en]) { return en; } if (k == s[mid]) { return mid; } if (k >= s[mid] && k <= s[mid + 1]) { return mid + 1; } if (s[mid] > k) { en = mid; } else { st = mid; } } } int main() { int n, x; int sum = 0; cin >> n; int s[n]; for (int j = 0; j < n; j++) { cin >> x; sum = sum + x; s[j] = sum; } int size = sizeof(s) / sizeof(s[0]); int m; cin >> m; while (m--) { int k; cin >> k; int ans = binary(0, size - 1, s, k); cout << ans + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAX_N = 101010; int a[MAX_N], sum[MAX_N]; int main() { int n, m, q; while (scanf("%d", &n) == 1) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; scanf("%d", &m); while (m--) { scanf("%d", &q); int l = 0, r = n + 1, mid; while (r - l > 1) { mid = l + r >> 1; if (sum[mid] < q) l = mid; else r = mid; } printf("%d\n", r); } } return 0; }
### Prompt Please create a solution in CPP to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX_N = 101010; int a[MAX_N], sum[MAX_N]; int main() { int n, m, q; while (scanf("%d", &n) == 1) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; scanf("%d", &m); while (m--) { scanf("%d", &q); int l = 0, r = n + 1, mid; while (r - l > 1) { mid = l + r >> 1; if (sum[mid] < q) l = mid; else r = mid; } printf("%d\n", r); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, x = 1; cin >> n; long long int a[n]; long long int p[1000001]; for (long long int i = 0; i < n; i++) { cin >> a[i]; while (a[i]--) { p[x] = i + 1; x++; } } long long int m; cin >> m; long long int b[m]; for (long long int i = 0; i < m; i++) { cin >> b[i]; } for (long long int i = 0; i < m; i++) { cout << p[b[i]] << "\n"; } }
### Prompt Your challenge is to write a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, x = 1; cin >> n; long long int a[n]; long long int p[1000001]; for (long long int i = 0; i < n; i++) { cin >> a[i]; while (a[i]--) { p[x] = i + 1; x++; } } long long int m; cin >> m; long long int b[m]; for (long long int i = 0; i < m; i++) { cin >> b[i]; } for (long long int i = 0; i < m; i++) { cout << p[b[i]] << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int n, m, a[100001], q, sum; int b[1000001]; int main() { int n, m, q, sum = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; b[a[i] + sum] = i; b[1 + sum] = i; sum += a[i]; } cin >> m; for (int j = 1; j <= m; j++) { cin >> q; if (b[q]) { cout << b[q] << endl; continue; } for (int k = 1; k <= 1000; k++) { if (b[q + k]) { cout << b[q + k] << endl; break; } if (b[q - k]) { cout << b[q - k] << endl; break; } } } return 0; }
### Prompt Please create a solution in cpp to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, a[100001], q, sum; int b[1000001]; int main() { int n, m, q, sum = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; b[a[i] + sum] = i; b[1 + sum] = i; sum += a[i]; } cin >> m; for (int j = 1; j <= m; j++) { cin >> q; if (b[q]) { cout << b[q] << endl; continue; } for (int k = 1; k <= 1000; k++) { if (b[q + k]) { cout << b[q + k] << endl; break; } if (b[q - k]) { cout << b[q - k] << endl; break; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int binary_search(int arr[], int n, int a) { int low, high, mid; low = 0; high = n - 1; while (high > low) { if ((low + 1) == high) { if (arr[low] < a) return high; return low; } mid = (low + high) / 2; if (arr[mid] == a) return mid; if (arr[mid] < a) low = mid; else high = mid; } } int main() { int n, ans, m, a; cin >> n; int arr[n], brr[n], i; for (i = 0; i < n; i++) { cin >> arr[i]; brr[i] = arr[i]; if (i) brr[i] += brr[i - 1]; } cin >> m; for (i = 0; i < m; i++) { cin >> a; ans = binary_search(brr, n, a); cout << ans + 1 << endl; } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int binary_search(int arr[], int n, int a) { int low, high, mid; low = 0; high = n - 1; while (high > low) { if ((low + 1) == high) { if (arr[low] < a) return high; return low; } mid = (low + high) / 2; if (arr[mid] == a) return mid; if (arr[mid] < a) low = mid; else high = mid; } } int main() { int n, ans, m, a; cin >> n; int arr[n], brr[n], i; for (i = 0; i < n; i++) { cin >> arr[i]; brr[i] = arr[i]; if (i) brr[i] += brr[i - 1]; } cin >> m; for (i = 0; i < m; i++) { cin >> a; ans = binary_search(brr, n, a); cout << ans + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> nums(n + 1, 0); for (int i = 1; i <= n; i++) { cin >> nums[i]; nums[i] += nums[i - 1]; } int m, x; cin >> m; for (int i = 0; i < m; i++) { cin >> x; int index = (int)(lower_bound(nums.begin(), nums.end(), x) - nums.begin()); cout << index << endl; } return; } int main() { int test = 1; while (test--) { solve(); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> nums(n + 1, 0); for (int i = 1; i <= n; i++) { cin >> nums[i]; nums[i] += nums[i - 1]; } int m, x; cin >> m; for (int i = 0; i < m; i++) { cin >> x; int index = (int)(lower_bound(nums.begin(), nums.end(), x) - nums.begin()); cout << index << endl; } return; } int main() { int test = 1; while (test--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long binSrc(long long arr[], long long n, long long item) { long long mid, lo, hi; lo = 0; hi = n - 1; while (lo <= hi) { mid = (lo + hi) / 2; if (arr[mid] == item) return mid + 1; if (item < arr[mid]) { if (!mid) return 1; if (arr[mid - 1] < item) return mid + 1; } if (arr[mid] < item) { lo = mid + 1; } else hi = mid - 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; long long arr[n]; for (long long i = 0; i < n; i++) { cin >> arr[i]; } long long cumSum[n], sz = n; cumSum[0] = arr[0]; for (long long i = 1; i < n; i++) { cumSum[i] = cumSum[i - 1] + arr[i]; } long long m; cin >> m; while (m--) { long long num; cin >> num; cout << binSrc(cumSum, n, num) << "\n"; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long binSrc(long long arr[], long long n, long long item) { long long mid, lo, hi; lo = 0; hi = n - 1; while (lo <= hi) { mid = (lo + hi) / 2; if (arr[mid] == item) return mid + 1; if (item < arr[mid]) { if (!mid) return 1; if (arr[mid - 1] < item) return mid + 1; } if (arr[mid] < item) { lo = mid + 1; } else hi = mid - 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; long long arr[n]; for (long long i = 0; i < n; i++) { cin >> arr[i]; } long long cumSum[n], sz = n; cumSum[0] = arr[0]; for (long long i = 1; i < n; i++) { cumSum[i] = cumSum[i - 1] + arr[i]; } long long m; cin >> m; while (m--) { long long num; cin >> num; cout << binSrc(cumSum, n, num) << "\n"; } return 0; } ```
#include <bits/stdc++.h> int str[100009]; int main(void) { int n, m; scanf("%d", &n); str[0] = 0; for (int i = 1; i <= n; i++) { int c; scanf("%d", &c); str[i] = str[i - 1] + c; } scanf("%d", &m); while (m--) { int c; scanf("%d", &c); if (str[n - 1] < c) { printf("%d\n", n); } else if (str[1] >= c) { printf("1\n"); } else { int up = n, down = 1, mid; while (1) { mid = (up + down) / 2; if (str[mid] >= c && str[mid - 1] < c) { printf("%d\n", mid); break; } if (str[mid] >= c) up = mid; else down = mid + 1; } } } return 0; }
### Prompt In cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> int str[100009]; int main(void) { int n, m; scanf("%d", &n); str[0] = 0; for (int i = 1; i <= n; i++) { int c; scanf("%d", &c); str[i] = str[i - 1] + c; } scanf("%d", &m); while (m--) { int c; scanf("%d", &c); if (str[n - 1] < c) { printf("%d\n", n); } else if (str[1] >= c) { printf("1\n"); } else { int up = n, down = 1, mid; while (1) { mid = (up + down) / 2; if (str[mid] >= c && str[mid - 1] < c) { printf("%d\n", mid); break; } if (str[mid] >= c) up = mid; else down = mid + 1; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, m, x = 0; cin >> n; int a[n], l[n]; for (int i = 0; i < n; i++) { cin >> a[i]; x += a[i]; l[i] = x; } cin >> m; int q[m]; for (int i = 0; i < m; i++) { cin >> q[i]; cout << lower_bound(l, l + n, q[i]) - l + 1 << endl; } }
### Prompt Please create a solution in Cpp to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, x = 0; cin >> n; int a[n], l[n]; for (int i = 0; i < n; i++) { cin >> a[i]; x += a[i]; l[i] = x; } cin >> m; int q[m]; for (int i = 0; i < m; i++) { cin >> q[i]; cout << lower_bound(l, l + n, q[i]) - l + 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; template <class T> void readAll(T& vec) { for (auto& i : vec) { cin >> i; } } int binarySearch(vector<int> arr, int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] >= x && arr[mid - 1] <= x) return mid + 1; if (arr[mid] >= x) return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, mid + 1, r, x); } return 1; } int answer[1000007]; void solve() { int n, m; cin >> n; vector<int> vec(n); readAll(vec); cin >> m; partial_sum(vec.begin(), vec.end(), vec.begin()); int j = 0; for (int i = 1; i <= vec.back(); i++) { if (i <= vec[j]) { answer[i] = j; } else { ++j; answer[i] = j; } } for (int i = 0; i < m; i++) { int x; cin >> x; cout << answer[x] + 1 << "\n"; } } int main() { int t = 1; while (t--) { solve(); } return 0; }
### Prompt Generate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> void readAll(T& vec) { for (auto& i : vec) { cin >> i; } } int binarySearch(vector<int> arr, int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] >= x && arr[mid - 1] <= x) return mid + 1; if (arr[mid] >= x) return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, mid + 1, r, x); } return 1; } int answer[1000007]; void solve() { int n, m; cin >> n; vector<int> vec(n); readAll(vec); cin >> m; partial_sum(vec.begin(), vec.end(), vec.begin()); int j = 0; for (int i = 1; i <= vec.back(); i++) { if (i <= vec[j]) { answer[i] = j; } else { ++j; answer[i] = j; } } for (int i = 0; i < m; i++) { int x; cin >> x; cout << answer[x] + 1 << "\n"; } } int main() { int t = 1; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> pilesQ, pilesAc; for (int i = 0; i < N; i++) { int temp; cin >> temp; pilesQ.push_back(temp); } int sum = 0; for (int i = 0; i < pilesQ.size(); i++) { sum += pilesQ[i]; pilesAc.push_back(sum); } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int temp; cin >> temp; vector<int>::iterator it; it = lower_bound(pilesAc.begin(), pilesAc.end(), temp); int pos = it - pilesAc.begin(); if (it == pilesAc.end()) cout << pos << endl; else if (*it == temp) cout << pos + 1 << endl; else if (*it > temp) cout << pos + 1 << endl; } return 0; }
### Prompt Create a solution in cpp for the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> pilesQ, pilesAc; for (int i = 0; i < N; i++) { int temp; cin >> temp; pilesQ.push_back(temp); } int sum = 0; for (int i = 0; i < pilesQ.size(); i++) { sum += pilesQ[i]; pilesAc.push_back(sum); } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int temp; cin >> temp; vector<int>::iterator it; it = lower_bound(pilesAc.begin(), pilesAc.end(), temp); int pos = it - pilesAc.begin(); if (it == pilesAc.end()) cout << pos << endl; else if (*it == temp) cout << pos + 1 << endl; else if (*it > temp) cout << pos + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n; int* a = new int[n + 1]; int* sum = new int[n + 1]; sum[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } cin >> m; int* q = new int[m]; for (int j = 0; j < m; j++) { cin >> q[j]; } for (int k = 0; k < m; k++) { int x = lower_bound(sum, sum + n, q[k]) - sum; cout << x << endl; } }
### Prompt In Cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n; int* a = new int[n + 1]; int* sum = new int[n + 1]; sum[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } cin >> m; int* q = new int[m]; for (int j = 0; j < m; j++) { cin >> q[j]; } for (int k = 0; k < m; k++) { int x = lower_bound(sum, sum + n, q[k]) - sum; cout << x << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, m, x, s = 0; vector<int> v; cin >> n; while (n--) { cin >> x; s += x; v.push_back(s); } cin >> m; while (m--) { cin >> x; cout << lower_bound(v.begin(), v.end(), x) - v.begin() + 1 << endl; } return 0; }
### Prompt Generate a Cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m, x, s = 0; vector<int> v; cin >> n; while (n--) { cin >> x; s += x; v.push_back(s); } cin >> m; while (m--) { cin >> x; cout << lower_bound(v.begin(), v.end(), x) - v.begin() + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; int a[100010], sum[100010]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sum[1] = 1; for (int i = 2; i <= n; i++) sum[i] = sum[i - 1] + a[i - 1]; int q; scanf("%d", &q); for (; q--;) { int x; scanf("%d", &x); printf("%d\n", upper_bound(sum + 1, sum + n + 1, x) - sum - 1); } }
### Prompt Generate a Cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; int a[100010], sum[100010]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sum[1] = 1; for (int i = 2; i <= n; i++) sum[i] = sum[i - 1] + a[i - 1]; int q; scanf("%d", &q); for (; q--;) { int x; scanf("%d", &x); printf("%d\n", upper_bound(sum + 1, sum + n + 1, x) - sum - 1); } } ```
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; long long a[N]; void solve() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) { a[i] += a[i - 1]; } int q; cin >> q; while (q--) { int tot; cin >> tot; int ind = lower_bound(a, a + n, tot) - a; cout << ind + 1 << "\n"; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) { solve(); } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; long long a[N]; void solve() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) { a[i] += a[i - 1]; } int q; cin >> q; while (q--) { int tot; cin >> tot; int ind = lower_bound(a, a + n, tot) - a; cout << ind + 1 << "\n"; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> int num[1000010]; int main() { int T, n, m, p; int i, j; int cnt = 1; while (~scanf("%d", &T)) { for (i = 1; i <= T; i++) { scanf("%d", &n); for (j = 1; j <= n; j++) num[cnt++] = i; } scanf("%d", &m); for (i = 1; i <= m; i++) { scanf("%d", &p); printf("%d\n", num[p]); } } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> int num[1000010]; int main() { int T, n, m, p; int i, j; int cnt = 1; while (~scanf("%d", &T)) { for (i = 1; i <= T; i++) { scanf("%d", &n); for (j = 1; j <= n; j++) num[cnt++] = i; } scanf("%d", &m); for (i = 1; i <= m; i++) { scanf("%d", &p); printf("%d\n", num[p]); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, q, l; int A[N], sum[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", A + i); sum[i] = sum[i - 1] + A[i]; } scanf("%d", &q); while (q--) { scanf("%d", &l); printf("%d\n", lower_bound(sum, sum + n + 1, l) - sum); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, q, l; int A[N], sum[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", A + i); sum[i] = sum[i - 1] + A[i]; } scanf("%d", &q); while (q--) { scanf("%d", &l); printf("%d\n", lower_bound(sum, sum + n + 1, l) - sum); } return 0; } ```
#include <bits/stdc++.h> int binary(int low, int high, int c[], int b) { int pos; if (low > high) pos = 0; else { int mid = (low + high) / 2; if (c[mid] == b) { pos = mid + 1; } else if (b < c[0]) { pos = 1; } else if (b > c[mid] && b < c[mid + 1]) { pos = mid + 2; } else if (b < c[mid]) { pos = binary(low, mid - 1, c, b); } else { pos = binary(mid + 1, high, c, b); } } return pos; } int main() { int n, a[100000], m, b, c[100000], d, i, j, sum = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); sum = sum + a[i]; c[i] = sum; } scanf("%d", &m); for (j = 0; j < m; j++) { scanf("%d", &b); d = binary(0, n - 1, c, b); printf("%d\n", d); } }
### Prompt Construct a CPP code solution to the problem outlined: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> int binary(int low, int high, int c[], int b) { int pos; if (low > high) pos = 0; else { int mid = (low + high) / 2; if (c[mid] == b) { pos = mid + 1; } else if (b < c[0]) { pos = 1; } else if (b > c[mid] && b < c[mid + 1]) { pos = mid + 2; } else if (b < c[mid]) { pos = binary(low, mid - 1, c, b); } else { pos = binary(mid + 1, high, c, b); } } return pos; } int main() { int n, a[100000], m, b, c[100000], d, i, j, sum = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); sum = sum + a[i]; c[i] = sum; } scanf("%d", &m); for (j = 0; j < m; j++) { scanf("%d", &b); d = binary(0, n - 1, c, b); printf("%d\n", d); } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; vector<int> v; for (int i = 0; i < n; i++) { cin >> a[i]; if (i == 0) v.push_back(a[i]); else { v.push_back(v[v.size() - 1] + a[i]); } } int m, temp; cin >> m; for (int i = 0; i < m; i++) { cin >> temp; vector<int>::iterator low = lower_bound(v.begin(), v.end(), temp); cout << (low - v.begin()) + 1 << endl; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; vector<int> v; for (int i = 0; i < n; i++) { cin >> a[i]; if (i == 0) v.push_back(a[i]); else { v.push_back(v[v.size() - 1] + a[i]); } } int m, temp; cin >> m; for (int i = 0; i < m; i++) { cin >> temp; vector<int>::iterator low = lower_bound(v.begin(), v.end(), temp); cout << (low - v.begin()) + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline int cmp(double x, double y = 0, double tol = 1e-10) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; } int main() { int n; scanf("%d", &n); int v[n]; for (int i = 0; i < n; i++) scanf("%d", &v[i]); vector<pair<int, int> > seg; seg.push_back({1, v[0]}); for (int i = 1; i < n; i++) seg.push_back({seg[i - 1].second + 1, seg[i - 1].second + v[i]}); int q, x; scanf("%d", &q); while (q--) { scanf("%d", &x); int l = 0, r = n - 1; while (l <= r) { int mid = (l + r) / 2; if (seg[mid].first <= x && x <= seg[mid].second) { printf("%d\n", mid + 1); break; } if (seg[mid].first > x) r = mid - 1; else l = mid + 1; } } return 0; }
### Prompt Please create a solution in CPP to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int cmp(double x, double y = 0, double tol = 1e-10) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; } int main() { int n; scanf("%d", &n); int v[n]; for (int i = 0; i < n; i++) scanf("%d", &v[i]); vector<pair<int, int> > seg; seg.push_back({1, v[0]}); for (int i = 1; i < n; i++) seg.push_back({seg[i - 1].second + 1, seg[i - 1].second + v[i]}); int q, x; scanf("%d", &q); while (q--) { scanf("%d", &x); int l = 0, r = n - 1; while (l <= r) { int mid = (l + r) / 2; if (seg[mid].first <= x && x <= seg[mid].second) { printf("%d\n", mid + 1); break; } if (seg[mid].first > x) r = mid - 1; else l = mid + 1; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[100005]; pair<int, int> p[200050]; int find(int t, int n) { int l, r, m; l = 0, r = n; while (1) { m = (l + r) / 2; if (p[m].first <= t && p[m].second >= t) { return m + 1; } if (p[m].first > t) r = m; else l = m; } } int main() { int n, m; cin >> n; int l = 1; for (int i = 0; i < n; i++) { cin >> a[i]; p[i].first = l; p[i].second = l + a[i] - 1; l = l + a[i]; } cin >> m; int q; while (m--) { cin >> q; cout << find(q, n) << endl; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[100005]; pair<int, int> p[200050]; int find(int t, int n) { int l, r, m; l = 0, r = n; while (1) { m = (l + r) / 2; if (p[m].first <= t && p[m].second >= t) { return m + 1; } if (p[m].first > t) r = m; else l = m; } } int main() { int n, m; cin >> n; int l = 1; for (int i = 0; i < n; i++) { cin >> a[i]; p[i].first = l; p[i].second = l + a[i] - 1; l = l + a[i]; } cin >> m; int q; while (m--) { cin >> q; cout << find(q, n) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int CUMULATIVE[300009]; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; bool isValid(int x, int y, int n, int m) { return x >= 0 && y >= 0 && x < n && y < m; } void debugMatrix(int X[][300009], int n, int m) { int i, j; for ((i) = (0); (i) <= (n - 1); (i) += 1) { for ((j) = (0); (j) <= (m - 1); (j) += 1) printf("%3d ", X[i][j]); cout << endl; } } void debugArray(int X[], int n) { int i; for ((i) = (1); (i) <= (n); (i) += 1) cout << X[i] << " "; cout << endl; } void debugVar(string preFix, int x) { cout << preFix << " " << x << endl; } void INIT(int n) { int i, j; for ((i) = (1); (i) <= (n); (i) += 1) { } } int SOLVE(int n) { int i, ans = 0, j; return ans; } int main() { int max_cases, n, i, m, k, j, x, q, pileNum; while (cin >> n) { memset((CUMULATIVE), 0, sizeof(CUMULATIVE)); for ((i) = (1); (i) <= (n); (i) += 1) { cin >> x; CUMULATIVE[i] = (i == 1) ? x : CUMULATIVE[i - 1] + x; } cin >> m; for ((i) = (1); (i) <= (m); (i) += 1) { cin >> q; pileNum = lower_bound(CUMULATIVE + 1, CUMULATIVE + n + 1, q) - (CUMULATIVE); cout << pileNum << endl; } } return 0; }
### Prompt Please create a solution in CPP to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int CUMULATIVE[300009]; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; bool isValid(int x, int y, int n, int m) { return x >= 0 && y >= 0 && x < n && y < m; } void debugMatrix(int X[][300009], int n, int m) { int i, j; for ((i) = (0); (i) <= (n - 1); (i) += 1) { for ((j) = (0); (j) <= (m - 1); (j) += 1) printf("%3d ", X[i][j]); cout << endl; } } void debugArray(int X[], int n) { int i; for ((i) = (1); (i) <= (n); (i) += 1) cout << X[i] << " "; cout << endl; } void debugVar(string preFix, int x) { cout << preFix << " " << x << endl; } void INIT(int n) { int i, j; for ((i) = (1); (i) <= (n); (i) += 1) { } } int SOLVE(int n) { int i, ans = 0, j; return ans; } int main() { int max_cases, n, i, m, k, j, x, q, pileNum; while (cin >> n) { memset((CUMULATIVE), 0, sizeof(CUMULATIVE)); for ((i) = (1); (i) <= (n); (i) += 1) { cin >> x; CUMULATIVE[i] = (i == 1) ? x : CUMULATIVE[i - 1] + x; } cin >> m; for ((i) = (1); (i) <= (m); (i) += 1) { cin >> q; pileNum = lower_bound(CUMULATIVE + 1, CUMULATIVE + n + 1, q) - (CUMULATIVE); cout << pileNum << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 1; i < n; i++) a[i] += a[i - 1]; int q; cin >> q; for (int i = 0; i < q; i++) { int x; cin >> x; int val = lower_bound(a, a + n, x) - a; cout << val + 1 << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) solve(); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 1; i < n; i++) a[i] += a[i - 1]; int q; cin >> q; for (int i = 0; i < q; i++) { int x; cin >> x; int val = lower_bound(a, a + n, x) - a; cout << val + 1 << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; struct node { int id, x; }; bool operator<(node a, node b) { if (a.x < b.x) return 1; else return 0; } int main() { int n, m, a[100009]; node b[100009]; map<int, int> ma; cin >> n; a[0] = 1; n += 1; for (int i = 1; i < n; i++) cin >> a[i]; a[n] = 1000009; int y = 0; for (int i = 2; i < n; i++) { a[i] += a[i - 1]; } cin >> m; int x; for (int i = 0; i < m; i++) { cin >> x; b[i].x = x; b[i].id = i; } sort(b, b + m); int j = 1; for (int i = 0; i < m; i++) { for (; j <= n;) { if (b[i].x <= a[j]) { ma[b[i].id] = j; break; } else j++; } } for (int i = 0; i < m; i++) { cout << ma[i] << endl; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct node { int id, x; }; bool operator<(node a, node b) { if (a.x < b.x) return 1; else return 0; } int main() { int n, m, a[100009]; node b[100009]; map<int, int> ma; cin >> n; a[0] = 1; n += 1; for (int i = 1; i < n; i++) cin >> a[i]; a[n] = 1000009; int y = 0; for (int i = 2; i < n; i++) { a[i] += a[i - 1]; } cin >> m; int x; for (int i = 0; i < m; i++) { cin >> x; b[i].x = x; b[i].id = i; } sort(b, b + m); int j = 1; for (int i = 0; i < m; i++) { for (; j <= n;) { if (b[i].x <= a[j]) { ma[b[i].id] = j; break; } else j++; } } for (int i = 0; i < m; i++) { cout << ma[i] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void pause() {} int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); unsigned long int n, a[100005], m, q[100005]; scanf("%d", &n); unsigned int i; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } scanf("%d", &m); for (i = 0; i < m; i++) { scanf("%d", &q[i]); } unsigned long int s[100005], e[100005]; s[0] = 1, e[0] = a[0]; for (i = 1; i < n; i++) { s[i] = s[i - 1] + a[i - 1]; e[i] = e[i - 1] + a[i]; } for (i = 0; i < m; i++) { unsigned int l = 0, h = n - 1, pos; while (l <= h) { unsigned int mid = (l + h) / 2; if (s[mid] == q[i]) { pos = mid; break; } else if (s[mid] < q[i]) { if (e[mid] >= q[i]) { pos = mid; break; } else l = mid + 1; } else h = mid - 1; } printf("%d\n", pos + 1); } pause(); return 0; }
### Prompt Please formulate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void pause() {} int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); unsigned long int n, a[100005], m, q[100005]; scanf("%d", &n); unsigned int i; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } scanf("%d", &m); for (i = 0; i < m; i++) { scanf("%d", &q[i]); } unsigned long int s[100005], e[100005]; s[0] = 1, e[0] = a[0]; for (i = 1; i < n; i++) { s[i] = s[i - 1] + a[i - 1]; e[i] = e[i - 1] + a[i]; } for (i = 0; i < m; i++) { unsigned int l = 0, h = n - 1, pos; while (l <= h) { unsigned int mid = (l + h) / 2; if (s[mid] == q[i]) { pos = mid; break; } else if (s[mid] < q[i]) { if (e[mid] >= q[i]) { pos = mid; break; } else l = mid + 1; } else h = mid - 1; } printf("%d\n", pos + 1); } pause(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, a, p = 0; scanf("%d", &n); vector<int> v; for (int i = 0; i < n; i++) { scanf("%d", &a); v.push_back(p + a); p += a; } vector<int>::iterator it; int q, x; scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d", &x); it = lower_bound(v.begin(), v.end(), x); cout << (it - v.begin()) + 1 << endl; } return 0; }
### Prompt Develop a solution in cpp to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, a, p = 0; scanf("%d", &n); vector<int> v; for (int i = 0; i < n; i++) { scanf("%d", &a); v.push_back(p + a); p += a; } vector<int>::iterator it; int q, x; scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d", &x); it = lower_bound(v.begin(), v.end(), x); cout << (it - v.begin()) + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int arr2[1000005] = {0}; int main() { int arr1[100005]; int n; cin >> n; for (int i = 0; i < n; i++) cin >> arr1[i]; int x = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < arr1[i]; j++) { arr2[x] = i + 1; x++; } } int m, y; cin >> m; for (int i = 0; i < m; i++) { cin >> y; cout << arr2[y] << endl; } return 0; }
### Prompt Generate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int arr2[1000005] = {0}; int main() { int arr1[100005]; int n; cin >> n; for (int i = 0; i < n; i++) cin >> arr1[i]; int x = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < arr1[i]; j++) { arr2[x] = i + 1; x++; } } int m, y; cin >> m; for (int i = 0; i < m; i++) { cin >> y; cout << arr2[y] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int A[1001000]; int main() { int n; cin >> n; int sum = 0; int tmp; for (int i = 0; i < n; i++) { cin >> tmp; for (int j = sum + 1; j <= sum + tmp; j++) A[j] = i + 1; sum += tmp; } int m, q; cin >> m; for (int i = 0; i < m; i++) { cin >> q; cout << A[q] << endl; } }
### Prompt Construct a Cpp code solution to the problem outlined: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int A[1001000]; int main() { int n; cin >> n; int sum = 0; int tmp; for (int i = 0; i < n; i++) { cin >> tmp; for (int j = sum + 1; j <= sum + tmp; j++) A[j] = i + 1; sum += tmp; } int m, q; cin >> m; for (int i = 0; i < m; i++) { cin >> q; cout << A[q] << endl; } } ```
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long binSrc(long long arr[], long long n, long long item) { long long mid, lo, hi; lo = 0; hi = n - 1; while (lo <= hi) { mid = lo + ((hi - lo) / 2); if (arr[mid] == item) return mid + 1; if (item < arr[mid]) { if (!mid) return 1; if (arr[mid - 1] < item) return mid + 1; } if (arr[mid] < item) lo = mid + 1; else hi = mid - 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; long long arr[n]; for (long long i = 0; i < n; i++) { cin >> arr[i]; } long long cumSum[n], sz = n; cumSum[0] = arr[0]; for (long long i = 1; i < n; i++) { cumSum[i] = cumSum[i - 1] + arr[i]; } long long m; cin >> m; while (m--) { long long num; cin >> num; cout << binSrc(cumSum, n, num) << "\n"; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long binSrc(long long arr[], long long n, long long item) { long long mid, lo, hi; lo = 0; hi = n - 1; while (lo <= hi) { mid = lo + ((hi - lo) / 2); if (arr[mid] == item) return mid + 1; if (item < arr[mid]) { if (!mid) return 1; if (arr[mid - 1] < item) return mid + 1; } if (arr[mid] < item) lo = mid + 1; else hi = mid - 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; long long arr[n]; for (long long i = 0; i < n; i++) { cin >> arr[i]; } long long cumSum[n], sz = n; cumSum[0] = arr[0]; for (long long i = 1; i < n; i++) { cumSum[i] = cumSum[i - 1] + arr[i]; } long long m; cin >> m; while (m--) { long long num; cin >> num; cout << binSrc(cumSum, n, num) << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 1; int n, x, q; int a[M]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &x); a[i] += x + a[i - 1]; } scanf("%d", &q); while (q--) { scanf("%d", &x); printf("%d\n", lower_bound(a, a + n, x) - a); } }
### Prompt Please provide a Cpp coded solution to the problem described below: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int M = 2e5 + 1; int n, x, q; int a[M]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &x); a[i] += x + a[i - 1]; } scanf("%d", &q); while (q--) { scanf("%d", &x); printf("%d\n", lower_bound(a, a + n, x) - a); } } ```
#include <bits/stdc++.h> using namespace std; int a, b = 0, c, z, y, l, r, t; int main() { cin >> a; int A[a]; for (z = 0; z < a; z++) { cin >> b; if (z > 0) { A[z] = A[z - 1] + b; } else { A[z] = b; } } cin >> a; int B[a]; int C[a]; for (z = 0; z < a; z++) { cin >> B[z]; } for (z = 0; z < a; z++) { l = 0; r = (sizeof(A) / sizeof(A[0])); c = (l + r) / 2; int temp1 = -1, temp2 = -2; while (c >= 0 && c <= (sizeof(A) / sizeof(A[0])) && temp2 != temp1) { if (B[z] <= A[c]) { if (B[z] > A[c - 1]) { C[z] = c + 1; break; } else if (c != 0) { temp2 = temp1; temp1 = A[c]; r = c; } else { C[z] = c + 1; break; } } else if (B[z] > A[c]) { temp2 = temp1; temp1 = A[c]; l = c; } c = (l + r) / 2; } } for (z = 0; z < a; z++) { cout << C[z] << "\n"; } }
### Prompt In Cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a, b = 0, c, z, y, l, r, t; int main() { cin >> a; int A[a]; for (z = 0; z < a; z++) { cin >> b; if (z > 0) { A[z] = A[z - 1] + b; } else { A[z] = b; } } cin >> a; int B[a]; int C[a]; for (z = 0; z < a; z++) { cin >> B[z]; } for (z = 0; z < a; z++) { l = 0; r = (sizeof(A) / sizeof(A[0])); c = (l + r) / 2; int temp1 = -1, temp2 = -2; while (c >= 0 && c <= (sizeof(A) / sizeof(A[0])) && temp2 != temp1) { if (B[z] <= A[c]) { if (B[z] > A[c - 1]) { C[z] = c + 1; break; } else if (c != 0) { temp2 = temp1; temp1 = A[c]; r = c; } else { C[z] = c + 1; break; } } else if (B[z] > A[c]) { temp2 = temp1; temp1 = A[c]; l = c; } c = (l + r) / 2; } } for (z = 0; z < a; z++) { cout << C[z] << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int a[maxn], l[maxn], r[maxn]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a[i] = a[i - 1] + x; l[i] = a[i - 1] + 1; r[i] = a[i]; } r[n + 1] = 1e9; l[n + 1] = 1e9; int m; scanf("%d", &m); while (m--) { int h; scanf("%d", &h); int p = lower_bound(l + 1, l + n + 1, h) - l; if (l[p] > h) p--; printf("%d\n", p); } return 0; }
### Prompt Please formulate a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int a[maxn], l[maxn], r[maxn]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a[i] = a[i - 1] + x; l[i] = a[i - 1] + 1; r[i] = a[i]; } r[n + 1] = 1e9; l[n + 1] = 1e9; int m; scanf("%d", &m); while (m--) { int h; scanf("%d", &h); int p = lower_bound(l + 1, l + n + 1, h) - l; if (l[p] > h) p--; printf("%d\n", p); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[100005], s[1000005]; int main() { int n, i, j, m, q; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } int cou = 1; for (i = 0; i < n; i++) { for (j = 0; j < a[i]; j++) { s[cou++] = i + 1; } } cin >> m; for (i = 0; i < m; i++) { cin >> q; cout << s[q] << endl; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[100005], s[1000005]; int main() { int n, i, j, m, q; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } int cou = 1; for (i = 0; i < n; i++) { for (j = 0; j < a[i]; j++) { s[cou++] = i + 1; } } cin >> m; for (i = 0; i < m; i++) { cin >> q; cout << s[q] << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int A[100005]; int bsearch(int q, int l, int r) { if (l == r - 1) { return l; } int mid = (l + r) / 2; if (A[mid - 1] < q && q <= A[mid]) { return mid; } else if (q <= A[mid - 1]) { return bsearch(q, l, mid); } else { return bsearch(q, mid, r); } return -1; } int main() { int N; cin >> N; A[0] = 0; for (int i = 1; i <= N; ++i) { cin >> A[i]; A[i] += A[i - 1]; } int M, query; cin >> M; for (int i = 0; i < M; ++i) { cin >> query; cout << bsearch(query, 0, N + 1) << endl; } return 0; }
### Prompt In CPP, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int A[100005]; int bsearch(int q, int l, int r) { if (l == r - 1) { return l; } int mid = (l + r) / 2; if (A[mid - 1] < q && q <= A[mid]) { return mid; } else if (q <= A[mid - 1]) { return bsearch(q, l, mid); } else { return bsearch(q, mid, r); } return -1; } int main() { int N; cin >> N; A[0] = 0; for (int i = 1; i <= N; ++i) { cin >> A[i]; A[i] += A[i - 1]; } int M, query; cin >> M; for (int i = 0; i < M; ++i) { cin >> query; cout << bsearch(query, 0, N + 1) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, i; cin >> n; int p[100010]; p[0] = 0; for (i = 0; i < n; i++) { int b; cin >> b; p[i + 1] = p[i] + b; } int m; cin >> m; for (i = 0; i < m; i++) { int worm; cin >> worm; int ans = lower_bound(p, p + n, worm) - p; cout << ans << endl; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, i; cin >> n; int p[100010]; p[0] = 0; for (i = 0; i < n; i++) { int b; cin >> b; p[i + 1] = p[i] + b; } int m; cin >> m; for (i = 0; i < m; i++) { int worm; cin >> worm; int ans = lower_bound(p, p + n, worm) - p; cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long ara[100010]; vector<long long> v1; long long val, sum, val1; long long f, l, mid, mid1, res1, res2, s; long long n, m; void bs(long long s) { f = 0; l = n - 1; while (f <= l) { mid = (f + l) / 2; mid1 = mid - 1; res1 = ara[mid]; res2 = ara[mid1]; if (s < res1 && mid1 < 0) { cout << "1"; break; } else if (s < res1 && s < res2) l = mid - 1; else if (s > res1 && s > res2) f = mid + 1; else if ((s >= res1 && s >= res2) || (s < res1 && s > res2)) { cout << mid + 1; break; } else l = mid - 1; } } int main() { sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> val; sum += val; ara[i] = sum; } cin >> m; for (int i = 0; i < m; i++) { cin >> val1; v1.push_back(val1); } for (int i = 0; i < m; i++) { s = v1.at(i); bs(s); cout << endl; } }
### Prompt Please formulate a cpp solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long ara[100010]; vector<long long> v1; long long val, sum, val1; long long f, l, mid, mid1, res1, res2, s; long long n, m; void bs(long long s) { f = 0; l = n - 1; while (f <= l) { mid = (f + l) / 2; mid1 = mid - 1; res1 = ara[mid]; res2 = ara[mid1]; if (s < res1 && mid1 < 0) { cout << "1"; break; } else if (s < res1 && s < res2) l = mid - 1; else if (s > res1 && s > res2) f = mid + 1; else if ((s >= res1 && s >= res2) || (s < res1 && s > res2)) { cout << mid + 1; break; } else l = mid - 1; } } int main() { sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> val; sum += val; ara[i] = sum; } cin >> m; for (int i = 0; i < m; i++) { cin >> val1; v1.push_back(val1); } for (int i = 0; i < m; i++) { s = v1.at(i); bs(s); cout << endl; } } ```
#include <bits/stdc++.h> using namespace std; int binary(long long int s[], int t, int m) { int l = 0, h = m - 1; while (l <= h) { int mid = (l + h) / 2; if (s[mid] == t) return mid; else if (l == h) return l; else if (t > s[mid]) l = mid + 1; else if (t < s[mid]) h = mid; } } int main() { int n; cin >> n; long long int sum[n + 1]; sum[0] = 0; for (int i = 1; i <= n; i++) { int temp; cin >> temp; sum[i] = sum[i - 1] + temp; } int m; cin >> m; for (int i = 0; i < m; i++) { int t1; cin >> t1; long long int y = binary(sum, t1, n + 1); cout << y << endl; } }
### Prompt Please formulate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int binary(long long int s[], int t, int m) { int l = 0, h = m - 1; while (l <= h) { int mid = (l + h) / 2; if (s[mid] == t) return mid; else if (l == h) return l; else if (t > s[mid]) l = mid + 1; else if (t < s[mid]) h = mid; } } int main() { int n; cin >> n; long long int sum[n + 1]; sum[0] = 0; for (int i = 1; i <= n; i++) { int temp; cin >> temp; sum[i] = sum[i - 1] + temp; } int m; cin >> m; for (int i = 0; i < m; i++) { int t1; cin >> t1; long long int y = binary(sum, t1, n + 1); cout << y << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, i, m, j, cnt = 0, ans, x, sum = 0; cin >> n; int a[n], c[n]; for (i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; c[i] = sum; } cin >> m; int b[m]; for (i = 0; i < m; i++) cin >> b[i]; for (j = 0; j < m; j++) { cout << lower_bound(c, c + n, b[j]) - c + 1 << endl; } return 0; }
### Prompt Generate a CPP solution to the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, i, m, j, cnt = 0, ans, x, sum = 0; cin >> n; int a[n], c[n]; for (i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; c[i] = sum; } cin >> m; int b[m]; for (i = 0; i < m; i++) cin >> b[i]; for (j = 0; j < m; j++) { cout << lower_bound(c, c + n, b[j]) - c + 1 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x; vector<int> sum(n); for (int i = 0; i < n; i++) { cin >> x; if (i == 0) sum[i] = x; else { sum[i] = x + sum[i - 1]; } } int m; cin >> m; vector<int> j(m); for (int i = 0; i < m; i++) cin >> j[i]; for (int i = 0; i < m; i++) { x = j[i]; int l = 0, r = n - 1; if (sum[0] >= x) { cout << 1 << endl; continue; } while (r - l > 1) { int mid = (l + r) / 2; if (sum[mid] >= x) r = mid; else l = mid; } cout << r + 1 << endl; } }
### Prompt Construct a cpp code solution to the problem outlined: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x; vector<int> sum(n); for (int i = 0; i < n; i++) { cin >> x; if (i == 0) sum[i] = x; else { sum[i] = x + sum[i - 1]; } } int m; cin >> m; vector<int> j(m); for (int i = 0; i < m; i++) cin >> j[i]; for (int i = 0; i < m; i++) { x = j[i]; int l = 0, r = n - 1; if (sum[0] >= x) { cout << 1 << endl; continue; } while (r - l > 1) { int mid = (l + r) / 2; if (sum[mid] >= x) r = mid; else l = mid; } cout << r + 1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; long long int M, k, Q, sum = 0, a = 1, W; vector<int> x; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &W); for (int j = 0; j < W; j++) { x.push_back(i); } } scanf("%lld", &M); for (k = 1; k <= M; k++) { scanf("%lld", &Q); printf("%d\n", x[Q - 1]); } return 0; }
### Prompt In CPP, your task is to solve the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; long long int M, k, Q, sum = 0, a = 1, W; vector<int> x; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &W); for (int j = 0; j < W; j++) { x.push_back(i); } } scanf("%lld", &M); for (k = 1; k <= M; k++) { scanf("%lld", &Q); printf("%d\n", x[Q - 1]); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } int m; cin >> m; for (int i = 1; i < n; i++) { v[i] += v[i - 1]; } for (int i = 1; i <= m; i++) { int target; cin >> target; int start = 0; int end = n - 1; while (start <= end) { int mid = (start + end) / 2; if (v[mid] >= target && target > v[mid - 1] && mid != 0) { cout << mid + 1 << endl; break; } else if (v[mid] >= target && mid == 0) { cout << 1 << endl; break; } else if (v[mid] < target) { start = mid + 1; } else { end = mid - 1; } } } }
### Prompt Create a solution in cpp for the following problem: It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } int m; cin >> m; for (int i = 1; i < n; i++) { v[i] += v[i - 1]; } for (int i = 1; i <= m; i++) { int target; cin >> target; int start = 0; int end = n - 1; while (start <= end) { int mid = (start + end) / 2; if (v[mid] >= target && target > v[mid - 1] && mid != 0) { cout << mid + 1 << endl; break; } else if (v[mid] >= target && mid == 0) { cout << 1 << endl; break; } else if (v[mid] < target) { start = mid + 1; } else { end = mid - 1; } } } } ```