💡

[鉄則A09] Winter in ALGO Kingdom ハマッた落とし穴

2024/07/24に公開

``````#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

typedef long long ll;

int main() {
int h, w, n;
cin >> h >> w >> n;
vector<int> a(n), b(n),c(n),d(n);
vector<vector<int> > z(h+1, vector<int>(w+1,0));
vector<vector<int> > x(h+1, vector<int>(w+1, 0));

for(int i = 0; i < n; i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
x[a[i]][b[i]]++;
x[a[i]][d[i] + 1]--;
x[c[i] + 1][b[i]]--;
x[c[i] + 1][d[i] + 1]++;
}

for(int i = 1; i <= h; i++){
for(int j = 1; j <= w; j++){
z[i][j] = z[i][j-1] + x[i][j];
}
}

for(int i = 1; i <= w; i++){
for(int j = 1; j <= h; j++){
z[j][i] += z[j-1][i];
}
}

for(int i = 1; i <= h; i++){
for(int j = 1; j <= w; j++){
if(j == w){
cout << z[i][j];
}else{
cout << z[i][j] << " ";
}
}
cout << endl;
}

return 0;
}
``````

``````vector<vector<int> > x(h+1, vector<int>(w+1, 0));
...
x[a[i]][b[i]]++;
x[a[i]][d[i] + 1]--;
x[c[i] + 1][b[i]]--;
x[c[i] + 1][d[i] + 1]++;
...
``````

にてxの配列を参照するときに、配列の要素数が足りないためエラーになる。

``````vector<vector<int> > x(h+2, vector<int>(w+2, 0));
``````

にすれば解決