fork download
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4. const int N = 1005;
  5. int a[N][N], r[N * 2][N * 2], p[N * 2][N * 2], res[N][N];
  6. int sum(int x1, int y1, int x2, int y2) {
  7. if(x1 > x2 or y1 > y2) return 0;
  8. return p[x2][y2] - p[x1 - 1][y2] - p[x2][y1 - 1] + p[x1 - 1][y1 - 1];
  9. }
  10. signed main() {
  11. ios::sync_with_stdio(0);
  12. cin.tie(0); cout.tie(0);
  13. int n, k; cin >> n >> k;
  14. for(int i = 1; i <= n; i ++)
  15. for(int j = 1; j <= n; j ++) {
  16. cin >> a[i][j];
  17. int x = i + j, y = i - j + n;
  18. r[x][y] = a[i][j];
  19. }
  20. int z = 2 * n + 1;
  21. for(int i = 1; i <= z; i ++)
  22. for(int j = 1; j <= z; j ++)
  23. p[i][j] = r[i][j] + p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1];
  24. for(int i = 1; i <= n; i ++)
  25. for(int j = 1; j <= n; j ++) {
  26. int x = i + j, y = i - j + n;
  27. int x1 = max(1LL, x - k);
  28. int y1 = max(1LL, y - k);
  29. int x2 = min(z, x + k);
  30. int y2 = min(z, y + k);
  31. res[i][j] = sum(x1, y1, x2, y2);
  32. }
  33. for(int i = 1; i <= n; i ++) {
  34. for(int j = 1; j <= n; j ++)
  35. cout << res[i][j] << " ";
  36. cout << "\n";
  37. }
  38. }
Success #stdin #stdout 0s 2500KB
stdin
Standard input is empty
stdout
0 0 
0 0