You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
然后process当下col时候, sum += colValue * N - sumRow. 就等于把交叉所有row(曾经Process过的row)的点减去了。很方便。
9
+
10
+
最后readin是O(P), process也是O(P).
11
+
12
+
13
+
```
14
+
15
+
/*
16
+
HackerRank.
17
+
You are given an N×NN×N grid. Each cell has the color white (color 0) in the beginning.
18
+
19
+
Each row and column has a certain color associated with it. Filling a row or column with a new color VV means changing all the cells of that row or column to VV (thus overriding the previous colors of the cells).
20
+
21
+
Now, given a sequence of PP such operations, calculate the sum of the colors in the final grid.
22
+
23
+
For simplicity, the colors will be positive integers whose values will be most 109109.
24
+
25
+
Input Format
26
+
The first line of input contains two integers NN and PP separated by a space.
27
+
The next PP lines each contain a filling operation. There are two types of filling operations.
28
+
29
+
ROW I V which means "fill row II with color VV".
30
+
COL I V which means "fill column II with color VV".
31
+
Output Format
32
+
Output one line containing exactly one integer which is the sum of the colors in the final grid.
33
+
34
+
Constraints
35
+
1≤N≤60001≤N≤6000
36
+
1≤P≤4000001≤P≤400000
37
+
1≤I≤N1≤I≤N
38
+
1≤V≤1091≤V≤109
39
+
40
+
Sample Input
41
+
42
+
5 4
43
+
COL 1 6
44
+
COL 4 11
45
+
ROW 3 9
46
+
COL 1 24
47
+
Sample Output
48
+
49
+
200
50
+
Explanation
51
+
There are four operations. After the second operation, the grid looks like
52
+
53
+
6 0 0 11 0
54
+
6 0 0 11 0
55
+
6 0 0 11 0
56
+
6 0 0 11 0
57
+
6 0 0 11 0
58
+
After the third operation (ROW 3 9), the third row was colored with 9, overriding any previous color in the cells.
59
+
60
+
6 0 0 11 0
61
+
6 0 0 11 0
62
+
9 9 9 9 9
63
+
6 0 0 11 0
64
+
6 0 0 11 0
65
+
After the fourth operation (COL 1 24), the grid becomes:
66
+
67
+
24 0 0 11 0
68
+
24 0 0 11 0
69
+
24 9 9 9 9
70
+
24 0 0 11 0
71
+
24 0 0 11 0
72
+
The sum of the colors in this grid is 200.
73
+
74
+
*/
75
+
76
+
importjava.io.*;
77
+
importjava.util.*;
78
+
importjava.text.*;
79
+
importjava.math.*;
80
+
importjava.util.regex.*;
81
+
/*
82
+
Thoughts:
83
+
This is for practice. I didn't run much tests on the code.
84
+
Store info into class Cell {int x; boolean isRow; long value}
85
+
Save to arraylist. Later need to call list.remove(object)
86
+
Use hash map to store the appearance <String, Cell>
87
+
process the final data:
88
+
keep track of curr single row cell sum = rowSum; also colSum
0 commit comments