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
3 3 3
1 1 3
2 2 1
1 2 2
output
3 1 3 
2 2 2
0 1 0
input
5 3 5
1 1 1
1 3 1
1 5 1
2 1 1
2 3 1
output
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.

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

package codeforces344;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer; /**
* Created by lenovo on 2016-03-10.
*
*/
public class B344 { BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
B344() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
br.close();
}
public static void main(String[] args) throws IOException{
new B344();
} /*
* solve method
* */
/*
* 来个逆向,就可以少对很多单元进行赋值,就只需要简单判断一下就好
* 因为多个操作可能对应于同一行或者是同一列,所以,之后的操作肯定会覆盖之前的操作,
* 所以就把那些无效的操作都筛掉。
* */
int[][] graph = new int[5000][5000];
void solve() throws IOException {
int n, m, k;
n = nextInt();
m = nextInt();
k = nextInt();
int[] op = new int[k + 10];
int[] rc = new int[k + 10];
int[] color = new int[k + 10];
int[] fr = new int[5000 + 10];
int[] fc = new int[5000 + 10]; for(int i = 0; i < k; ++i) {
op[i] = nextInt();
rc[i] = nextInt();
rc[i] -= 1;
color[i] = nextInt();
} for(int i = k-1; i>= 0; --i) { if(op[i] == 1) {
if(fr[rc[i]] == 0){
fr[rc[i]] = 1;
for(int j = 0; j < m; ++j){
if(graph[rc[i]][j] == 0)
graph[rc[i]][j] = color[i];
}
} } else {
if(fc[rc[i]] == 0){
fc[rc[i]] = 1; for(int j = 0; j < n; ++j){
if(graph[j][rc[i]] == 0)
graph[j][rc[i]] = color[i];
}
}
}
}
print(n, m);
} void print(int n, int m){
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
System.out.print(graph[i][j] + " ");
}
System.out.println();
}
} /*
* 优化的流
* */
String nextToken(){
while(st == null || !st.hasMoreTokens()){
try{
st = new StringTokenizer(br.readLine());
} catch(IOException e) {
eof = true;
return null;
}
}
return st.nextToken();
} String nextString(){
try{
return br.readLine();
} catch (Exception e) {
eof = true;
return null;
}
} int nextInt() throws IOException {
return Integer.parseInt(nextToken());
} long nextLong() throws IOException {
return Long.parseLong(nextToken());
} double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
}

  

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. [Python] from scipy import sparse 报 DLL load failed:找不到指定模块错误

    依赖vc运行环境.需要安装 vc_redist Py3.5要安装2015版 传送门: https://www.microsoft.com/zh-CN/download/details.aspx?id= ...

  2. 百度API城市代码CityCode官方文档

    100 拉萨市101 那曲地区102 日喀则地区103 阿里地区104 昆明市105 楚雄彝族自治州106 玉溪市107 红河哈尼族彝族自治州108 普洱市109 西双版纳傣族自治州110 临沧市11 ...

  3. [BZOJ3223]Tyvj 1729 文艺平衡树

    [BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...

  4. powershell通过wps excel导出csv

    powershell比较强大,比较好用,比较方便. $et=New-Object -ComObject et.application #$et.Visible=$true $et.DisplayAle ...

  5. erlang 虚机CPU 占用高排查

    -问题起因 近期线上一组服务中,个别节点服务器CPU使用率很低,只有其他1/4.排除业务不均,曾怀疑是系统top统计错误,从Erlang调度器的利用率调查 找到通过erlang:statistics( ...

  6. Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  7. Selenium Xpath Tutorials - Identifying xpath for element with examples to use in selenium

    Xpath in selenium is close to must required. XPath is element locator and you need to provide xpath ...

  8. windows 安装 mongodb

    windows 安装 mongodb 下载 首先到官网下载合适的安装包,下载地址为 https://www.mongodb.com/download-center MongoDB for Window ...

  9. xcode 8 重新支持插件

    苹果出了Xcode8之后,就加了签名让之前的自定义插件无法继续的安装使用.想要重新使用插件的话只要用自己的签名覆盖苹果的签名即可. 1.创建自签名证书 钥匙串->钥匙串访问->证书助理-& ...

  10. MVC Code First 当实体类发生变化时,如何自动更新数据库表

    下面做一个例子,Category是用户新建的一个实体类,然后添加一个字段,然后让数据库中的Category表也添加一个字段 1.Category.cs