This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
#ifndef __SIZEOF_INT128__
#define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < (k); ++i)
#define per(i, p, k) for(int i(p); i > (k); --i)
#define sz(x) (int)(x).size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fi first
#define se second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
string name{""};
time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
duration<float, std::milli> dur;
Timer() = default;
Timer(string nm): name(nm) {}
~Timer() {
end = high_resolution_clock::now(); dur= end - start;
cout << "@" << name << "> " << dur.count() << " ms" << '\n';
}
};
template <typename T = int> inline T in()
{
static T x;
std::cin >> x;
return x;
}
std::string yn(bool b)
{
if(b) return "YES\n";
else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
for(const auto& i : wek)out << i << ' ';
return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
out << '{'<<par.first<<", "<<par.second<<"}";
return out;
}
#define show(x) cerr << #x << " = " << x << '\n';
constexpr int maxa = 7e6 +3, maxn = 2503, maxs = maxn*maxn;
vector <pair <int, int>> v[maxa];
bool on[maxn][maxn];
int pti(pair <int, int> p){return 1+p.first * maxn + p.second;}
array <int, maxs> rp, xl, xr, yl, yr, si;
int find(int w){
if(!rp[w])return w;
return rp[w] = find(rp[w]);
}
void uni(pair <int, int> pa, pair <int, int> pb){
int a(find(pti(pa))), b(find(pti(pb)));
if(a == b)return;
rp[a] = b;
si[b] += si[a];
xl[b] = min(xl[b], xl[a]);
xr[b] = max(xr[b], xr[a]);
yl[b] = min(yl[b], yl[a]);
yr[b] = max(yr[b], yr[a]);
}
ll count_rectangles(vector <vector <int>> a)
{
int n(sz(a)), m(sz(a[0]));
rep(i, 0, n)rep(j, 0, m){
int g(pti({i, j}));
si[g] = 1;
xl[g] = xr[g] = i;
yl[g] = yr[g] = j;
}
rep(i, 0, n)rep(j, 0, m)v[a[i][j]].pb(i, j);
ll o(0);
rep(i, 0, maxa)if(sz(v[i])){
set <int> tupd;
for(auto [x, y]: v[i]){
on[x][y] = 1;
if(x > 0 && on[x-1][y])uni({x, y}, {x-1, y});
if(x < n-1 && on[x+1][y])uni({x, y}, {x+1, y});
if(y > 0 && on[x][y-1])uni({x, y}, {x, y-1});
if(y < m-1 && on[x][y+1])uni({x, y}, {x, y+1});
tupd.insert(find(pti({x, y})));
}
for(auto r: tupd)if(find(r) == r && si[r] == (xr[r] - xr[r]+1)*(yr[r] - yl[r]+1))++o;
// cerr << i << "---------------";
// for(auto i: v[i])cerr << pti(i) << ' ';
// cerr << '\n';
// rep(i, 0, n){
// rep(j, 0, m)cerr << find(pti({i, j})) << ' ';
// cerr << '\n';
// }
}
return o;
}
# | 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... |