1
+ /*
2
+ * Author : Tashin.Parvez
3
+ * United International University
4
+ * Created: 17.09.2024
5
+ */
6
+
7
+ #include < bits/stdc++.h>
8
+ #define faster ios_base::sync_with_stdio (false ); cin.tie(0 ); cout.tie(0 );
9
+ #define CRACKED return 0 ;
10
+ #define nl " \n "
11
+ using namespace std ;
12
+ #define int long long
13
+ #define ll long long
14
+ #define ull unsigned long long
15
+ #define ld long double
16
+ #define output (x ) cout << x << nl
17
+ #define setdec (x ) fixed << setprecision(x)
18
+ #define YES cout << " YES" << nl;
19
+ #define Yes cout << " Yes" << nl;
20
+ #define NO cout << " NO" << nl;
21
+ #define No cout << " No" << nl;
22
+
23
+ // --------------------------------- Debug --------------------------------
24
+ #define tashin cout << " ____Tashin____" << nl;
25
+ #define parvez cout << " ____Parvez____" << nl;
26
+
27
+ #define dbg (...) __f(#__VA_ARGS__, __VA_ARGS__)
28
+ template <typename Arg1> void __f (const char *name, Arg1 &&arg1) { cout << name << " = " << arg1 << std::endl; }
29
+ template <typename Arg1, typename ... Args> void __f (const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr (names + 1 , ' ,' ); cout.write (names, comma - names) << " = " << arg1 << " | " ; __f (comma + 1 , args...); }
30
+
31
+ // --------------------------------- FOR --------------------------------
32
+ #define FOR_OVERLOAD (_1, _2, _3, NAME, ...) NAME
33
+ #define FOR (...) FOR_OVERLOAD(__VA_ARGS__, FOR_THREE_ARGS, FOR_TWO_ARGS, FOR_ONE_ARG)(__VA_ARGS__)
34
+ #define FOR_ONE_ARG (n ) for (int i = 0 ; i < (n); i++)
35
+ #define FOR_TWO_ARGS (a, b ) for (int i = (a); (a) <= (b) ? (i < (b)) : (i > (b)); (a) <= (b) ? ++i : --i)
36
+ #define FOR_THREE_ARGS (a, b, c ) for (int i = (a); (c) > 0 ? (i < (b)) : (i > (b)); i += (c))
37
+ #define FORJ (n ) for (int j = 0 ; j < (n); j++)
38
+ #define FORK (n ) for (int k = 0 ; k < (n); k++)
39
+
40
+ // -------------------------------- Vector -------------------------------
41
+ #define vi vector<int >
42
+ #define vc vector<char >
43
+ #define vs vector<string>
44
+ #define vb vector<bool >
45
+ #define vpii vector<pair<int , int >>
46
+ #define vpsi vector<pair<string, int >>
47
+
48
+ #define pb push_back
49
+ #define ppb pop_back
50
+ #define vmin (a ) (*min_element (a.begin(), a.end()))
51
+ #define vmax (a ) (*max_element (a.begin(), a.end()))
52
+ #define vsum (a ) accumulate(a.begin(), a.end(), 0LL )
53
+
54
+ // -------------------------------- Sort -------------------------------
55
+ #define all (a ) (a).begin(),(a).end()
56
+ #define vsort (v ) sort(v.begin(), v.end())
57
+ #define vsortrev (v ) sort(v.begin(), v.end(), greater<>())
58
+ #define arrsort (a, n ) sort(a, a + n)
59
+ #define arrsortrev (a, n ) sort(a, a + n, greater<>())
60
+
61
+ // -------------------------------- pair -------------------------------
62
+ typedef pair<int , int > pii;
63
+ typedef pair<int , string> pis;
64
+ typedef pair<string, int > psi;
65
+ #define mp make_pair
66
+ #define ff first
67
+ #define ss second
68
+
69
+ // -------------------------------- Map -------------------------------
70
+ #define mpii map<int , int >
71
+ #define mpsi map<string, int >
72
+ #define mpci map<char , int >
73
+
74
+ #define mptov (m, v ) copy(m.begin(), m.end(), back_inserter(v))
75
+ #define vtomp (v, m ) copy(v.begin(), v.end(), inserter(m, m.begin()))
76
+
77
+ // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ Some Func $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
78
+
79
+ template <typename T> int len (const T &x) { return x.size (); }
80
+ #define mem (a, b ) memset (a, b, sizeof (a))
81
+
82
+ // ------------------------------ NumberTheory ------------------------------
83
+
84
+ #define lcm (a, b ) (a * (b / __gcd(a,b)) )
85
+ #define gcd (a, b ) __gcd(a,b)
86
+
87
+ vector<int > sieve (int n) { const int isprimeflag_limit = 1e8 ; static bitset<isprimeflag_limit> isprimeflag; vector<int > primeNumbers; if (n == 1 || n <= 0 ) return primeNumbers; for (int i = 3 ; i <= n; i += 2 ) isprimeflag[i] = 1 ; isprimeflag[2 ]=1 ; int sq = sqrt (n); for (int i = 3 ; i <= sq; i += 2 ) if (isprimeflag[i]) for (int j = i * i; j <= n; j += i) isprimeflag[j] = 0 ; primeNumbers.push_back (2 ); for (int i = 3 ; i <= n; i += 2 ) if (isprimeflag[i]) primeNumbers.push_back (i); return primeNumbers; }
88
+ vector<int > getprimefac (int n, vector<int > &primeNumbers) { vector<int > factors; for (auto i : primeNumbers) { if (i * i > n) break ; while (n % i == 0 ) factors.push_back (i), n /= i; } if (n > 1 ) factors.push_back (n); return factors; }
89
+
90
+ void getDivisorsAll (int limit) { const int idxfordivisor = 1e7 ; vector<int > divisorsCnt (idxfordivisor + 2 , 0 ), divisorsSum (idxfordivisor + 2 , 0 ); vector<vector<int >> alldivisors (idxfordivisor + 2 ); divisorsCnt.resize (limit + 1 , 0 ); divisorsSum.resize (limit + 1 , 0 ); for (int i = 1 ; i <= limit; i++) for (int j = i; j <= limit; j += i) divisorsCnt[j]++, divisorsSum[j] += i, alldivisors[j].push_back (i); /* return alldivisors;*/ }
91
+ vector<int > getDivisors (int n) { vector<int > divisors; for (int i = 1 ; i * i <= n; i++) if (n % i == 0 ) { divisors.push_back (i); if (n / i != i) divisors.push_back (n / i); } return divisors; }
92
+ int NOD (int n, vector<int > primeNumbers) { int nod = 1 ; for (auto i : primeNumbers) { if (i * i > n) break ; if (n % i == 0 ) { int cnt = 1 ; while (n % i == 0 ) { n /= i; cnt++; } nod *= cnt; } } if (n > 1 ) nod *= 2 ; return nod; }
93
+ int SNOD (int n) { int sum = 0 , sq = sqrt (n); for (int i = 1 ; i <= sq; i++) sum += (n/i - i) * 2 ; return sum + sq; }
94
+ int SOD (int n, vector<int >& primeNumbers) { int sod = 1 ; for (auto i : primeNumbers) { if (i * i > n) break ; if (n % i == 0 ) { int isum = 1 , icntSum = 1 ; while (n % i == 0 ) icntSum *= i, isum += icntSum, n /= i; sod *= isum; } } if (n > 1 ) sod *= (n + 1 ); return sod; }
95
+ int eulerPhi (int n, vector<int > &primeNumbers) { int phi = n; for (auto i : primeNumbers) { if (i * i > n) break ; if (n % i == 0 ) { while (n % i == 0 ) n /= i; phi /= i; phi *= (i - 1 ); } } if (n > 1 ) { phi /= n; phi *= (n - 1 ); } return phi; }
96
+
97
+ // ------------------------------- Int func's -------------------------------
98
+
99
+ #define CEIL (a, b ) (a+b-1 )/b
100
+ #define FLOOR (a, b ) a/b
101
+ #define min3 (a, b, c ) min(min(a, b), c)
102
+ #define mid3 (a, b, c ) (a + b + c) - max3(a, b, c) - min3(a, b, c)
103
+ #define max3 (a, b, c ) max(max(a, b), c)
104
+ #define MID (a, b ) a + ((b - a) / 2 );
105
+ #define suminrange (a, b ) ((b * (b + 1 )) / 2 ) - (((a - 1 ) * (a)) / 2 )
106
+
107
+ ll numrev (ll n) { ll tmp=n,ans=0 ,r;while (tmp){r=tmp%10 ;ans=ans*10 +r;tmp/=10 ;}return ans;}
108
+ bool isprime (ll n) {if (n<2 )return false ;if (n==2 )return true ;if (n%2 ==0 )return false ;for (ll i=3 ;i<=sqrt (n);i+=2 ){if (n%i==0 )return false ;}return true ;}
109
+ bool issquare (ll x) {ll sq=sqrt (x);return sq*sq==x;}
110
+ bool iseven (int n) { return !(n & 1 );}
111
+ ll POW (ll a, ll b) {if (!b) return 1 ;ll r=POW (a,b/2 );if (b%2 ) return r*r*a;else return r*r;}
112
+
113
+ // ------------------------------- string func's -------------------------------
114
+
115
+ ll strtoint (string s) {istringstream ss (s);ll n;ss>>n;return n;}
116
+ string inttostr (ll x) {string s;while (x){s+=(char )(x%10 )+' 0' ;x/=10 ;}reverse (all (s));return s;}
117
+
118
+ #define strtolower (s ) transform(s.begin(), s.end(), s.begin(), ::tolower)
119
+ #define strrev (s ) reverse(s.begin(), s.end())
120
+ int getASCII (char c) { return c;}
121
+
122
+ void arrprint (int arr[], int len) { for (int i = 0 ; i < len; i++) (i + 1 == len) ? cout << arr[i] << nl : cout << arr[i] << " " ;}
123
+ void vprint (const vector<int >& vec) { for (size_t i = 0 ; i < vec.size (); ++i) { if (i + 1 == vec.size ()) cout << vec[i] << nl; else cout << vec[i] << " " ; } }
124
+
125
+ /* --------------------- REMEMBER --------------------
126
+ int = -2e31 to 2e31 -1 2e31 = 2*10^9 max Digit = 10
127
+ long long = -2e63 to 2e63 -1 2e63 = 9*10^18 max Digit = 19
128
+ Max size of global array can upto 10e8
129
+
130
+ 1. Think Greedy
131
+ 2. Think Brute Force
132
+ 3. Think solution in reverse order
133
+ 4. Think DP [ check constraints carefully ]
134
+ 5. Check base cases for DP and prove solution for Greedy
135
+ 6. Think Graph
136
+ 7. SubArray / Continious / SubSegment = PrefixSum
137
+
138
+ */
139
+ const double PI = 3.1415926535 ;
140
+ const int inf = 1e18 ;
141
+ const int mod = 1000000007 ;
142
+
143
+ bool cmp (const pii &a, const pii &b) { return a.first > b.first ; }
144
+
145
+ void solution () // main solution
146
+ {
147
+ int a, b, c, d;
148
+ int i, j, k, m, n, q;
149
+ int u, v, x, y, z;
150
+ int l, r;
151
+ int even = 0 , odd = 0 ;
152
+
153
+ string s; char chr;
154
+ bool flag = false ;
155
+
156
+ int ans, cnt = 0 , idx = -1 , sum = 0 , product = 1 ;
157
+ int mn = INT_MAX, mx = INT_MIN;
158
+
159
+ cin >> r >> c;
160
+ int arr[r][c];
161
+
162
+ FOR (r)
163
+ {
164
+ FORJ (c)
165
+ {
166
+ cin >> arr[i][j];
167
+ }
168
+ }
169
+
170
+ FOR (r)
171
+ {
172
+ FORJ (c)
173
+ {
174
+ cnt = 0 ;
175
+ int tempcnt = 0 ;
176
+ mn = INT_MIN;
177
+ if (i - 1 >= 0 )
178
+ {
179
+ if (arr[i][j] > arr[i - 1 ][j])
180
+ {
181
+ cnt++;
182
+ mn = max (mn, arr[i - 1 ][j]);
183
+ }
184
+ tempcnt++;
185
+ }
186
+ if (i + 1 < r)
187
+ {
188
+ if (arr[i][j] > arr[i + 1 ][j])
189
+ {
190
+ cnt++;
191
+ mn = max (mn, arr[i + 1 ][j]);
192
+ }
193
+ tempcnt++;
194
+ }
195
+ if (j - 1 >= 0 )
196
+ {
197
+ if (arr[i][j] > arr[i][j - 1 ])
198
+ {
199
+ cnt++;
200
+ mn = max (mn, arr[i][j - 1 ]);
201
+ }
202
+ tempcnt++;
203
+ }
204
+ if (j + 1 < c)
205
+ {
206
+ if (arr[i][j] > arr[i][j + 1 ])
207
+ {
208
+ cnt++;
209
+ mn = max (mn, arr[i][j + 1 ]);
210
+ }
211
+ tempcnt++;
212
+ }
213
+ if (cnt == tempcnt && tempcnt!=0 )
214
+ arr[i][j] = mn;
215
+ }
216
+ }
217
+
218
+
219
+ FOR (r)
220
+ {
221
+ FORJ (c)
222
+ {
223
+ cout << arr[i][j] << " " ;
224
+ }
225
+ cout << nl;
226
+ }
227
+ }
228
+
229
+ int32_t main ()
230
+ {
231
+ faster;
232
+ int t = 1 ;
233
+ cin >> t;
234
+ int c = 1 ;
235
+ while (t--)
236
+ {
237
+ // cout << "Case " << c++ << ": ";
238
+ solution ();
239
+ }
240
+ CRACKED;
241
+ }
0 commit comments