1#include <bits/stdc++.h>2usingnamespace std;3intmain() {4 ios::sync_with_stdio(0); cin.tie(0); 5int n, k, q; cin >> n >> k >> q;6///we maintain previous window and updated window. suprisingly easier than p17//throwing 2d seg tree at this problem is too hard so we just brute force!!!!8//since k is so tiny we brute force. We do not need to loop all k * k squares only the ones influenced9//the cur[i][j] will be the sum of beauty values inside the k × k square and top left corner is row i col j10vector<vector<int>>pre(n, vector<int>(n)), cur(n, vector<int>(n));11int ans =0;12while (q--) {13int r, c, v; cin >> r >> c >> v;14 r--; c--;15int change = v - pre[r][c];16 pre[r][c] = v;17// boundaries of winodws18int topr =max(0, r - k +1), bottomr =min(r, n - k), leftcol =max(0, c - k +1), rightcol =min(c, n - k);19//all windows affected by this update 20for (int i = topr; i <= bottomr; i++) {21for (int j = leftcol; j <= rightcol; j++) {22 cur[i][j] += change;23 ans =max(ans, cur[i][j]);24 }25 }26 cout << ans <<'\n';27 }28return0;29}
☕
Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.
Python records executed lines and locals automatically. For selected values in any language, add // @trace i, total on its own valid line; Python uses # @trace i, total.
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.
Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.