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 <bits/stdc++.h>
using namespace std;
// #define int long long
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define FILE_IO(x) freopen((string(x) + ".in").c_str(), "r", stdin); freopen((string(x) + ".out").c_str(), "w", stdout)
#define f first
#define s second
#define x1 x1qwer
#define y1 y1qwer
#define right right123
#define left left123
#define foreach(it, v) for (auto it : v)
#define rep(it, n) for (int it = 0; it < n; ++it)
#define forin(it, l, r) for (int it = l; it < r; ++it)
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const double DINF = numeric_limits<double>::infinity();
const ll MOD = 1e9 + 7;
const double EPS = 1e-7;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
mt19937 mmtw_(MOD);
uniform_int_distribution<ll> rd_;
ll randomll() { return rd_(mmtw_);}
ll rnd(ll x, ll y) { return rd_(mmtw_) % (y - x + 1) + x; }
template <class T> T fact(T n) { if (n == 1) return 1; return n * fact(n - 1); }
////////////////////////////////////////////////////////////////////////////////////////////////
namespace solver {
const int MAXN = 1e3 + 5;
vector<pair<int, int>> a;
int pos;
void clear() {
a.clear();
a.push_back({0, -1});
pos = -1;
}
ll feed(int x) {
pos++;
while (!a.empty() && a.back().f >= x) {
a.pop_back();
}
a.push_back({x, pos});
ll res = 0;
for (int i = 1; i < a.size(); ++i) {
res += 1LL * (a[i].f - a[i - 1].f) * (pos - a[i - 1].s);
}
return res;
}
};
const int MAXN = 1e3 + 5;
int n, m;
int a[MAXN][MAXN];
int eq[MAXN][MAXN];
int main() {
FAST_IO;
cin >> n >> m;
rep(i, n) {
rep(j, m) {
cin >> a[i][j];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (i == 0) {
eq[i][j] = 1;
continue;
}
if (a[i][j] == a[i - 1][j]) {
eq[i][j] = eq[i - 1][j] + 1;
} else {
eq[i][j] = 1;
}
}
}
ll res = 0;
for (int i = 0; i < n; ++i) {
solver::clear();
for (int j = 0; j < m; ++j) {
if (j != 0 && a[i][j] != a[i][j - 1]) {
solver::feed(0);
}
res += solver::feed(eq[i][j]);
}
}
cout << res << endl;
}
Compilation message (stderr)
bob.cpp: In function 'll solver::feed(int)':
bob.cpp:56:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < a.size(); ++i) {
~~^~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |