Submission #466901

# Submission time Handle Problem Language Result Execution time Memory
466901 2021-08-21T02:41:48 Z ntabc05101 Bob (COCI14_bob) C++14
120 / 120
173 ms 13940 KB
#include<bits/stdc++.h>
using namespace std;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int n, m; cin >> n >> m;
  
	int a[n][m];
	int col[m];
	memset(col, 0, sizeof(col));
	
	long long res = 0;
	for (int i = 0; i < n; i++) {
		vector<int> ranges;
		ranges.push_back(0);
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			if (i > 0 && a[i][j] != a[i - 1][j]) {
				col[j] = 0;
			}
			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++) {
			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);
				
				res += 1ll * (col[j] - mx) * (right - left[j]) * (right - left[j] - 1) / 2;
			}
		}
	}
	
	cout << res << endl;
	
	return 0;
}

Compilation message

bob.cpp: In function 'int main()':
bob.cpp:29:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |   for (int c = 1; c < ranges.size(); c++) {
      |                   ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 2180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 2372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 2424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 153 ms 10912 KB Output is correct
2 Correct 92 ms 6120 KB Output is correct
3 Correct 91 ms 6132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 169 ms 13940 KB Output is correct
2 Correct 102 ms 6212 KB Output is correct
3 Correct 91 ms 6132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 13732 KB Output is correct
2 Correct 87 ms 6124 KB Output is correct
3 Correct 86 ms 6116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 13824 KB Output is correct
2 Correct 94 ms 6128 KB Output is correct
3 Correct 91 ms 6124 KB Output is correct