This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <complex>
using namespace std;
using ll = long long;
const complex<int> IMAG(0, 1);
vector<vector<ll>> special;
vector<vector<ll>> used;
vector<vector<ll>> f;
ll unfill(ll i, ll j){
--used[i][j];
return -f[i][j];
}
ll fill(ll i, ll j){
++used[i][j];
return f[i][j];
}
ll fill_plus(ll i, ll j){
ll ans = fill(i, j);
for(complex<int> timer = 0, mv = 1; timer != 4; timer += 1, mv *= IMAG){
ans += fill(i + mv.real(), j + mv.imag());
}
special[i][j] = 0;
return ans;
}
ll dfs(ll i, ll j){
if(special[i][j]){
ll ans = 0;
for(complex<int> timer = 0, mv = 1; timer != 4; timer += 1, mv *= IMAG){
if(used[i + mv.real()][j + mv.imag()]){
ans += unfill(i + mv.real(), j + mv.imag());
break;
}
}
ans += fill_plus(i, j);
for(complex<int> timer = 0, mv = 1; timer != 4; timer += 1, mv *= IMAG){
ans += dfs(i + mv.real(), j + mv.imag());
}
return ans;
} else {
for(complex<int> timer = 0, mv = 1; timer != 4; timer += 1, mv *= IMAG){
if(0 < i + mv.real() && i + mv.real() < f.size() && 0 < j + mv.imag() && j + mv.imag() < f[0].size() && special[i + mv.real()][j + mv.imag()]){
return dfs(i + mv.real(), j + mv.imag());
}
}
return 0;
}
}
complex<int> mindfs(int i, int j){
complex<int> ans = i + 1 + j * IMAG;
for(complex<int> timer = 0, mv = 1; timer != 4; timer += 1, mv *= IMAG){
if(f[ans.real()][ans.imag()] > f[i + mv.real()][j + mv.imag()]){
ans = mv + i + j * IMAG;
}
}
special[i][j] = 2;
for(complex<int> timer = 0, mv = 1; timer != 4; timer += 1, mv *= IMAG){
if(special[i + mv.real() * 2][j + mv.imag() * 2] == 1){
complex<int> nans = mindfs(i + mv.real() * 2, j + mv.imag() * 2);
if(f[ans.real()][ans.imag()] > f[nans.real()][nans.imag()]){
ans = nans;
}
}
}
return ans;
}
signed main(){
ll n, m;
cin >> n >> m;
f.assign(n + 2, vector<ll>(m + 2));
used.assign(n + 2, vector<ll>(m + 2));
special.assign(n + 2, vector<ll>(m + 2));
for(ll i = 0; i <= n + 1; ++i)
used[i][0] = used[i][m + 1] = 1;
for(ll j = 0; j <= m + 1; ++j)
used[0][j] = used[n + 1][j] = 1;
for(ll i = 1; i <= n; ++i){
for(ll j = 1; j <= m; ++j){
cin >> f[i][j];
}
}
ll q;
cin >> q;
for(ll i = 0; i < q; ++i){
ll x, y;
cin >> x >> y;
++x, ++y;
special[x][y] = 1;
}
ll ans = 0;
for(ll i = 1; i <= n; ++i){
for(ll j = 1; j <= m; ++j){
if(!special[i][j])
continue;
if(special[i + 1][j]){
ans += fill_plus(i, j);
ans += unfill(i + 1, j);
ans += fill_plus(i + 1, j);
ans += unfill(i, j);
}
if(special[i][j + 1]){
ans += fill_plus(i, j);
ans += unfill(i, j + 1);
ans += fill_plus(i, j + 1);
ans += unfill(i, j);
}
if(special[i + 1][j + 1]){
ans += fill_plus(i, j);
ans += unfill(i, j + 1);
ans += fill_plus(i + 1, j + 1);
ans += unfill(i + 1, j);
}
if(special[i - 1][j + 1]){
ans += fill_plus(i, j);
ans += unfill(i, j + 1);
ans += fill_plus(i - 1, j + 1);
ans += unfill(i - 1, j);
}
}
}
for(ll i = 0; i <= n + 1; ++i){
for(ll j = 0; j <= m + 1; ++j){
if(used[i][j]){
ans += dfs(i, j);
}
}
}
//for(ll i = 0; i <= n + 1; ++i){
//for(ll j = 0; j <= m + 1; ++j){
//cout << used[i][j] << ' ';
//}
//cout << endl;
//}
//cout << ans << endl;
for(ll i = 1; i <= n; ++i){
for(ll j = 1; j <= m; ++j){
if(special[i][j]){
complex<int> pt = mindfs(i, j);
used[pt.real()][pt.imag()] = 1;
ans += dfs(pt.real(), pt.imag());
}
}
}
for(ll i = 0; i <= n + 1; ++i){
for(ll j = 0; j <= m + 1; ++j){
if(used[i][j] > 1){
cout << "No\n";
return 0;
}
}
}
cout << ans << endl;
}
Compilation message (stderr)
covering.cpp: In function 'll dfs(ll, ll)':
covering.cpp:47:42: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | if(0 < i + mv.real() && i + mv.real() < f.size() && 0 < j + mv.imag() && j + mv.imag() < f[0].size() && special[i + mv.real()][j + mv.imag()]){
| ~~~~~~~~~~~~~~^~~~~~~~~~
covering.cpp:47:91: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | if(0 < i + mv.real() && i + mv.real() < f.size() && 0 < j + mv.imag() && j + mv.imag() < f[0].size() && special[i + mv.real()][j + mv.imag()]){
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |