I would like to ask for help with my following solution (based on USACO posted solution, I just do it myself for practice) to "Convoluted Intervals " 2021 Dec. Silver.
Below is my code.
When I submit it, all cases are correct except it got an X (wrong answer) on case 20;
when I change the line “vector aFreq, bFreq, ans;” to “vector aFreq, bFreq, ans;”: case 20 is correct. However, it got an ! (runtime error or memory exceeded) on case 2, 6, 8, and 16.
Any help is highly appreciated!
#include #include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
int a, b;
long long num = 0;
vector<int> aFreq, bFreq, ans;
cin >> N >> M;
aFreq.assign(M + 1, 0);
bFreq.assign(M + 1, 0);
ans.assign(2 * M + 1, 0);
for(int i = 0; i < N; i++) {
cin >> a >> b;
aFreq[a]++;
bFreq[b]++;
}
for(int i = 0; i <= M; i++) {
for(int j = 0; j <= M; j++) {
ans[i + j] += aFreq[i] * aFreq[j];
ans[i + j + 1] -= bFreq[i] * bFreq[j];
}
}
for(int i = 0; i < 2 * M + 1; i++) {
num += ans[i];
cout << num << endl;
}
return 0;
Sorry, there is an error in my posted question (I think it is caused by some automatic formatting). it should be:
when I change the line “vector aFreq, bFreq, ans;” to “vector aFreq, bFreq, ans;”: case 20 is correct. However, it got an ! (runtime error or memory exceeded) on case 2, 6, 8, and 16.
Seems to be pretty clear.
For case 20, if you use ints, integer overflow occurs.
However, if you use long longs, which take twice as much memory as ints, the grader runs out of memory.
EDIT: This is wrong, see below post.