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)的点减去了。很方便。
最后readin是O(P), process也是O(P).
```
/*
HackerRank.
You are given an N×NN×N grid. Each cell has the color white (color 0) in the beginning.
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).
Now, given a sequence of PP such operations, calculate the sum of the colors in the final grid.
For simplicity, the colors will be positive integers whose values will be most 109109.
Input Format
The first line of input contains two integers NN and PP separated by a space.
The next PP lines each contain a filling operation. There are two types of filling operations.
ROW I V which means "fill row II with color VV".
COL I V which means "fill column II with color VV".
Output Format
Output one line containing exactly one integer which is the sum of the colors in the final grid.
Constraints
1≤N≤60001≤N≤6000
1≤P≤4000001≤P≤400000
1≤I≤N1≤I≤N
1≤V≤1091≤V≤109
Sample Input
5 4
COL 1 6
COL 4 11
ROW 3 9
COL 1 24
Sample Output
200
Explanation
There are four operations. After the second operation, the grid looks like
6 0 0 11 0
6 0 0 11 0
6 0 0 11 0
6 0 0 11 0
6 0 0 11 0
After the third operation (ROW 3 9), the third row was colored with 9, overriding any previous color in the cells.
6 0 0 11 0
6 0 0 11 0
9 9 9 9 9
6 0 0 11 0
6 0 0 11 0
After the fourth operation (COL 1 24), the grid becomes:
24 0 0 11 0
24 0 0 11 0
24 9 9 9 9
24 0 0 11 0
24 0 0 11 0
The sum of the colors in this grid is 200.
*/
importjava.io.*;
importjava.util.*;
importjava.text.*;
importjava.math.*;
importjava.util.regex.*;
/*
Thoughts:
This is for practice. I didn't run much tests on the code.
Store info into class Cell {int x; boolean isRow; long value}
Save to arraylist. Later need to call list.remove(object)
Use hash map to store the appearance <String, Cell>
process the final data:
keep track of curr single row cell sum = rowSum; also colSum