Codeforces Round #344 (Div. 2)-B. Print Check(贪心+模拟)

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  ≤ 5000n·m ≤ 100 0001 ≤ 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.

Codeforces Round #344 (Div. 2)-B. Print Check(贪心+模拟)_第1张图片



题意:
      给出n行m列,k个操作。
(1) 1 r v 表示第几行全部为v
(2) 2 c v 表示第几列全为v

思路:
    这题根据数据就知道普通暴力方法肯定TLE,所以要写优雅一点点的暴力。因为n,m都小于5000,连二维数组也不能开了。之后很苦恼。突然发现
它给出 n · m  ≤ 100 000,所以果断一维数组做。这里要用贪心,因为最后更新的肯定不会被覆盖掉的,而且时间复杂度只有
O(k+100000)左右。所以我分开行数组与列数组记录还剩下多少个数就OK了,就可以将用过的降到O(1)去查询了。



AC代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
#define QWQ ios::sync_with_stdio(0)
typedef unsigned __int64 LL;
typedef  __int64 ll;
#define CMP bool cmp(const node& a,const node& b){	return a.R

看到别人的代码我才知道我想复杂了,其实只要贪心选出行列就OK了。。。。

cf优秀代码:

#include 
using namespace std;
int arr[5009][5009];
int row[5009];
int col[5009];
int rowval[5009];
int colval[5009];
int main() {
    //freopen("in.txt","r+",stdin);
    int n,m,k,t,r,a;
    scanf("%d%d%d",&n,&m,&k);

    for(int i=1;i<=k;i++){
        scanf("%d%d%d",&t,&r,&a);
        if(t==1){
            row[r]=i;
            rowval[r]=a;
        }else{
            col[r]=i;
            colval[r]=a;
        }
    }

    int ans;
       for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                ans=0;
                //cout<col[j]){
                    ans=rowval[i];
                }else{
                    ans=colval[j];
                }

                if(j!=1)
                    printf(" ");
                printf("%d",ans);
            }
            printf("\n");
       }
    return 0;
}



你可能感兴趣的:(codeforces,codeforces)