Print Check

CodeForces - 631B

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.

sol:我们发现如果倒着做修改,每个点最多只会被修改一次,所以可以维护n个链表(每行一个),但是如果有许多列修改并且m=5000时就会被卡到5000*100000,所以对于列记一个bool数组表示是否被访问过,这样复杂度就对了

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,B=;
int n,m,Q;
int Cor[N][N];
int Next[N][N];
bool Tag_Hang[N],Tag_Lie[N];
//n行,n个链表
struct Question
{
int opt,Pos,Cor;
}Que[B];
int main()
{
int i,j;
R(n); R(m); R(Q);
for(i=;i<=n;i++)
{
for(j=;j<=m;j++) Next[i][j]=j+;
}
for(i=;i<=Q;i++)
{
R(Que[i].opt); R(Que[i].Pos); R(Que[i].Cor);
}
for(i=Q;i>=;i--)
{
if(Que[i].opt==)
{
if(Tag_Hang[Que[i].Pos]) continue;
Tag_Hang[Que[i].Pos]=;
for(j=;j<=m;)
{
if(!Cor[Que[i].Pos][j])
{
Cor[Que[i].Pos][j]=Que[i].Cor;
Next[Que[i].Pos][j-]=Next[Que[i].Pos][j];
}
j=Next[Que[i].Pos][j];
}
}
else
{
if(Tag_Lie[Que[i].Pos]) continue;
Tag_Lie[Que[i].Pos]=;
for(j=;j<=n;j++)
{
if(!Cor[j][Que[i].Pos])
{
Cor[j][Que[i].Pos]=Que[i].Cor;
Next[j][Que[i].Pos-]=Next[j][Que[i].Pos];
}
}
}
}
for(i=;i<=n;i++,puts(""))
{
for(j=;j<=m;j++) W(Cor[i][j]);
}
return ;
}
/*
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
*/

codeforces631B的更多相关文章

随机推荐

  1. 错误:“Manifest merger failed with multiple errors, see logs”

    今天用Android Studio打开以前写个的项目后,出现如下错误:Manifest merger failed with multiple errors, see logs 现象是:  遇到这个问 ...

  2. Android学习之基础知识七—碎片的使用

    碎片(Fragment)是一种可以嵌入在活动中的UI片断,它能让程序更加合理和充分地利用大屏幕的空间,它与活动相似,可以简单的理解为一个迷你型的活动,它也有自己的生命周期.碎片在平板的应用非常广泛. ...

  3. Android学习之基础知识四-Activity活动8讲(活动的灵活运用)

    一.判断当前是在哪个活动 1.我们还是接着上一讲的代码,首先创建一个Java类:BaseActivity.java.这个类我们不作为一个活动,也不在AndroidManifest.xml中注册,它只是 ...

  4. GZIP压缩提高网络传输效率

    [spring]通过GZIP压缩提高网络传输效率(可以实现任何资源的gzip压缩.包括AJAX) gzip是http协议中使用的一种加密算法,客户端向web服务器端发出了请求后,通常情况下服务器端会将 ...

  5. python简介及安装配置

    概述 python是解释型语言,相对编译型语言,执行效率较低.python是通过c语言编写,官方解释器也是c语言编写cpython,也有其他的如用java编写的jpython.目前有2.0和3.0版本 ...

  6. Omi框架学习之旅 - 获取DOM节点 及原理说明

    虽然绝大部分情况下,开发者不需要去查找获取DOM,但是还是有需要获取DOM的场景,所以Omi提供了方便获取DOM节点的方式. 这是官网的话,但是我一直都需要获取dom,对dom操作,所以omi提供的获 ...

  7. SkylineGlobe 从v6.1到v6.5 二次开发方面的变化参考

       2.1关于 TerraExplorer v6.5 API 除了一些新的功能,API v6.5不同于API v6.1的最大改进是其对象ID系统.虽然在以前版本的API中,有两个ID系统,一个用于对 ...

  8. tornado学习篇(第二部)

    执行字符串表示的函数,并为该函数提供全局变量 本篇的内容从题目中就可以看出来,就是为之后剖析tornado模板做准备,     #!usr/bin/env python #coding:utf-8 n ...

  9. 扩展 WPF 动画类

    原文:扩展 WPF 动画类 扩展 WPF 动画类                                                                     Charles ...

  10. copy constructor

    copy constructor也分为trivial和nontrivial两种 如果class展现出bitwise copy semantics(按位拷贝语义),则不会构造出 copy constru ...