fork download
  1. // They Plan, and Allah Plans...
  2. // Indeed Allah is the Best of Planners.
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. typedef long long ll;
  7. /*======================================= Macro Start====================================================*/
  8. #define fastio \
  9. ios_base::sync_with_stdio(false); \
  10. cin.tie(NULL);
  11. #define precise \
  12. cout.precision(10); \
  13. cout << fixed;
  14. #define all(x) x.begin(), x.end()
  15. #define endl "\n"
  16. #define memset(arr, init) memset(arr, init, sizeof(arr))
  17. #define gcd(a, b) __gcd(a, b)
  18. #define lcm(a, b) (a * (b / gcd(a, b)))
  19. #define bitoggle(number, pos) number ^ (1 << pos)
  20. #define checkbit(number, pos) number &(1 << pos)
  21. #define biton(number, pos) number | (1 << pos)
  22.  
  23. const double PI = acos(-1);
  24. const ll infinity = (1000000000 + 7);
  25.  
  26. void solve()
  27. {
  28. int d;
  29. cin >> d;
  30. int n;
  31. cin >> n;
  32. int grid[1025][1025], sgx[1025][1025], sgy[1025][1025];
  33. memset(grid, 0);
  34. memset(sgx, 0);
  35. memset(sgy, 0);
  36. for (int i = 0; i < n; i++)
  37. {
  38. int x, y, popu;
  39. cin >> x >> y >> popu;
  40. grid[x][y] = popu;
  41. }
  42. int ans = 0;
  43. for (int i = 0; i < 1025; i++)
  44. {
  45. for (int j = 0; j < 1025; j++)
  46. {
  47. sgx[i][j] = grid[i][j];
  48. sgy[i][j] = grid[i][j];
  49. if (i != 0)
  50. {
  51. sgx[i][j] += sgx[i - 1][j];
  52. }
  53. if (j != 0)
  54. {
  55. sgy[i][j] += sgy[i][j - 1];
  56. }
  57.  
  58. int a = sgx[i][j], b = sgy[i][j];
  59. if ((i - 2 * d) > 0)
  60. {
  61. a -= sgx[i - 2 * d - 1][j];
  62. }
  63. if ((j - 2 * d) > 0)
  64. {
  65. b -= sgy[i][j - 2 * d - 1];
  66. }
  67. ans = max(ans, a + b);
  68. }
  69. }
  70. cout << ans << endl;
  71. }
  72. void Task()
  73. {
  74. int test;
  75. cin >> test;
  76. for (int i = 1; i <= test; i++)
  77. {
  78. //cout << "Case " << i << ": ";
  79. solve();
  80. }
  81. }
  82.  
  83. int main()
  84. {
  85. Task();
  86. return 0;
  87. }
Success #stdin #stdout 0.01s 15776KB
stdin
1
1
2
4 4 10
6 6 20
stdout
40