codeforces631B

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 ≤ n1 ≤ ai ≤ 109), means that row ri is painted in color ai;
  • ci ai (1 ≤ ci ≤ m1 ≤ 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=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0');    return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=5005,B=100005;
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=1;i<=n;i++)
    {
        for(j=0;j<=m;j++) Next[i][j]=j+1;
    }
    for(i=1;i<=Q;i++)
    {
        R(Que[i].opt); R(Que[i].Pos); R(Que[i].Cor);
    }
    for(i=Q;i>=1;i--)
    {
        if(Que[i].opt==1)
        {
            if(Tag_Hang[Que[i].Pos]) continue;
            Tag_Hang[Que[i].Pos]=1;
            for(j=1;j<=m;)
            {
                if(!Cor[Que[i].Pos][j])
                {
                    Cor[Que[i].Pos][j]=Que[i].Cor;
                    Next[Que[i].Pos][j-1]=Next[Que[i].Pos][j];
                }
                j=Next[Que[i].Pos][j];
            }
        }
        else
        {
            if(Tag_Lie[Que[i].Pos]) continue;
            Tag_Lie[Que[i].Pos]=1;
            for(j=1;j<=n;j++)
            {
                if(!Cor[j][Que[i].Pos])
                {
                    Cor[j][Que[i].Pos]=Que[i].Cor;
                    Next[j][Que[i].Pos-1]=Next[j][Que[i].Pos];
                }
            }
        }
    }
    for(i=1;i<=n;i++,puts(""))
    {
        for(j=1;j<=m;j++) W(Cor[i][j]);
    }
    return 0;
}
/*
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
*/
View Code

 

posted @ 2019-03-29 21:38  yccdu  阅读(139)  评论(0编辑  收藏  举报