#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define REP(a) F0R(_,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define rsz resize
#define pb push_back
#define f first
#define s second
#define pf push_front
#define ft front()
#define bk back()
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define tcT template<class T
#define tcTU tcT, class U
#define nl "\n"
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using pi = pair<int,int>;
using vpi = vector<pi>;
using str = string;
using vs = vector<str>;
using db = double;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
tcTU> using PR = pair<T,U>;
const int MOD = 1e9+7;
const ll INF = 1e18;
const int dx[] = {-1,0,1,0}, dy[] = {0,-1,0,1};
constexpr int bits(int x) { return x == 0 ? 0 : 31 - __builtin_clz(x); }
constexpr bool onbit(int msk, int i) { return msk>>i&1; }
constexpr ll p2(int x) { return 1LL<<x; }
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int lg2(int x) { return x == 0 ? 0 : 31 - __builtin_clz(x); }; //int only
template<int N> constexpr ll pow10 = pow10<N-1> * 10;
template<> constexpr ll pow10<0> = 1;
tcT> bool ckmin(T&a, const T&b) { return b < a ? a = b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) { return b > a ? a = b,1 : 0; }
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); }
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); }
tcTU> T fstTrue(T lo, T hi, U f) {
hi++; assert(lo <= hi);
while (lo < hi) {
T mid = lo+(hi-lo)/2;
f(mid) ? hi = mid : lo = mid+1;
}
return lo;
}
tcTU> T lstTrue(T lo, T hi, U f) {
lo--; assert(lo <= hi);
while (lo < hi) {
T mid = lo+(hi-lo+1)/2;
f(mid) ? lo = mid : hi = mid-1;
}
return lo;
}
tcT> void remDup(V<T> &v) {
sort(all(v)); v.erase(unique(all(v)), end(v)); }
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
void setPrec() { cout << fixed << setprecision(16); }
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
setPrec();
if(sz(NAME)) {
freopen((NAME + ".in").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
const int MX = 1e3+5;
int N,M,A[MX][MX], amt[MX][MX];
ll ans = 0;
struct Nod {
int cnt, v, h;
ll prev;
Nod(int _cnt = 0, int _v = 0, int _h = 0, ll _prev = 0) {
cnt = _cnt; v = _v; h = _h;
prev = _prev;
}
};
void solve() {
cin>>N>>M;
FOR(i,1,N+1) FOR(j,1,M+1) cin>>A[i][j];
FOR(i,1,N+1) {
stack<Nod> st;
st.push(Nod(0, -1, 0, 0));
FOR(j,1,M+1) {
amt[i][j] = 1;
if(i>0 && A[i][j] == A[i-1][j]) amt[i][j] = amt[i-1][j] + 1;
int cnt = 1;
while(st.top().v == A[i][j] && st.top().h >= amt[i][j]) {
cnt += st.top().cnt;
st.pop();
}
ll tmp = 1LL * cnt * amt[i][j];
if(st.top().v == A[i][j]) tmp += st.top().prev;
ans += tmp;
st.push(Nod(cnt, A[i][j], amt[i][j], tmp));
}
}
cout << ans << "\n";
}
int main() {
setIO();
int t=1;
//cin>>t;
while(t-->0) {
solve();
}
return 0;
}
Compilation message
bob.cpp: In function 'void setIO(std::string)':
bob.cpp:96:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen((NAME + ".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bob.cpp:97:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen((NAME + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
4828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
5136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
5316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
5424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
14824 KB |
Output is correct |
2 |
Correct |
67 ms |
10020 KB |
Output is correct |
3 |
Correct |
64 ms |
10104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
17860 KB |
Output is correct |
2 |
Correct |
60 ms |
10104 KB |
Output is correct |
3 |
Correct |
63 ms |
10104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
17732 KB |
Output is correct |
2 |
Correct |
67 ms |
10100 KB |
Output is correct |
3 |
Correct |
59 ms |
10108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
17804 KB |
Output is correct |
2 |
Correct |
58 ms |
10052 KB |
Output is correct |
3 |
Correct |
59 ms |
10076 KB |
Output is correct |