#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--)…
- User
#include <bits/stdc++.h> using namespace std;
int main() { int t; cin >> t; while (t--) { int n, q; cin >> n >> q;
int a[n];
long long k[q];codefor (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < q; i++) { cin >> k[i]; } map<long long, long long int> mp; long long int tmp; for (int i = 1; i <= n; i++) { tmp = n - 1 + (n - i) * (i - 1); mp[tmp] += 1; // cout<<tmp<<i<<endl; if (i != n) { mp[tmp - i+1] += a[i] - a[i-1] - 1; } } long long int ans[q]; for (int i = 0; i < q; i++) { if (mp.find(k[i]) != mp.end()) { ans[i] = mp[k[i]]; } else { ans[i] = 0; } } // Output results for (int i = 0; i < q - 1; i++) { cout << ans[i] << " "; } cout << ans[q - 1] << endl; }}
This is the code I wrote for a problem. It gives the correct answer for most of the test cases but fails on one. Can you help me debug this and find a logical error? This was the question ⠀ You are given n points on the x axis, at increasing positive integer coordinates x1<x2<…<xn .
For each pair (i,j) with 1≤i<j≤n , you draw the segment [xi,xj] . The segments are closed, i.e., a segment [a,b] contains the points a,a+1,…,b .
You are given q queries. In the i -th query, you are given a positive integer ki , and you have to determine how many points with integer coordinates are contained in exactly ki segments.
Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104 ). The description of the test cases follows.
The first line of each test case contains two integers n , q (2≤n≤105 , 1≤q≤105 ) — the number of points and the number of queries.
The second line of each test case contains n integers x1,x2,…,xn (1≤x1<x2<…<xn≤109 ) — the coordinates of the n points.
The third line of each test case contains q integers k1,k2,…,kq (1≤ki≤1018 ) — the parameters of the q queries.
It is guaranteed that the sum of n over all test cases does not exceed 105 , and the sum of q over all test cases does not exceed 105 .
- Assistant