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. ESP32 I2S

    I2S支持DMA; I2S可以直接利用DAC来输出模拟信号 (GPIO 25 & GPIO 26): 高精度时钟使能参数:   .use_apll = true ESP32配置外设一般都是配置 ...

  2. 【Codeforces Round 1114】Codeforces #538 (Div. 2)

    Codeforces Round 1114 这场比赛做了\(A\).\(C\).\(D\).\(E\),排名\(134\). \(B\)题做了很长时间,好不容易最后一分钟\(Pretest\ Pass ...

  3. WireShark抓包工具使用

    WireShark是一款网络封包分析软件,它抓取网络封包,并尽可能显示出最详细的封包资料. wireshark的准备工作 安装wireshark sudo apt-get install wiresh ...

  4. 每个大主播都是满屏弹幕,怎么做到的?Python实战无限刷弹幕!

    anmu 是一个开源的直播平台弹幕接口,使用他没什么基础的你也可以轻松的操作各平台弹幕.使用不到三十行代码,你就可以使用Python基于弹幕进一步开发.支持斗鱼.熊猫.战旗.全民.Bilibili多平 ...

  5. Java使用数字证书加密通信(加解密/加签验签)

    本文中使用的Base64Utils.java可参考:http://www.cnblogs.com/shindo/p/6346618.html 证书制作方法可参考:http://www.cnblogs. ...

  6. SQL Server数据库(时间戳timestamp)类型 (转载)

    timestamp介绍 公开数据库中自动生成的唯一二进制数字的数据类型. timestamp 通常用作给表行加版本戳的机制. 存储大小为 8 个字节. 不可为空的 timestamp 列在语义上等价于 ...

  7. Spring Boot 之 Profile 使用

    Spring Boot 之 Profile 使用 一个应用为了在不同的环境下工作,常常会有不同的配置,代码逻辑处理.Spring Boot 对此提供了简便的支持. 关键词: @Profile.spri ...

  8. python---pandas.merge使用

    merge 函数参数 ”’ merge: 合并数据集, 通过left, right确定连接字段,默认是两个数据集相同的字段 参数 说明 left 参与合并的左侧DataFrame right 参与合并 ...

  9. Vue-初步了解vue-router的三要素:路由map 、路由视图、路由导航

    安装vue-router模块 使用vue-router前要先安装vue-router库 cnpm install vue-router –save 使用vue-router vue-router有三个 ...

  10. iOS开发简记(2):自定义tabbar

    tabbar是放在APP底部的控件.常见的APP都使用tabbar来进行功能分类的管理,比如微信.QQ等等. 小程需要一个特殊一点的tabbar,要求突显中间的那个按钮,让中间按钮特别显眼,从而引导用 ...