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.
paint all elements in ri row with the colour ai .
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;
}
}