이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Comp = array<ll, 3>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
struct BIT {
int n;
vector<int> tree;
void config(int _n) {
n = _n + 10;
tree.resize(_n+60);
}
void update(int p, int v) {
for(p++; p<n; p+=p&-p) tree[p] += v;
}
int query(int p) {
int ans = 0;
for(p++; p>0; p-=p&-p) ans += tree[p];
return ans;
}
int query(int l, int r) { return query(r) - query(l-1); }
};
int dr[8] = {0, -1, 0, 1, 1, 1, -1, -1};
int dc[8] = {-1, 0, 1, 0, 1, -1, 1, -1};
char mat[2005][2005];
bool vis[2005][2005];
int L=1e9, R=-1, sum=0, n, m;
vector<Comp> comp;
void dfs(int r, int c) {
vis[r][c] = 1;
L = min(L, c); R = max(R, c);
sum += (mat[r][c] - '0');
for(int i=0; i<4; i++) {
int newR = r + dr[i];
int newC = c + dc[i];
if(newR < 0 || newR >= n || newC < 0 || newC >= m) continue;
if(vis[newR][newC] || mat[newR][newC] == '.') continue;
dfs(newR, newC);
}
}
int dp[2005][2005], val[2005][2005];
void f(int l, int r, int tl, int tr, int j) {
if(l > r) return ;
int tm = (l + r) / 2, p = tl;
for(int i=tl; i<=min(tm, tr); i++) {
ll v = dp[i-1][j-1] + val[i][tm];
if(v > dp[tm][j]) {
dp[tm][j] = v;
p = i;
}
}
f(l, tm-1, tl, p, j);
f(tm+1, r, p, tr, j);
}
signed main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
cin >> n >> m;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++) cin >> mat[i][j];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(vis[i][j] || mat[i][j] == '.') continue;
L=1e9, R=-1, sum=0;
dfs(i, j);
comp.push_back({ L+1, R+1, sum });
}
}
sort(comp.begin(), comp.end(), [&](Comp &a, Comp &b) { return a[1] < b[1]; });
BIT bit; bit.config(m);
for(auto &[l, r, v] : comp) bit.update(l, v);
int ptr = 0;
for(int j=1; j<=m; j++) {
while(ptr < comp.size() && comp[ptr][1] < j) {
bit.update(comp[ptr][0], -comp[ptr][2]);
ptr++;
}
for(int i=1; i<=j; i++) val[i][j] = bit.query(i, j);
}
for(int i=1; i<=m; i++) {
f(1, m, 1, m, i);
int mx = 0;
for(int j=1; j<=m; j++) mx = max(mx, dp[j][i]);
cout << mx << '\n';
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
nafta.cpp: In function 'int main()':
nafta.cpp:104:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | while(ptr < comp.size() && comp[ptr][1] < j) {
| ~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |