답안 #335492

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
335492 2020-12-12T19:05:40 Z VROOM_VARUN Strah (COCI18_strah) C++14
110 / 110
249 ms 67180 KB
/*
ID: varunra2
LANG: C++
TASK: strah
*/

#include <bits/stdc++.h>
using namespace std;

#define int long long

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions

int n, m;
VVI grid;
VVI cnt;
int ret = 0;

void init() {
  grid.resize(n);
  trav(x, grid) x.resize(m);
}

int calc(int val) { return (val * (val + 1)) / 2; }

int calc(int l, int r) { return calc(r) - calc(l - 1); }

ll solve(VI& cur, int pos) {
  int ret = 0;
  int sum1 = 0;  // sums of cur of all elements
  int sum2 = 0;  // sums of cur * i of all elements
  stack<PII> st;
  for (int i = 0; i < n; i++) {
    PII val(cur[i], 1);  // val, cnt
    int ind = i;
    while (!st.empty() and st.top().x >= val.x) {
      sum1 -= calc(st.top().x) * st.top().y;
      sum2 -= calc(st.top().x) * calc(ind - st.top().y + 1, ind);
      ind -= st.top().y;
      val.y += st.top().y;
      st.pop();
    }
    st.push(val);
    sum1 += val.y * calc(val.x);
    sum2 += calc(ind + 1, i + 1) * calc(val.x);
    int cur = (i + 2) * sum1 - sum2;
    ret += cur;
    // debug(i, pos, cur, sum1, sum2);
  }
  return ret;
}

int32_t main() {
  // #ifndef ONLINE_JUDGE
  // freopen("strah.in", "r", stdin);
  // freopen("strah.out", "w", stdout);
  // #endif
  cin.sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m;

  init();

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == '.')
        grid[i][j] = 1;
      else
        grid[i][j] = 0;
    }
  }

  cnt = grid;

  for (int i = 0; i < n; i++) {
    for (int j = 1; j < m; j++) {
      if (grid[i][j]) cnt[i][j] += cnt[i][j - 1];
    }
  }

  for (int i = 0; i < m; i++) {
    VI cur;
    for (int j = 0; j < n; j++) {
      cur.PB(cnt[j][i]);
    }
    ret += solve(cur, i);
  }

  // trav(x, cnt) debug(x);

  cout << ret << '\n';

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1900 KB Output is correct
2 Correct 7 ms 1900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1900 KB Output is correct
2 Correct 6 ms 1900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1900 KB Output is correct
2 Correct 7 ms 1900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 17900 KB Output is correct
2 Correct 141 ms 40252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 41452 KB Output is correct
2 Correct 238 ms 61676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 26396 KB Output is correct
2 Correct 154 ms 44012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 4844 KB Output is correct
2 Correct 177 ms 49600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 230 ms 67180 KB Output is correct
2 Correct 249 ms 67052 KB Output is correct