Submission #1118584

#TimeUsernameProblemLanguageResultExecution timeMemory
1118584vjudge1Bitaro the Brave (JOI19_ho_t1)C++17
100 / 100
417 ms138412 KiB
#include <bits/stdc++.h>
#define F first
#define S second
#define LL long long
 
#define rep(i, x) for(auto &i : (x))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
 
#define left(x) ((x)<<1)
#define right(x) ((x)>>1)
#define LSOne(x) (x & -(x))
 
using namespace std;
using T = pair<pair<int, int>, int>;
 
const int SZ = 2e5 + 1;
const LL MOD = 1e9 + 7; 
const LL INF = 1e18 + 1;
 
LL bp (LL b, LL e, LL m = MOD) {
 if ( e == 0 ) return 1;
 LL T = bp(b, e / 2);
 T *= T; T %= m;
 if ( e & 1 ) T *= b;
 return T %= m;
}
 
LL nv_i (LL a) { return bp(a, MOD - 2); };
LL gcd (LL a, LL b) { return (b == 0 ? a : gcd(b, a % b)); }
LL lcm(LL a, LL b) { return (a * (b / gcd(a, b))); }
LL ceil (LL a, LL b) { return ((a + b - 1) / b); }
 
void solve() {
  int n, m;
  cin >> n >> m;
  
  vector<pair<int, int>> Js;
  vector<vector<int>> Px_S_Os(n + 1, vector<int> (m + 1));
  vector<vector<int>> Px_S_Is(m + 1, vector<int> (n + 1));

  for(int i = 1; i <= n; i ++) {
    for(int j = 1; j <= m; j ++) {
      char x;
      cin >> x;

      if(x == 'J') {
        Js.emplace_back(i, j);
      }
      if(x == 'O') {
        Px_S_Os[i][j] ++;
      }
      if(x == 'I') {
        Px_S_Is[j][i] ++;
      }
    }
  }

  for(int i = 1; i <= n; i ++) {
    for(int j = 1; j <= m; j ++) {
      Px_S_Os[i][j] += Px_S_Os[i][j - 1];
    }
  }

  for(int j = 1; j <= m; j ++) {
    for(int i = 1; i <= n; i ++) {
      Px_S_Is[j][i] += Px_S_Is[j][i - 1];
    }
  }

  LL ans = 0, pos = 0;
  vector<int> CNT; 
  
  rep(J, Js) {
    int sum = Px_S_Os[J.F][m] - Px_S_Os[J.F][J.S - 1];
    CNT.push_back(sum);
  }

  rep(J, Js) {
    int sum = Px_S_Is[J.S][n] - Px_S_Is[J.S][J.F - 1];
    ans += CNT[pos] * sum;
    pos ++;
  }

  cout << ans;
} 
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
 
  solve();
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...