Submission #1007331

#TimeUsernameProblemLanguageResultExecution timeMemory
1007331VMaksimoski008Nafta (COI15_nafta)C++17
100 / 100
365 ms130892 KiB
#include <bits/stdc++.h> using namespace std; using Comp = array<int, 3>; struct BIT { int n; vector<int> tree; BIT(int _n) : n(_n+60), tree(_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[4] = {0, -1, 0, 1}, dc[4] = {-1, 0, 1, 0}; char M[2005][2005]; int L=1e9, R=-1, sum=0, n, m, dp[2005][2005], val[2005][2005], vis[2005][2005]; vector<Comp> comp; void dfs(int r, int c) { vis[r][c] = 1; L = min(L, c); R = max(R, c); sum += (M[r][c] - '0'); for(int i=0; i<4; i++) { int nr = r + dr[i], nc = c + dc[i]; if(nr < 0 || nr >= n || nc < 0 || nc >= m) continue; if(vis[nr][nc] || M[nr][nc] == '.') continue; dfs(nr, nc); } } 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++) { int 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() { cin >> n >> m; for(int i=0; i<n; i++) for(int j=0; j<m; j++) cin >> M[i][j]; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(vis[i][j] || M[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(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; }

Compilation message (stderr)

nafta.cpp: In function 'int main()':
nafta.cpp:79:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |         while(ptr < comp.size() && comp[ptr][1] < j) {
      |               ~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...