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. date日期 格式化

    这个是别人写的,我拿过来用的,哈哈 Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth() ...

  2. 数据库隔离级别深入理解(ORACLE)

    TRANSACTION_READ_UNCOMMITTED 1 这种隔离级别最低,脏读,不可重复读,幻读都会发生,我用的oracle,并没有支持这个级别,不作研究. TRANSACTION_READ_C ...

  3. Attention[Content]

    0. 引言 神经网络中的注意机制就是参考人类的视觉注意机制原理.即人眼在聚焦视野区域中某个小区域时,会投入更多的注意力到这个区域,即以"高分辨率"聚焦于图像的某个区域,同时以&qu ...

  4. 随机指定范围内N个不重复的数

    此为工具类,支持抽奖业务需求,具体实现见下方代码: package com.org.test; import java.util.ArrayList; import java.util.List; p ...

  5. 同一个解决方案或有依赖关系的两个项目引用同名但不同版本的DLL

    问题描述 我们最近在使用Redis作Session的集中化,中间碰到了一个如下问题:我们有一些项目比较老,引用了NewtonJson的4.0.3.0版本的DLL,但是Redis提供的C#集成DLL引用 ...

  6. bitset常用用法&&简单题分析

    Preface bitset,还是一个比较好用的STL,可以给一些题目做到神奇的常数优化(\(O(\frac{原来的复杂度}{机器的位数(32位or64位)})\)) 关于一些具体的函数等内容可以参考 ...

  7. [JSOI2016]轻重路径[树链剖分]

    题意 题目链接 分析 先对原树树剖,在一次删点操作后从根节点开始二分,如果一条边从重边变成轻边,必然有 \(size_u\le \frac{1}{2}size_{rt}\) (取等号是特判对应儿子消失 ...

  8. REST-framework快速构建API--源码解析

    一.APIView 通过APIView实现API的过程如下: urls.py url(r'^books/$', views.BookView.as_view(),name="books&qu ...

  9. Centos下SFTP双机高可用环境部署记录

    SFTP(SSH File Transfer Protocol),安全文件传送协议.有时也被称作 Secure File Transfer Protocol 或 SFTP.它和SCP的区别是它允许用户 ...

  10. 龟速机器学习总结----day1

    机器学习主要工作大致分为以下几步,数据预处理,包括数据切分,特征选取,数据缺失值处理,来了解数据.接下来分割数据,分别分出训练集和测试集.第三步,选择模型,使用训练数据训练模型参数,再对测试数据进行预 ...