B. Print Check
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Kris works in a large company "Blake Technologies". As a best engineer of the company he was assigned a task to develop a printer that will be able to print horizontal and vertical strips. First prototype is already built and Kris wants to tests it. He wants you to implement the program that checks the result of the printing.

Printer works with a rectangular sheet of paper of size n × m. Consider the list as a table consisting of n rows and m columns. Rows are numbered from top to bottom with integers from 1 to n, while columns are numbered from left to right with integers from 1 to m. Initially, all cells are painted in color 0.

Your program has to support two operations:

  1. Paint all cells in row ri in color ai;
  2. Paint all cells in column ci in color ai.

If during some operation i there is a cell that have already been painted, the color of this cell also changes to ai.

Your program has to print the resulting table after k operation.

Input

The first line of the input contains three integers nm and k (1  ≤  n,  m  ≤ 5000, n·m ≤ 100 000, 1 ≤ k ≤ 100 000) — the dimensions of the sheet and the number of operations, respectively.

Each of the next k lines contains the description of exactly one query:

  • ri ai (1 ≤ ri ≤ n, 1 ≤ ai ≤ 109), means that row ri is painted in color ai;
  • ci ai (1 ≤ ci ≤ m, 1 ≤ ai ≤ 109), means that column ci is painted in color ai.
Output

Print n lines containing m integers each — the resulting table after all operations are applied.

Examples
input
  1. 3 3 3
    1 1 3
    2 2 1
    1 2 2
output
  1. 3 1 3
    2 2 2
    0 1 0
input
  1. 5 3 5
    1 1 1
    1 3 1
    1 5 1
    2 1 1
    2 3 1
output
  1. 1 1 1
    1 0 1
    1 1 1
    1 0 1
    1 1 1
Note

The figure below shows all three operations for the first sample step by step. The cells that were painted on the corresponding step are marked gray.

题目要求你根据操作给矩阵染色,关键就是对于同一行(同一列)之后的操作会覆盖之前的操作,所以逆序染色标记一下,前边的就不用染了。

  1. package codeforces344;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.util.StringTokenizer;
  8.  
  9. /**
  10. * Created by lenovo on 2016-03-10.
  11. *
  12. */
  13. public class B344 {
  14.  
  15. BufferedReader br;
  16. PrintWriter out;
  17. StringTokenizer st;
  18. boolean eof;
  19. B344() throws IOException {
  20. br = new BufferedReader(new InputStreamReader(System.in));
  21. out = new PrintWriter(System.out);
  22. solve();
  23. out.close();
  24. br.close();
  25. }
  26. public static void main(String[] args) throws IOException{
  27. new B344();
  28. }
  29.  
  30. /*
  31. * solve method
  32. * */
  33. /*
  34. * 来个逆向,就可以少对很多单元进行赋值,就只需要简单判断一下就好
  35. * 因为多个操作可能对应于同一行或者是同一列,所以,之后的操作肯定会覆盖之前的操作,
  36. * 所以就把那些无效的操作都筛掉。
  37. * */
  38. int[][] graph = new int[5000][5000];
  39. void solve() throws IOException {
  40. int n, m, k;
  41. n = nextInt();
  42. m = nextInt();
  43. k = nextInt();
  44. int[] op = new int[k + 10];
  45. int[] rc = new int[k + 10];
  46. int[] color = new int[k + 10];
  47. int[] fr = new int[5000 + 10];
  48. int[] fc = new int[5000 + 10];
  49.  
  50. for(int i = 0; i < k; ++i) {
  51. op[i] = nextInt();
  52. rc[i] = nextInt();
  53. rc[i] -= 1;
  54. color[i] = nextInt();
  55. }
  56.  
  57. for(int i = k-1; i>= 0; --i) {
  58.  
  59. if(op[i] == 1) {
  60. if(fr[rc[i]] == 0){
  61. fr[rc[i]] = 1;
  62. for(int j = 0; j < m; ++j){
  63. if(graph[rc[i]][j] == 0)
  64. graph[rc[i]][j] = color[i];
  65. }
  66. }
  67.  
  68. } else {
  69. if(fc[rc[i]] == 0){
  70. fc[rc[i]] = 1;
  71.  
  72. for(int j = 0; j < n; ++j){
  73. if(graph[j][rc[i]] == 0)
  74. graph[j][rc[i]] = color[i];
  75. }
  76. }
  77. }
  78. }
  79. print(n, m);
  80. }
  81.  
  82. void print(int n, int m){
  83. for(int i = 0; i < n; ++i) {
  84. for(int j = 0; j < m; ++j) {
  85. System.out.print(graph[i][j] + " ");
  86. }
  87. System.out.println();
  88. }
  89. }
  90.  
  91. /*
  92. * 优化的流
  93. * */
  94. String nextToken(){
  95. while(st == null || !st.hasMoreTokens()){
  96. try{
  97. st = new StringTokenizer(br.readLine());
  98. } catch(IOException e) {
  99. eof = true;
  100. return null;
  101. }
  102. }
  103. return st.nextToken();
  104. }
  105.  
  106. String nextString(){
  107. try{
  108. return br.readLine();
  109. } catch (Exception e) {
  110. eof = true;
  111. return null;
  112. }
  113. }
  114.  
  115. int nextInt() throws IOException {
  116. return Integer.parseInt(nextToken());
  117. }
  118.  
  119. long nextLong() throws IOException {
  120. return Long.parseLong(nextToken());
  121. }
  122.  
  123. double nextDouble() throws IOException {
  124. return Double.parseDouble(nextToken());
  125. }
  126. }

  

Codeforces Round #344 (Div. 2) B. Print Check的更多相关文章

  1. Codeforces Round #344 (Div. 2) B. Print Check 水题

    B. Print Check 题目连接: http://www.codeforces.com/contest/631/problem/B Description Kris works in a lar ...

  2. Codeforces Round #344 (Div. 2) 631 B. Print Check (实现)

    B. Print Check time limit per test1 second memory limit per test256 megabytes inputstandard input ou ...

  3. Codeforces Round #344 (Div. 2)

    水 A - Interview 注意是或不是异或 #include <bits/stdc++.h> int a[1005], b[1005]; int main() { int n; sc ...

  4. Codeforces Round #344 (Div. 2) B

    B. Print Check time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Codeforces Round #344 (Div. 2) A

    A. Interview time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  6. Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳

    E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...

  7. Codeforces Round #344 (Div. 2) D. Messenger kmp

    D. Messenger 题目连接: http://www.codeforces.com/contest/631/problem/D Description Each employee of the ...

  8. Codeforces Round #344 (Div. 2) C. Report 其他

    C. Report 题目连接: http://www.codeforces.com/contest/631/problem/C Description Each month Blake gets th ...

  9. Codeforces Round #344 (Div. 2) A. Interview 水题

    A. Interview 题目连接: http://www.codeforces.com/contest/631/problem/A Description Blake is a CEO of a l ...

随机推荐

  1. iOS9网络适配(ATS)

    [转]iOS9 new_001:iOS9网络适配(ATS) 下载Xcode7打开APP后大家都发现自己的APP无法联网了,why? 苹果官方文档介绍如下: App Transport Security ...

  2. C++程序设计基础

    01 1 预编译常用的有,宏定义和包含库.2 库:是实用工具的集和,由程序员编写,可以完成一些特定的功能.3 <> 系统库 ""用户自定义库.4 宏定义:定义符号常量, ...

  3. 如何灵活运用Linux 进程资源监控和进程限制

    导读 每个 Linux 系统管理员都应该知道如何验证硬件.资源和主要进程的完整性和可用性.另外,基于每个用户设置资源限制也是其中一项必备技能. 在这篇文章中,我们会介绍一些能够确保系统硬件和软件正常工 ...

  4. python print及格式化

    print(value,sep=' ',end='\n',file=sys.stdout, flush=False) sep=' '默认空格 print('hello','world') #hello ...

  5. HTTP Header 详解

    HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应.就整个网络资源传 ...

  6. 使用winshark分析三次握手,四次挥手

    三次握手 ip 106.120.167.67捕获的数据 数据信息   分析: 从图中可以看出,前三条为三次握手过程,使用TCP协议. 结合图,第一条为建立连接请求,客户端向服务器发送SYN=1的报文, ...

  7. Python 开源网上商城项目

    django-oscar  https://github.com/django-oscar/django-oscar#screenshots django-shop  https://github.c ...

  8. mysql 命令导入导出

    导出 mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -u root -p dataname >xxx.sql 导入 mysql>source ...

  9. $compile

    <html ng-app="compile"> <head> <script src="http://apps.bdimg.com/libs ...

  10. cacti 安装

    cacti:是常用的一个监控软件(开源,免费) 特点:重图形,有数据历史,需要用到数据库的支持,支持web配置,默认不支持告警,可以加插件 cacti安装 1.安装扩展源epel (nagios 和z ...