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<stack>
#include<memory.h>
#include<stdlib.h>
#define pi pair<int,int>
#define mp make_pair
using namespace std;
const int max_n = 3030;
int n,m,h[max_n],l[max_n],r[max_n],lbnd[max_n];
long long ans;
char a[max_n];
inline long long comb(int x) {
return 1LL * x*(x+1)/2;
}
int main() {
ios_base::sync_with_stdio(false);
cin>>n>>m;
for(int i=0;i<n;i++) {
cin>>a;
for(int j=0;j<m;j++) {
if(a[j]=='R') h[j]++;
else h[j] = 0;
}
stack<pi> s;
s.push(mp(-1,-1));
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
memset(lbnd,0,sizeof(lbnd));
for(int j=0;j<m;j++) {
while(1) {
pi T = s.top();
if(T.second > h[j]) {
s.pop();
r[T.first] = j;
lbnd[T.first] = max(lbnd[T.first], h[j]);
}
else break;
}
if(s.top().second == h[j]) {
pi T= s.top();
l[j] = l[T.first];
lbnd[j] = lbnd[T.first];
s.pop();
}
else {
l[j] = s.top().first;
lbnd[j] = (s.top().second == -1) ? 0 : s.top().second;
}
s.push(mp(j,h[j]));
}
while(!s.empty()) {
int now = s.top().first;
r[now] = m;
s.pop();
}
for(int j=0;j<m;j++) {
if(r[j]) {
ans += comb(r[j]-l[j]-1) * (h[j]-lbnd[j]);
}
}
}
cout<<ans<<endl;
return 0;
}
/*
1
3+1=4
6+4=10
10+10=20
15+20=35
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |