#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 18;
const ll INF = 1e15;
const ll MAXN = 2010;
vector<int> occ[2005];
int N, M;
int grid[MAXN][MAXN], emp[MAXN][MAXN];
int sg[4*MAXN], lazy[4*MAXN];
ll total = 0, ans = 0;
void init(int k, int a, int b) {
if (a == b) {
sg[k] = 0;
lazy[k] = -1;
return ;
}
init(2*k, a, (a+b)/2);
init(2*k+1, (a+b)/2+1, b);
sg[k] = max(sg[2*k], sg[2*k+1]);
lazy[k] = -1;
}
void push(int k, int a, int b) {
if (lazy[k] != -1) {
sg[k] = lazy[k];
if (a != b) {
lazy[2*k] = lazy[k];
lazy[2*k+1] = lazy[k];
}
lazy[k] = -1;
}
}
void update(int k, int a, int b, int q_l, int q_r, int new_val) {
push(k, a, b);
if (q_r < a || q_l > b)
return ;
if (q_l <= a && b <= q_r) {
lazy[k] = new_val;
push(k, a, b);
return ;
}
update(2*k, a, (a+b)/2, q_l, q_r, new_val);
update(2*k+1, (a+b)/2+1, b, q_l, q_r, new_val);
sg[k] = max(sg[2*k], sg[2*k+1]);
}
ll calc(ll x) {
return (x * x * x + 3 * x * x + 2 * x) / 6;
}
int get(int k, int a, int b, int pos) {
push(k, a, b);
if (b < pos || a > pos)
return 0;
if (a == b)
return sg[k];
return get(2*k, a, (a+b)/2, pos) + get(2*k+1, (a+b)/2+1, b, pos);
}
int query(int k, int a, int b, int val) {
push(k, a, b);
if (a == b) {
if (sg[k] >= val)
return a;
return N+1;
}
push(2*k, a, (a+b)/2);
if (sg[2*k] < val)
return query(2*k+1, (a+b)/2 + 1, b, val);
return query(2*k, a, (a+b)/2, val);
}
void split(int pos) {
int Q = get(1, 0, N, pos);
int l = query(1, 0, N, Q);
int r = query(1, 0, N, Q + 1) - 1;
ans -= calc(r - l);
ans += calc(pos - l - 1);
ans += calc(r - pos);
update(1, 0, N, pos, r, pos);
}
int main() {
fast
cin >> N >> M;
for (int i = 1; i <= N; i++) {
string s;
cin >> s;
for (int j = 1; j <= M; j++)
grid[i][j] = (s[j-1] == '.');
}
for (int j = M; j >= 1; j--) {
for (int i = 1; i <= N; i++) {
if (grid[i][j])
emp[i][j] = emp[i][j+1] + 1;
}
}
for (int j = 1; j <= M; j++) {
init(1, 0, N);
ans = calc(N);
for (int q = 1; q <= 2000; q++)
occ[q].clear();
for (int i = 1; i <= N; i++) {
if (emp[i][j] == 0)
split(i);
else
occ[emp[i][j]].push_back(i);
}
for (ll q = 1; q <= 2000; q++) {
total += ans * q;
for (auto u : occ[q])
split(u);
}
}
cout << total << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
6236 KB |
Output is correct |
2 |
Correct |
29 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
6236 KB |
Output is correct |
2 |
Correct |
29 ms |
6232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
6748 KB |
Output is correct |
2 |
Correct |
29 ms |
6296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
370 ms |
16372 KB |
Output is correct |
2 |
Correct |
914 ms |
29184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
886 ms |
25756 KB |
Output is correct |
2 |
Execution timed out |
1041 ms |
35156 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
579 ms |
17332 KB |
Output is correct |
2 |
Execution timed out |
1029 ms |
30160 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
25436 KB |
Output is correct |
2 |
Execution timed out |
1060 ms |
36168 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1043 ms |
40012 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |