Submission #489642

# Submission time Handle Problem Language Result Execution time Memory
489642 2021-11-23T13:44:27 Z ntabc05101 Strah (COCI18_strah) C++14
110 / 110
208 ms 8148 KB
#include<bits/stdc++.h>
using namespace std;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int n, m; cin >> n >> m;
	
	char a[n][m];
	int col[m];
	memset(col, 0, sizeof(col));
	
	long long res = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		vector<int> ranges;
		ranges.push_back(0);
		for (int j = 0; j < m; j++) {
			if (a[i][j] == '#') {
				col[j] = 0;
			}
			else {
				col[j]++;
			}
			if (j > 0 && a[i][j] != a[i][j - 1]) {
				ranges.push_back(j);
			}
		}
		ranges.push_back(m);
		
		for (int c = 1; c < ranges.size(); c++) {
			//cout << ranges[c] << " ";
			int left[m];
			stack<int> st;
			for (int j = ranges[c - 1]; j < ranges[c]; j++) {
				while (!st.empty() && col[st.top()] >= col[j]) {
					st.pop();
				}
				left[j] = st.empty() ? ranges[c - 1] - 1: st.top();
				st.push(j);
			}
			
			while (!st.empty()) {
				st.pop();
			}
			for (int j = ranges[c] - 1; j >= ranges[c - 1]; j--) {
				while (!st.empty() && col[st.top()] > col[j]) {
					st.pop();
				}
				int right = st.empty() ? ranges[c]: st.top();
				int mx = max((left[j] == ranges[c - 1] - 1 ? 0: col[left[j]]), (right == ranges[c] ? 0 : col[right]));
				st.push(j);
				
				//cout << i << " " << j << " " << left[j] << " " << right << "\n";
				long long b = col[j] - mx, a = right - left[j] - 1;
				//cout << i << " " << j << " " << a << " " << b << "\n";
				res += (a * a * (a + 1) / 2 - (a - 1) * a * (a + 1) / 3) * ((col[j] + mx + 1) * b / 2);
			}
		}
		//cout << "\n";
	}
	
	cout << res << endl;
	
	return 0;
}

Compilation message

strah.cpp: In function 'int main()':
strah.cpp:35:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |   for (int c = 1; c < ranges.size(); c++) {
      |                   ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 464 KB Output is correct
2 Correct 7 ms 408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 496 KB Output is correct
2 Correct 5 ms 496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 464 KB Output is correct
2 Correct 5 ms 484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 2348 KB Output is correct
2 Correct 120 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 5136 KB Output is correct
2 Correct 167 ms 7484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 3360 KB Output is correct
2 Correct 122 ms 5408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 720 KB Output is correct
2 Correct 119 ms 5996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 8044 KB Output is correct
2 Correct 208 ms 8148 KB Output is correct