AC题解
2023-08-28 08:20:06
发布于:广东
4阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int N;
bool ok[1000][1000];
ll solve() {
ll ans = 0;
vector<int> lst(N,N-1);
vector<int> to_add[1000];
for (int i = N-1; i >= 0; --i) {
for (int j = i; j < N; ++j) to_add[j].clear();
for (int k = 0; k < N; ++k) {
if (ok[i][k] == 0) lst[k] = i-1;
else to_add[lst[k]].push_back(k);
}
int sum_comb = 0;
vector<int> lef(N,-1), rig(N,-1);
for (int j = N-1; j >= i; --j) {
for (int k: to_add[j]) {
int l = k, r = k;
auto c2 = [](int x) { return (x+1)*(x+2)/2; };
if (k && lef[k-1] != -1) {
l = lef[k-1];
sum_comb -= c2(k-1-l);
}
if (k+1 < N && rig[k+1] != -1) {
r = rig[k+1];
sum_comb -= c2(r-k-1);
}
lef[r] = l, rig[l] = r;
sum_comb += c2(r-l);
}
ans += sum_comb;
}
}
return ans;
}
int main() {
cin >> N;
vector<vector<int>> pasture(N,vector<int>(N));
for (vector<int>& a: pasture)
for (int& b: a) cin >> b;
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
ok[i][j] = pasture[i][j] >= 100;
ll ans = solve();
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
ok[i][j] = pasture[i][j] > 100;
ans -= solve();
cout << ans << "\n";
}
这里空空如也
有帮助,赞一个