2

Problem: We have given a nxm matrix, initially initialized with 0. We have to perform k queries: Each query supports one of the two operations.

  1. paint all elements in ri row with the colour ai .

  2. paint all elements in ci column with the colour ai.

    The same element can be painted more than once. But the color of that element is the same as the last painted colour for that element. You have to print the final matrix after painting.

Input: The first line contains three space-separated integers N,M,K
Next K lines consist of exactly one typep of operation to be performed
1) 1 ri ai means row ri is painted with color ai
2) 2 ci ai means column ci is painted with color ai

Output: Print the final matrix of size nxm after painting.

Sample Input:
3 3 3
1 1 3
2 2 1
1 2 2

Output:
3 1 3
2 2 2
0 1 0


I have written the following code to solve it but it is showing TLE for some test cases. Can you give me some idea how to solve it in efficient way?
My Code

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int mat[5000][5000];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,m,k,q,r,c,val,i,j,re;
    cin>>n>>m>>re;
    while(re--)
    {
        cin>>q;
        if(q==1)
        {
            cin>>r;
            cin>>val;
            i=r-1;
            for(j=0,k=m-1;j<=k;j++,k--)
            {
                mat[i][j]=val;
                mat[i][k]=val;
            }
        }
        else if(q==2)
        {
            cin>>c>>val;
            j=c-1;
            for(i=0,k=n-1;i<=k;i++,k--)
            {
                mat[i][j]=val;
                mat[k][j]=val;

            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            cout<<mat[i][j]<<" ";
        }
        cout<<endl;
    }
}
  • Why do you divide the loop this way with two paint operations? Is this a cheap attempt at unrolling? – ypnos Jun 16 '20 at 10:53
  • If you are new to C++ I would suggest reading: [Why using namespace std is considered bad practise](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), it is pretty interesting! – darclander Jun 16 '20 at 13:00

2 Answers2

2

It is only needed to memorize the last color that was affected to a given row or a given column, and the last time at which it was performed.

Then, for a given element mat[i][j], we simply have to check if the last modification on row i occured before of after the last modification on column j.

We don't even need to set such a matrix.

#include    <iostream>
#include    <ios>
#include    <vector>

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);

    int n, m, re;
    std::cin >> n >> m >> re;
    std::vector<int> row_color (n, 0), row_date (n, -1);
    std::vector<int> col_color (m, 0), col_date (m, -1);

    int date = 0;
    while (re--) {
        int q, index, val;
        std::cin >> q >> index >> val;
        index--;
        if (q == 1) {
            row_color[index] = val;
            row_date[index] = date;
        } else {
            col_color[index] = val;
            col_date[index] = date;
        }
        ++date;
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            int val = (row_date[i] > col_date[j]) ? row_color[i] : col_color[j];
            std::cout << val << " ";
        }
        std::cout << "\n";
    }
}
Damien
  • 4,809
  • 4
  • 15
  • 20
1

Instead of performing all the paint operations as they come in, you could:

  1. While parsing the input, keep and update:

    • For each row the last color ai it is supposed to be painted in and the corresponding value k (running from 0 to K)
    • The same for each column
  2. Setup an array of paint operations that combines both row and column paintings for all rows, columns where a painting occured

  3. Sort the array based on k
  4. Perform these operations on a matrix initialized with 0

This algorithm has an advantage if there is a large k (so, lots of overpainting) which you could expect from these kind of problems.

ypnos
  • 50,202
  • 14
  • 95
  • 141