We need to solve: Given a huge grid H x W (up to 1e9), but only up to N=1e5 black cells painted. Count the number of 3x3 subrectangles that contain exactly j black cells, for j=0..9. Output each count on separate line.
We need efficient approach that uses the fact that only black cells affect counts; most 3x3 squares will have 0 black cells. However, H,W huge but N small, so many 3x3 squares will have 0 black cells. But we also need to account for those that contain some black cells. Since black cells are sparse, only 3x3 squares that intersect at least one black cell could have non-zero count. However, we must be careful: there may be many 3x3 squares with no black cells; we can compute total number of 3x3 squares = (H-2)(W-2) and subtract those that have at least one black cell after counting distribution. But note that the black cells can be up to 1e5, but the number of 3x3 squares that intersect any black cell might be up to 9N? Actually each black cell belongs to up to 9 squares (the 3x3 squares that cover it). Since each black cell lies in the intersection of rows between max(1, a_i-2) to min(H-2, a_i) for the top-left row? Wait careful: A 3x3 subrectangle is defined by its top-left corner (r,c) where 1 <= r <= H-2, 1 <= c <= W-2. The subrectangle covers rows r, r+1, r+2 and columns c, c+1, c+2. So a black cell at (a,b) is inside a 3x3 subrectangle if the top-left corner (r,c) satisfies r in [a-2, a] intersect [1, H-2] and c in [b-2, b] intersect [1, W-2]. So for each black cell, the number of 3x3 subrectangles that contain it is at most 9, but could be less near edges. So total candidate squares that contain at least one black cell is O(9N) = 9e5 at most. However, there could be overlapping, so distinct squares might be less. But we can simply iterate over all possible top-left corners that are affected by black cells: i.e., those squares for which there is at least one black cell inside. For each such square, compute the number of black cells in it. Since squares are many? At most 9*1e5 = 900k, that is manageable. Then we can accumulate counts for j from 0 to 9. But we have to also account for squares with 0 black cells; we can compute total squares and subtract sum_{j>0} count_j.
But careful: H,W up to 1e9, so total squares = (H-2)*(W-2) which can be up to ~1e18, which fits in 64-bit integer? (1e9-2)^2 ~ 1e18 - 4e9 +4, yes < 2^63-1 ~9.22e18, so 64-bit signed is fine if we use long long.
So plan: Collect all 3x3 squares that have at least one black cell. For each black cell, we generate all possible top-left corners (r,c) such that the square includes (a,b). r can be a-2, a-1, a (provided within [1, H-2]), similarly c from b-2,b-1,b within [1, W-2]. For each such (r,c), we can later count black cells in that square. However, many squares will be generated multiple times. So we can store these squares in a set (like map or unordered_map) keyed by (r,c). For each generated square, we will accumulate a count of black cells (by adding contributions). But we need to count exactly how many black cells in each square. Since each black cell will be processed and we generate the squares containing it, we can increment a counter for that square. At the end, we have for each distinct square the number of black cells (from 1 to 9). Then we can tally the frequencies for each j, including j=0 computed as total squares minus sum of non-zero squares counts.
But careful: N up to 1e5, each black cell generates up to 9 squares, so at most 900k entries in the map. That's fine.
Implementation: We'll use an unordered_map with key = (r,c) encoded as a 64-bit integer, e.g., r*(W-2+something) but W may be up to 1e9, product could overflow. Better to use pair<int,int>? But r and c are up to 1e9? Actually r = top-left row, which ranges from 1 to H-2, H up to 1e9, so r up to 1e9. c similarly. So we can use pair<long long, long long> (or pair<int64_t,int64_t>). In C++, unordered_map with pair requires hash; we can define our own hash, or use map (ordered) which is O(log N) but N=900k, log(900k) ~20, so 900k * 20 = 18 million operations, still okay. But unordered_map with custom hash may be faster. I'd prefer using map for simplicity; but careful about time: 900k insertions and lookups maybe okay. Actually each insertion (key not present) will be O(log size) per operation, worst-case about 900k * log(900k) ~ 900k * 20 = 18e6, fine. And we also need to increment each time we encounter a square (for each black cell's generated squares). So total operations ~9N (maybe 900k increments). For each black cell, we generate up to 9 squares; for each, we need to check map and increment. So total ~9N map operations. That's ~9e5 map accesses. That's definitely fine with map.
Alternative: Use dictionary (hash map) for faster. I'll use unordered_map with hash of a combined 64-bit: key = r * (W-2+1?) But W can be up to 1e9, product may overflow 64-bit? r up to 1e9, W up to 1e9, product up to 1e18 fits in 64-bit. Use key = r * (1e9+7) + c? That's not safe if W-2 up to 1e9. Simpler: use pair and default hash for pair<int,int> in unordered_map? In C++ unordered_map doesn't have hash for pair by default unless we define it. So we can use map or define hash. For simplicity, I'll use map<long long, int> with key = r * (W+1) + c? But to ensure uniqueness, we can use r * (W+2) + c; but W+2 up to 1e9+2, product could overflow 64-bit? r up to 1e9, factor up to 1e9+2, product up to 1e18, fits in signed 64-bit? 2^63-1 ~9.22e18, so safe. But we need to use unsigned maybe, but signed 64-bit works. Actually 1e9 * 1e9 = 1e18, which is less than 9.22e18, so it's okay. So define key = r * (W+10) + c. But careful: W can be 1e9, r up to H-2 ~ 1e9, so product ~ 1e18; fits in long long (usually 64-bit). Use unsigned long long to be safe. Or use pair and map.
I think map<pair<int,int>, int> is simplest and safe.
Edge Cases: H or W less than 3? Constraints say H,W >=3, so fine.
Also, note that when H or W is exactly 3, total squares = (H-2)*(W-2) which could be 1, etc.
Now complexity: O(N * min(9, (H-2)*(W-2)?) ) = O(N) essentially.
We must careful: For each black cell, we generate r in {a-2, a-1, a} intersect [1, H-2]; similarly for c. That's straightforward.
After processing all black cells, we will have counts for each affected square. Then we create an array ans[10] = {0}. For each square in map, let cnt = value (between 1 and 9), ans[cnt]++.
Then ans[0] = total_squares - sum_{j=1..9} ans[j].
But caution: Could there be squares that have more than 9 black cells? No, max is 9 because 3x3 = 9 cells.
Also, ensure we don't double count squares: We increment the count for each black cell. So a square that contains, say, 3 black cells, will be incremented 3 times, so its final value = 3. That's correct.
But careful: For squares near edges, the number of possible r,c from each black cell might be less than 3 (if near top/bottom edges). But that is fine.
Now we must check if there is any other nuance: The grid is huge but N is small; the method seems correct.
But there is one potential pitfall: The total number of 3x3 squares is (H-2)(W-2). This can be up to ~1e18, so we need 128-bit? In C++, long long (signed 64-bit) can hold up to 9.22e18, so if H and W are up to 1e9, (1e9-2)(1e9-2) ≈ (1e9)^2 - 4e9 + 4 = 1e18 - 4e9 +4 ≈ 1e18, which is < 9.22e18, so safe. Actually 1e18 is exactly 10^18. 10^18 < 9.22e18, so fits. So long long is fine.
But wait: H and W can be up to 1e9 each, so maximum product is (1e9)*(1e9) = 10^18. That is within 2^63-1 (approx 9.22e18). So yes.
Now we must consider memory: map with up to 9e5 entries, each storing key (two ints) and int value, that's fine.
Now test with provided examples.
Example 1:
H=4, W=5 => total squares = (4-2)(5-2)=23=6.
Black cells: 8 positions. Our algorithm: For each black cell, generate squares containing it. After processing, we count squares with certain counts. Output: We see ans[3]=2, ans[4]=4, others 0, so ans[0] = 6-6=0. That matches.
Example 2: Let's test mentally? Not necessary.
Example 3: H=W=1e9, N=0 => total squares = (1e9-2)(1e9-2) = (10^9-2)^2. Compute: (10^9-2)^2 = 10^18 - 410^9 + 4 = 1000000000000000000 - 4000000000 + 4 = 999999996000000004? Let's compute: 10^18 is 1e18 = 1000000000000000000. Subtract 4e9 = 4000000000 gives 999999996000000000? Actually careful: 1e18 - 4e9 = 1,000,000,000,000,000,000 - 4,000,000,000 = 999,999,996,000,000,000. Then plus 4 gives 999,999,996,000,000,004. Yes output matches.
Thus algorithm seems correct.
But note: There's a subtlety: If N is large (like 10^5), the map may contain up to 9*10^5 entries, which is fine. But we must also consider that black cells may be at positions that cause some squares to be counted more than 9? No.
Now could there be squares that contain many black cells? Possibly up to 9, but we have to ensure that our incrementing method yields correct count for each square. Yes.
Edge Cases: Multiple black cells at same coordinates are not allowed; distinct.
Now Implementation steps:
- Read H, W, N.
- total = (H-2) * (W-2) // as long long.
- Create map<pair<int,int>, int> counter.
- For each i from 0 to N-1:
read a, b.
For dr in { -2, -1, 0 } maybe easier: for r0 = a-2 to a: if 1 <= r0 <= H-2:
for c0 = b-2 to b: if 1 <= c0 <= W-2:
key = {r0, c0}
counter[key]++ // if not present, default 0, then ++.
But careful: When using map, accessing counter[key] will default initialize to 0 if not exist, then increment. So fine.
-
After loop, ans[10] = {0}. For each pair in counter: cnt = it.second (int). ans[cnt]++. Also sum_nonzero += ans[cnt] (or later compute sum of ans[1..9]).
Note: Since cnt is between 1 and 9, but could it be 0? No, because only squares that we inserted have at least one black cell. However, if we insert a square from a black cell, it will have at least 1. But could it be that we inserted a square but later it had count 0? No, because each insertion corresponds to a black cell inside, so each insertion increments; thus any square in the map has count >=1.
-
Compute sum1to9 = accumulate(ans[1..9]).
ans[0] = total - sum1to9.
But careful: total is (H-2)*(W-2) which can be up to 1e18; sum1to9 is at most number of distinct squares (max 9N) which is <= 9e5, so fits in long long.
- Print ans[0] to ans[9] each on its own line.
Check for overflow when computing total: Use long long.
Potential issues: H-2 and W-2 are up to 1e9, but if we multiply two ints, product may overflow int before being assigned to long long. In C++, if H and W are int (assuming 32-bit), (H-2)*(W-2) is computed as int, overflow if >2^31-1. So we need to cast to long long. So compute total = (H-2) * 1LL * (W-2). Or store H,W as long long.
We'll read H,W as long long or int? With constraints up to 1e9, they fit in int (since int is at least 2^31-1 = 2.147e9, so okay). But multiplication may overflow int if product > 2^31-1. But (H-2)*(W-2) up to 1e18 > 2^31-1, so it will overflow int. So we must ensure we compute in 64-bit. So we do:
long long H, W; // use long long for safety.
cin >> H >> W >> N;
long long total = (H-2) * (W-2); // Since H and W are long long, multiplication yields long long, safe.
Alternatively, use 1LL.
Now about the keys: r0 and c0 are within [1, H-2] and [1, W-2] respectively. H-2 could be up to 1e9, so r0,c0 fit in int? 1e9 fits in 32-bit signed int? 1e9 is less than 2^31-1 (2.147e9), so fits in int. But if H=1e9, H-2=999,999,998 which is <2^31-1, so int is okay. However, in some environments int may be 32-bit, so safe. But to be safe, we can use long long for r0,c0 as well.
Since we are using map<pair<int,int>, int>, if we store int keys, they must be within int range. That's fine. However, if H=1e9, H-2 = 999,999,998 fits in int. So okay.
But what if H=10^9? H-2 = 999,999,998 < 2^31-1. So int works. But if H could be up to 10^9, yes. So int is fine.
But caution: H,W up to 10^9, so H-2 is up to 10^9-2, still fits in 32-bit signed (max 2147483647). So int (32-bit) is okay.
Thus we can use int for coordinates.
Now about reading N=0: Then the loop over black cells is empty. counter is empty. sum1to9 = 0. ans[0] = total. Print ans[0] and then zeros. That matches.
Now is there any issue with squares that have more than 9 black cells? That can't happen because a 3x3 square only has 9 cells, each can be black at most once, and we only have N distinct black cells. So count max =9.
Now complexity is fine.
Now we need to output exactly 10 lines, each containing the count.
Now one more nuance: The grid is huge, but black cells may be placed such that some squares contain many black cells, but that's fine.
Now we should consider if there is a more efficient method? Not needed.
Now potential memory/time improvement: Since we generate each square multiple times, we could use a hash map and accumulate. That's what we do.
Now verify with example 2 manually? Not needed.
Now let's think about correctness: Since we count each square's black cells by summing contributions from black cells that lie inside it, and each black cell increments exactly those squares that contain it, we get correct count.
Now one concern: The coordinates a_i, b_i are given as row, column, both 1-indexed. That's consistent.
Now we should also consider that the number of distinct squares that contain at least one black cell is at most 9N, but could be less if many black cells are close. That's fine.
Now implement.
Implementation details:
We'll use:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll H, W;
int N;
cin >> H >> W >> N;
map<pair<int,int>, int> mp;
for (int i=0; i<N; ++i) {
int a, b;
cin >> a >> b;
for (int dr = -2; dr <= 0; ++dr) {
int r = a + dr;
if (r < 1 || r > H-2) continue;
for (int dc = -2; dc <= 0; ++dc) {
int c = b + dc;
if (c < 1 || c > W-2) continue;
mp[{r, c}]++;
}
}
}
vector ans(10, 0);
for (auto &p : mp) {
int cnt = p.second;
if (cnt >= 1 && cnt <= 9) {
ans[cnt]++;
}
}
ll total = (H-2) * (W-2);
ll non_zero = accumulate(ans.begin()+1, ans.end(), 0LL);
ans[0] = total - non_zero;
for (int j=0; j<10; ++j) {
cout << ans[j] << endl;
}
return 0;
}
But we must careful: The accumulation of ans[1..9] may not be correct if we have ans[10] and ans[9] etc. Yes.
But we also need to consider that some squares could theoretically have more than 9? No.
Now test with sample 1:
H=4,W=5 => total=6.
Black cells:
1 1: generates r in [ -1? Actually a=1, r from -1 to 1? r must be 1..2? H-2=2, so r=1? Because a-2=-1 invalid, a-1=0 invalid, a=1 valid. Similarly for b=1: c from -1,0,1 => only c=1 valid. So square (1,1) count++.
1 4: a=1 => r=1 only; b=4 => c from 2,3,4? but W-2=3, so c max=3? Actually c must be <=3 because top-left column must be <=W-2=3. b=4 => c options: 2,3,4? c=4 >3 invalid; c=3? b-1=3 => valid; c=2? b-2=2 => valid. So squares (1,2) and (1,3). Also c from b? Actually c from -2,-1,0 => b+dc: dc=-2 => 2, dc=-1 =>3, dc=0=>4. So (1,2) and (1,3) valid. So increments.
1 5: a=1 => r=1 only; b=5 => c options: 3,4,5? But W-2=3 => c max 3 => only c=3 valid (5-2=3). So square (1,3) increment.
2 3: a=2 => r options: 0,1,2? But r must be >=1 <=2 => r=1,2 valid? a-2=0 invalid, a-1=1 valid, a=2 valid. So r=1 and 2. b=3 => c options: 1,2,3? Since W-2=3, c max=3 => all valid? b-2=1, b-1=2, b=3. So squares: (1,1),(1,2),(1,3),(2,1),(2,2),(2,3). That's 6 squares.
3 1: a=3 => H-2=2? Actually H=4, H-2=2. So r options: 1,2? a-2=1, a-1=2, a=3 invalid because >2. So r=1,2 valid. b=1 => c options: only c=1 valid. So squares (1,1) and (2,1).
3 2: a=3 => r=1,2; b=2 => c options: 0,1,2? but c>=1 <=3 => c=0 invalid, c=1,2 valid. So squares: (1,1),(1,2),(2,1),(2,2).
3 4: a=3 => r=1,2; b=4 => c options: 2,3,4? c max=3 => c=2,3 valid. So squares: (1,2),(1,3),(2,2),(2,3).
4 4: a=4 => H-2=2, r options: 2? because a-2=2 valid, a-1=3 invalid >2, a=4 invalid. So r=2 only. b=4 => c options: 2,3,4? c max=3 => c=2,3 valid. So squares (2,2),(2,3).
Now summing all increments, we can compute final counts for squares:
List all distinct squares: possible top-left rows 1..2, cols 1..3. So squares: (1,1),(1,2),(1,3),(2,1),(2,2),(2,3). Count contributions:
Let's tally manually:
Initialize all to 0.
From (1,1): increments from: 1 1; 2 3? (1,1) from 2 3? Actually 2 3 contributed to (1,1) because r=1, c=1? For 2 3, r=1 (a-1) and c=1 (b-2) => yes; also 3 1 gave (1,1); 3 2 gave (1,1); maybe others? Also 2 3 gave (1,1) we counted? Actually from 2 3, squares: (1,1),(1,2),(1,3),(2,1),(2,2),(2,3). So (1,1) gets one from that. So total from list: 1 1 (1), 2 3 (1), 3 1 (1), 3 2 (1). Also check if any other black cell gives (1,1): 1 4? no; 1 5? no; 3 4? no; 4 4? no. So total = 4? But 3 2 gave (1,1)? yes. So (1,1) count =4. However from example output, they have squares with 3 and 4 black cells. Possibly (1,1) has 4? Let's continue.
(1,2): from: 1 4 (1), 2 3 (1) (since r=1, c=2 from b-1?), actually 2 3: r=1 (a-1) and c=2 (b-1) => yes; 3 2 gave (1,2) (r=1, c=2); 3 4 gave (1,2) (r=1, c=2 from b-2? 3 4: b-2=2 => c=2); also maybe others? 1 5? (1,5) gave (1,3) only; 3 1? no; 4 4? no; 1 1? no; So contributions: 1 4 (1), 2 3 (1), 3 2 (1), 3 4 (1) -> total 4? Also check 1 5? gave (1,3) only. So (1,2) =4? That's also 4.
(1,3): from: 1 4 (1? Actually 1 4 gave (1,3) from dc=0? Wait 1 4 gave (1,2) and (1,3)? Actually we said b=4 => c=2,3 valid, so yes (1,3) from dc=-1? Actually b+dc: dc=-1 -> 3? yes; 1 5 gave (1,3); 2 3 gave (1,3) from r=1, c=3 (b=3? b=3, dc=0 gives c=3); 3 4 gave (1,3) from r=1, c=3 (b-1=3); maybe 4 4? no; 3 2? no; So contributions: 1 4 (1), 1 5 (1), 2 3 (1), 3 4 (1) -> total 4? That would be 4 again. But example says there are 2 squares with 3 black cells and 4 squares with 4 black cells. Which squares have 3? Possibly some have 3. Let's tally more carefully.
We need to recount because maybe some contributions overlap incorrectly. Let's systematically compute counts for each square.
List black cells:
1: (1,1)
2: (1,4)
3: (1,5)
4: (2,3)
5: (3,1)
6: (3,2)
7: (3,4)
8: (4,4)
Now define squares:
S11 (r=1,c=1): covers cells (1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3). Which black cells in this set? Check each black:
(1,1) -> inside
(1,4) -> column 4 no
(1,5) -> no
(2,3) -> yes (row2 col3)
(3,1) -> yes
(3,2) -> yes
(3,4) -> column 4 no
(4,4) -> row4 no
Thus 4 black cells: (1,1),(2,3),(3,1),(3,2). So count =4.
S12 (r=1,c=2): covers (1,2-4),(2,2-4),(3,2-4). Black cells:
(1,1) no (col1)
(1,4) yes
(1,5) col5 no? Actually columns 2-4 => includes col4, yes (1,4) is in. (1,5) col5 no.
(2,3) yes
(3,1) no
(3,2) yes (col2 row3)
(3,4) yes (col4 row3)
(4,4) row4 no.
Thus black: (1,4),(2,3),(3,2),(3,4) = 4. So count =4.
S13 (r=1,c=3): covers columns 3-5, rows 1-3. Black:
(1,1) no
(1,4) yes (col4)
(1,5) yes (col5)
(2,3) yes (col3)
(3,1) no
(3,2) col2 no
(3,4) yes (col4)
(4,4) row4 no
=> (1,4),(1,5),(2,3),(3,4) = 4? That's 4. But example says there are 2 squares with 3 black cells. Which one has 3? Let's check S21? Actually squares with top-left row=2.
S21 (r=2,c=1): covers rows 2-4, cols 1-3. Black:
(1,1) row1 no
(1,4) no
(1,5) no
(2,3) yes (row2 col3)
(3,1) yes (row3 col1)
(3,2) yes (row3 col2)
(3,4) col4 no
(4,4) row4 col4? col4 no (cols1-3)
=> black: (2,3),(3,1),(3,2) = 3. So count=3.
S22 (r=2,c=2): rows2-4, cols2-4. Black:
(1,1) no
(1,4) row1 no
(1,5) no
(2,3) yes (col3)
(3,1) col1 no
(3,2) yes (col2 row3)
(3,4) yes (col4 row3)
(4,4) yes (row4 col4) but row4 is within rows2-4? rows are 2,3,4 => yes row4 included, col4 within 2-4 => yes. So (4,4) is inside? Actually check: top-left (2,2) covers rows 2,3,4 and columns 2,3,4. So (4,4) is row4 col4, inside. So black: (2,3),(3,2),(3,4),(4,4) = 4.
But careful: (3,4) is inside, yes. So count = 4? That would be 4.
S23 (r=2,c=3): rows2-4, cols3-5. Black:
(1,1) no
(1,4) row1 no
(1,5) row1 no
(2,3) yes (col3)
(3,1) col1 no
(3,2) col2 no
(3,4) yes (col4)
(4,4) yes (col4 row4)
Also (1,5) no because row1 not in rows 2-4. So black: (2,3),(3,4),(4,4) = 3? Also (1,5) is outside. So count = 3.
Thus we have squares: S21=3, S23=3, others are 4: S11,S12,S13,S22? Actually S13=4, S22=4. So that gives 2 squares with 3, and 4 squares with 4? Wait total squares: there are 6 squares. Counts: 3: S21, S23 => 2 squares; 4: S11, S12, S13, S22 => that's 4 squares? But S13 we computed as 4, yes; S22=4; S11=4; S12=4; that's 4 squares with count 4. That sums to 6 squares, all accounted. But example output says:
0
0
0
2
4
0
0
0
0
0
That is: j=0:0, j=1:0, j=2:0, j=3:2, j=4:4, j=5:0,... So indeed there are 2 squares with exactly 3 black cells, and 4 squares with exactly 4 black cells. That matches our manual count. Good.
Our algorithm would produce ans[4] = 4, ans[3] = 2.
Now consider if there is any square that contains 0 but we incorrectly counted because map contains no entry for it. Yes.
Now regarding total = 6, sum of nonzero counts = 6, ans[0] = 0.
Thus algorithm works.
Now we should also consider that sometimes a black cell may generate a square that is outside [1, H-2]? Already filtered.
Now there is one more nuance: The total number of squares (H-2)*(W-2) may be larger than number of squares we can represent? No.
Now we must implement efficiently. Using map with pair<int,int> is fine. However, note that we will be doing many insertions; each insertion may allocate new node. That's okay.
Potential alternative: Use unordered_map with custom hash. I'll stick with map.
But wait: In some competitive programming environments, using map with 900k entries may be near limit? 900k entries is not huge; memory usage around maybe 40 bytes per entry * 900k = 36 MB, which is okay. Actually typical map node may be larger (~40-60 bytes). But still within typical 256-512 MB limit. So fine.
But there is a subtlety: We might generate duplicate squares many times, but map will handle.
Now there is also the possibility that N=100,000 and all black cells are in a small region, causing many to map to the same squares? But that's okay.
Now edge-case: H or W is less than 3? But constraints guarantee >=3, so total squares >=1? Actually (H-2)(W-2) could be zero if H=3 and W=3? (3-2)(3-2)=1, so at least 1? But if H=3 and W=1000000000? That's fine.
Now I'll implement.
But I see a potential improvement: Use long long for total, and also ensure that the product (H-2)*(W-2) is computed using 64-bit. Already.
Now, if H or W is up to 10^9, and we compute H-2, it can be negative? No.
Now check reading: Input may have many lines; use fast I/O (ios::sync_with_stdio(false); cin.tie(0);).
Now final code.
But before finalizing, consider if there is any scenario where a square could get more than 9 increments erroneously because some black cell is counted multiple times? No, each black cell is unique and increments each square containing it exactly once.
Now test with sample 2 using code after writing? We'll trust.
Now let's consider the computational complexity for worst-case N=100k, each generating up to 9 squares, we do up to 900k map inserts/updates. Each map operation O(log M) where M up to 900k, maybe ~20 comparisons. So ~18 million operations, which is fine.
Now memory: map of up to 900k entries. Each entry is pair<pair<int,int>, int>. In C++ map typically uses nodes with key and value. Could be 32-48 bytes each. 900k*48 = ~43 MB, okay.
Now we need to ensure we don't accidentally cause overflow in keys: pair<int,int> with int range safe.
Now implementation details: Use int for a_i,b_i, but also need to store H and W as long long. So careful when comparing r <= H-2, need to convert H-2 to long long? Actually H is long long, H-2 is long long. r is int, but if H-2 might exceed int? H-2 could be up to 1e9 -2, fits in int. So we can compare int r with long long H-2, they'll be promoted. So fine.
But we must be careful that when we compute r = a + dr, a is int, dr is int, so r is int. If a is near max int? a up to 1e9, a-2 is 999,999,998, fits int. But what if H > 2^31-1? But H max is 1e9, so safe.
Thus no issue.
Now think about potential pitfalls: The map key uses int for coordinates. But H-2 and W-2 can be as large as 1e9, which fits in signed 32-bit? Yes, because maximum signed int is 2,147,483,647. So okay.
Now I'll code.
But before that, consider alternative: Could we use array/vector to store only affected squares? Yes, we can collect all candidate squares (with possible duplicates) and then sort and count. That may be more efficient memory-wise? Actually we have up to 9N entries, we could store them all in a vector of pair<int,int> of length up to 9N, then sort, then traverse and count frequencies. That avoids map overhead, and sorting O(M log M) with M up to 900k, comparable. Memory: vector of 900k pairs (each pair 8 bytes) => ~7.2 MB, plus temporary. Then after sorting, we can iterate and count occurrences. That is efficient and maybe faster. Since map with dynamic allocation overhead could be slower. So I'll adopt the sorting method.
Plan with sorting:
- Create vector<pair<int,int>> squares; (or use long long encoded? but pair is fine)
- For each black cell, generate all valid top-left corners (r,c) as before, and push_back({r, c}).
- After processing all black cells, sort(squares.begin(), squares.end()).
- Then iterate through sorted vector, count consecutive equal pairs. For each group of identical (r,c), let cnt = size of group. That cnt is the number of black cells in that square. Then increment ans[cnt]++.
- After processing, ans[0] = total - sum_{j>0} ans[j].
But careful: What if the same square appears multiple times? Yes, that's exactly the count. So this works and avoids map.
Complexities: Sorting M up to 9e5, which is fine. Also memory: M entries of pair<int,int>; each pair is 8 bytes if using int (likely 8 bytes). So ~7.2 MB plus vector overhead, fine.
We need to ensure we don't push duplicates from same black cell? Actually a black cell can generate multiple squares, but each square is pushed once per black cell that contains it. So duplicates across different black cells are multiple entries, that's good.
Now one nuance: If the same square appears from multiple black cells, we want to count them together. The sorting method does that.
Thus this approach is simpler and more memory-friendly.
Implementation details for sorting:
vector<pair<int,int>> vec;
vec.reserve(9*N);
After generating, sort(vec.begin(), vec.end());
Then iterate:
int i=0;
while(i<vec.size()){
pair<int,int> cur = vec[i];
int cnt = 0;
while(i<vec.size() && vec[i]==cur){
cnt++; i++;
}
if(cnt>=1 && cnt<=9) ans[cnt]++;
else something? cnt could be >9? Only if more than 9 black cells fall in same 3x3 square, but that's impossible because there are only 9 cells. However, could it be >9 due to duplicates? Actually if there are more than 9 black cells, they'd have to be placed in same 3x3 square, but there are only 9 distinct cells. Since black cells are distinct, at most 9 can be in the same 3x3. So cnt <=9. But still, we can assert or just handle up to 9, but if by some bug cnt>9, we need to cap? Not needed.
Then compute sum1to9 = accumulate ans+1..9.
Compute ans0 = total - sum1to9.
Edge: If N=0, vec empty, ans0 = total, others 0.
Now need to make sure that total can be 0? If H=3, W=3, total =1. It's fine.
Now confirm that we correctly filter squares to only those with top-left in valid range. In generation we already do that.
Now test with sample 1:
We generated squares list? In earlier we had many pushes. After sorting and counting, we will get counts: For each distinct square, count of occurrences = number of black cells inside. Our manual counting gave correct.
Now we must be careful: When we generate for each black cell, we include squares that contain that black cell. So each black cell contributes to each square that contains it. So after pushing all, the count of each square equals the number of black cells inside it. That's correct.
Thus sorting method works.
Now about efficiency: For each black cell we do at most 9 loops, and push to vector. That is O(N). Sorting O(M log M). With M up to 9e5, log M ~ 20, so ~18 million comparisons. Fine.
Now we need to use fast I/O and possibly avoid cin/cout slow? Use scanf? I'll use ios::sync_with_stdio(false); cin.tie(0);.
Now implement.
Pseudo-code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll H, W;
int N;
cin >> H >> W >> N;
vector<pair<int,int>> squares;
squares.reserve(9*N); // optional
for (int i=0; i<N; ++i) {
int a, b;
cin >> a >> b;
for (int dr = -2; dr <= 0; ++dr) {
int r = a + dr;
if (r < 1 || r > H-2) continue;
for (int dc = -2; dc <= 0; ++dc) {
int c = b + dc;
if (c < 1 || c > W-2) continue;
squares.emplace_back(r, c);
}
}
}
sort(squares.begin(), squares.end());
vector<ll> ans(10, 0);
size_t i = 0;
while (i < squares.size()) {
auto cur = squares[i];
int cnt = 0;
while (i < squares.size() && squares[i] == cur) {
++cnt;
++i;
}
if (cnt >= 1 && cnt <= 9) {
ans[cnt]++;
}
}
ll total = (H-2) * (W-2);
ll sum_nonzero = accumulate(ans.begin()+1, ans.end(), 0LL);
ans[0] = total - sum_nonzero;
for (int j = 0; j < 10; ++j) {
cout << ans[j] << '\n';
}
return 0;
}
Now check that we used int for a,b. Could a_i be up to H (1e9) which fits int, ok.
Now verify that r and c comparisons: r > H-2, where H is long long, H-2 is long long. r is int, promoted to long long for comparison. That's fine. But we could also compute max_r = (int)(H-2) if safe. H-2 <= 1e9 fits in int, so we can store int maxR = H-2; but H is long long, cast to int after checking it's safe. But to avoid potential overflow if H is huge beyond int? Not possible. So we can compute int maxR = H-2; but H is long long, need explicit cast: int maxR = H-2; but if H-2 > INT_MAX? H max 1e9, INT_MAX is at least 2e9 on typical, so safe. But to be safe, we can just keep comparisons as is.
Now test with sample 2 using mental? Might be okay.
Now also test with N=0: squares empty, while loop skipped, sum_nonzero=0, ans[0]=total, prints.
Now we should consider if the vector squares could be huge due to N=100k * 9 = 900k, fine.
Now one more nuance: The total squares could be extremely large, up to 10^18, ans[0] can be that large. We must output as integer, use 64-bit.
Now I think solution is correct.
Now but wait, we must also consider that black cells may be such that some squares receive contributions from many black cells, but since max 9, our counting is fine.
Now edge case: If H or W is less than 3? Not allowed.
Now potential issue: In generation, we used loops for dr=-2..0, dc=-2..0. That's correct because top-left corner can be at most 2 rows above and 2 columns left of the black cell. But also top-left could be at same row (dr=0) or one above (dr=-1) or two above (dr=-2). Yes.
Now could there be squares where black cell is at bottom-right corner? That is included with dr=-2? Actually suppose black cell is at (a,b). The top-left row r must satisfy r <= a <= r+2 => r in [a-2, a]. So yes, dr = a - r, so r = a - dr, where dr in {0,1,2}? Actually if we let dr = r - a? I'm using dr as offset from a to r? I used r = a + dr, with dr negative. So dr = r - a. Since r <= a, dr <=0. The possible dr values: 0, -1, -2? Actually r = a, a-1, a-2. So dr = 0, -1, -2. That's what we do.
Now ensure that we only include squares with r >=1 and r <= H-2. For a near bottom, a may be H, then r can be H, H-1, H-2. But r <= H-2, so only r = H-2 works, possibly also H-1? Actually H-1 > H-2 so not allowed. So we only include r=H-2 if a=H? Let's test: a = H (max row). r can be a (H) but H > H-2, so not allowed. r = a-1 = H-1 > H-2, not allowed. r = a-2 = H-2, allowed. So that's correct. Similarly for leftmost column.
Now what about a = 1? r can be 1,0,-1. Only r=1 valid. So works.
Now we also filter c similarly.
Now with sorting method, we must be careful that we use vector of pairs. Since vector of pairs of ints will sort lexicographically.
Now final step: Output each ans on new line.
Now I'll implement.
Potential optimization: Use array of size maybe 9*N for vector, but it's fine.
Now confirm sample outputs.
Now I will code.
But wait, there is a tricky case: The grid is huge, but black cells are few; but it's possible that some 3x3 squares contain black cells but are not counted because we generate only squares that contain at least one black cell. That's correct, because if a square contains no black cells, it won't be generated. So ans[0] is computed as total minus those we counted. That's fine.
Now double-check that we are not missing any squares that have black cells. For any square that contains at least one black cell, that black cell will generate that square (since the black cell is inside the square, the square's top-left will be among the generated offsets). So yes, all squares with at least one black cell will appear in our vector at least once.
Now one more nuance: When we generate squares for a black cell, we push all valid top-left corners. But what if the square is such that the black cell is inside, but the top-left corner is not within [a-2, a] etc? That's the definition, so it must be.
Now everything seems correct.
Now I'll write final solution with sorting.
Now but there is a potential memory/performance issue: For each black cell we generate up to 9 entries, but if N=100k and each black cell is near the center, exactly 9 entries, we will have 900k entries. Sorting 900k pairs is fine.
Now I want to test with a random small case to ensure.
We can write a small test: H=5,W=5,N=1, black at (3,3). Then squares containing it: top-left candidates: (1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)? But H=5 => H-2=3 so r can be 1..3. Similarly c=1..3. So all 9 squares? Actually (3,3) is valid? r=3,c=3 yields rows 3-5, cols 3-5; includes (3,3) yes. So all 9 squares are valid and contain the black cell? Check: If top-left is (1,1), square covers rows1-3, cols1-3, includes (3,3) yes. So all 9 squares contain (3,3). So we will generate 9 entries for that black cell (since all offsets valid). Then after sorting, each distinct square appears once, so ans[1] should be 9. Total squares = (5-2)*(5-2)=9. So ans[0]=0, ans[1]=9. That's correct. Actually if there is only one black cell, each 3x3 square that contains it will have exactly 1 black cell, but does every 3x3 square contain that black cell? In a 5x5 grid, there are 9 possible 3x3 squares. The black cell at (3,3) lies exactly in the center cell; indeed each 3x3 square's rows and columns are consecutive; to contain (3,3), the top-left row must be 1,2,3 and top-left col must be 1,2,3. So yes, all 9 squares contain that cell. So each square has exactly 1 black cell. So ans[1]=9. That seems correct.
Now test another: H=3,W=3,N=1 at (2,2). Total squares =1. Square top-left (1,1) includes (2,2). So ans[1]=1. Works.
Now test edge: H=3,W=4,N=1 at (2,2). Total squares = (1)*(2)=2. Squares: top-left (1,1): rows1-3, cols1-3 includes (2,2); top-left (1,2): rows1-3, cols2-4 includes (2,2). So both contain black cell. So ans[1]=2. That's correct.
Now test scenario where some squares get more than one black cell: Already done.
Now all good.
Now I will produce final answer.
But before final, we need to consider possible integer overflow in computation of total = (H-2)(W-2). Since H and W are up to 1e9, (H-2)(W-2) up to 1e18, which fits in signed 64-bit as argued. But confirm: 1e18 = 1000000000000000000, which is less than 9,223,372,036,854,775,807 (2^63-1). So yes.
Now I'll code.
Implementation details:
- Use
#include <bits/stdc++.h> and using namespace std.
- Use
typedef long long ll;
- Use
vector<pair<int,int>> squares; squares.reserve(9*N);
- Use
for (int i=0; i<N; ++i) { int a,b; cin >> a >> b; ... }
- Use
sort(squares.begin(), squares.end());
- Then count.
But careful: When N is 0, squares.reserve(0) is okay, but we won't have any. The loop for counting should handle empty vector.
Now need to be cautious about squares.size() might be up to 9*N which is up to 900,000, fits in int, but using size_t is fine.
Now I think it's done.
But wait: There is an alternate nuance: Since H and W are up to 1e9, and N up to 1e5, the total squares is huge, but ans[0] might be huge, but we store in long long.
Now test with sample 2 maybe via mental? Not necessary.
Now I will provide final code.
But before final, think if there is any case where the number of squares generated could exceed 9*N? Could there be squares that we generate from a black cell that are not valid due to edges, but we filter. So it's <=9N.
Now I will produce final solution.
But I realize that sorting method requires storing all pairs, while map method requires storing only distinct squares. Sorting method might store duplicates, but that's okay; it may use more memory in terms of number of elements, but still okay (9e5 pairs of ints is ~7.2 MB). Map would store distinct squares only, maybe less. But map overhead per entry may be larger. So both fine.
But sorting method might be faster due to contiguous memory.
Thus I'll implement sorting.
Now I'll code.
After code, test with given examples.
Let's simulate example 1 manually by code.
- Input: H=4, W=5, N=8 with positions.
- squares vector length? For each black cell we generated:
(1,1): generated (1,1) only? Actually a=1,b=1 => r from -1,0,1; only r=1 valid; c from -1,0,1; only c=1 valid => (1,1)
(1,4): a=1,b=4 => r=1; c from 2,3,4 but only c=2,3 valid => (1,2),(1,3)
(1,5): a=1,b=5 => r=1; c from 3,4,5, only c=3 valid => (1,3)
(2,3): a=2,b=3 => r=0,1,2? r=0 invalid, r=1,2 valid; c=1,2,3 valid => (1,1),(1,2),(1,3),(2,1),(2,2),(2,3)
(3,1): a=3,b=1 => r=1,2 valid; c=1 valid => (1,1),(2,1)
(3,2): a=3,b=2 => r=1,2 valid; c=0,1,2? c=0 invalid, c=1,2 valid => (1,1),(1,2),(2,1),(2,2)
(3,4): a=3,b=4 => r=1,2 valid; c=2,3 valid => (1,2),(1,3),(2,2),(2,3)
(4,4): a=4,b=4 => r=2 valid; c=2,3 valid => (2,2),(2,3)
So total pushes: 1 +2 +1 +6 +2 +4 +4 +2 = 22? Actually count: 1,2,1,6,2,4,4,2 sum = 22 pushes. After sorting, we count groups:
Group sizes: (1,1) appears? Count: from list: (1,1) appears from cells: (1,1) (1), (2,3) (1), (3,1) (1), (3,2) (1) => 4. (1,2): from (1,4), (2,3), (3,2), (3,4) => 4. (1,3): from (1,4)? Actually (1,3) from (1,4) (1), (1,5) (1), (2,3) (1), (3,4) (1) => 4. (2,1): from (2,3) (1), (3,1) (1), (3,2) (1) => 3? Actually (2,1) appears in (2,3) generation? Wait (2,3) gave (2,1)? (2,3): r=2,c=1 yields (2,1) yes; (3,1) gave (2,1) as r=2? Actually (3,1) gave (2,1) because r=2 from a-1? For a=3, r options: 1,2; so (2,1) from r=2, c=1; (3,2) gave (2,1) from r=2,c=1? For (3,2) c=1 valid, r=2 valid, yes. So (2,1) appears 3 times. (2,2): from (2,3) (1), (3,2) (1), (3,4) (1), (4,4) (1) => 4? Actually check (3,4) gave (2,2) as r=2,c=2; (4,4) gave (2,2) as r=2,c=2; (2,3) gave (2,2) as r=2,c=2; (3,2) gave (2,2) as r=2,c=2; So that's 4. (2,3): from (2,3) (1), (3,4) (1), (4,4) (1) => 3? Also (??) maybe (1,5) no, (1,4) no. So (2,3) appears 3. So group sizes: (1,1):4, (1,2):4, (1,3):4, (2,1):3, (2,2):4, (2,3):3. That's 6 groups, counts: 4,4,4,3,4,3. So ans[4] = 4? Actually there are 4 groups with count 4? Actually (1,1),(1,2),(1,3),(2,2) are 4 groups with count 4; (2,1) and (2,3) with count 3. So ans[4]=4, ans[3]=2. Good.
So sorting yields correct.
Now final code.
Potential pitfalls: When H or W is 3, H-2 = 1, so rows r from 1 to 1. For a black cell at row 1, r = a + dr: dr = -2 => -1 invalid, dr=-1 =>0 invalid, dr=0 =>1 valid. So it's fine.
Now about reading large input: Use cin. With N up to 1e5, it's fine.
Now finalize.
But wait, there is an alternative nuance: The grid is huge, but we only consider squares that contain at least one black cell. However, what about squares that have black cells but some of those black cells are outside the range of the square? That's not possible. So ok.
Now I will also consider if the number of squares is too large to hold in vector due to memory if N=100k and each yields 9, that's 900k, which is fine.
Now final code as described.
But there is a potential improvement: We can reduce number of entries by using map and only storing distinct squares, but we already decided.
Now I think it's good.
Now I'll produce the final answer.
But wait, could there be a case where a square gets count >9? As argued no.
Now final code.
Now also consider that we use ans vector of long long; ans[0] computed as total - sum_nonzero; ensure total is long long and sum_nonzero is long long.
Now compile.
Now after writing, I'll check for any off-by-one errors in loops: For dr from -2 to 0 inclusive; for dc from -2 to 0 inclusive; that's correct.
Now possible optimization: Instead of nested loops we can compute r_min = max(1, a-2), r_max = min(a, H-2) and similarly for c, then loops. But the nested loops are simple.
Now final code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll H, W;
int N;
cin >> H >> W >> N;
vector<pair<int,int>> squares;
squares.reserve(9 * N);
for (int i = 0; i < N; ++i) {
int a, b;
cin >> a >> b;
for (int dr = -2; dr <= 0; ++dr) {
int r = a + dr;
if (r < 1 || r > H-2) continue;
for (int dc = -2; dc <= 0; ++dc) {
int c = b + dc;
if (c < 1 || c > W-2) continue;
squares.emplace_back(r, c);
}
}
}
sort(squares.begin(), squares.end());
vector<ll> ans(10, 0);
size_t i = 0;
while (i < squares.size()) {
auto cur = squares[i];
int cnt = 0;
while (i < squares.size() && squares[i] == cur) {
++cnt;
++i;
}
if (cnt >= 1 && cnt <= 9) {
ans[cnt]++;
}
}
ll total = (H-2) * (W-2);
ll nonzero_sum = accumulate(ans.begin()+1, ans.end(), 0LL);
ans[0] = total - nonzero_sum;
for (int j = 0; j < 10; ++j) {
cout << ans[j] << '\n';
}
return 0;
}
Now test with sample 1 quickly? Should output.
Now think about potential integer sign: H-2 and W-2 are positive, total positive.
Now about the case when H-2 or W-2 might be zero? If H=3 => H-2=1, positive. Actually if H=3, H-2=1 >0. So total >=1. If H=3 and W=3, total=1.
Now if H=3, W=100, total = 1*(98) = 98.
Now if H=2? Not allowed.
Now all good.
Now there is an alternative: using unordered_map might be faster, but sorting is fine.
Now I'd like to double-check the count of possible squares: The top-left row can range from 1 to H-2 inclusive. So number of rows = H-2. Similarly columns = W-2. So total = (H-2)*(W-2). That's correct.
Now check the handling of large numbers: In loops, we compare r > H-2, where H is long long, but r is int. If H-2 > INT_MAX? As said, H max 1e9, INT_MAX is at least 2,147,483,647, so H-2 <= 1e9 < INT_MAX, so safe. However, on some platforms, int could be 16-bit? No, typical is 32-bit. So fine.
But to be extra safe, we could declare r as long long, but it's fine.
Now final answer.
But wait, there is a nuance: The problem constraints: "0 ≤ N ≤ min(10^5, H×W)". So N can be zero, but also N could be as large as H×W if H×W is less than 1e5. In such case, the grid is small enough that we could maybe process all squares directly. But our approach still works, because even if N=H×W (i.e., all cells are black), then total number of black cells equals number of cells, but N <= 1e5, so H×W ≤ 1e5. So grid is at most 1e5 cells, but H and W might be up to 1e9 individually? But if H×W ≤ 1e5, then H and W cannot both be huge; at least one dimension is small. But our approach still works: we will generate up to 9N squares, N up to 1e5, so still at most 9e5 squares. Sorting is fine. And total squares (H-2)*(W-2) will be at most H×W ≤ 1e5, which is small. So ans[0] may be small. So it's fine.
But what about the case where H and W are large, but N is also large, up to 1e5. That's fine.
Now I think solution is correct.
Now I will produce final answer.
But there is one more nuance: In generation, we use condition r > H-2. But if H-2 could be negative? H>=3, so H-2>=1 positive. So fine.
Now provide final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll H, W;
int N;
cin >> H >> W >> N;
vector<pair<int, int>> squares;
squares.reserve(9 * N);
for (int i = 0; i < N; ++i) {
int a, b;
cin >> a >> b;
// generate all 3x3 squares that contain (a,b)
for (int dr = -2; dr <= 0; ++dr) {
int r = a + dr;
if (r < 1 || r > H - 2) continue;
for (int dc = -2; dc <= 0; ++dc) {
int c = b + dc;
if (c < 1 || c > W - 2) continue;
squares.emplace_back(r, c);
}
}
}
sort(squares.begin(), squares.end());
vector<ll> ans(10, 0);
size_t i = 0;
while (i < squares.size()) {
auto cur = squares[i];
int cnt = 0;
while (i < squares.size() && squares[i] == cur) {
++cnt;
++i;
}
// cnt is the number of black cells in this 3x3 square
if (cnt >= 1 && cnt <= 9) {
ans[cnt]++;
}
}
ll total = (H - 2) * (W - 2);
ll nonzero_sum = accumulate(ans.begin() + 1, ans.end(), 0LL);
ans[0] = total - nonzero_sum;
for (int j = 0; j < 10; ++j) {
cout << ans[j] << '\n';
}
return 0;
}