#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
const int mxN = 4*1e6;
const int mxS = 2000;
int R,S;
int sums[mxN];
pii bounds[mxN];
int grid[mxN];
int vis[mxN];
int cnt = 0;
void bfs(int node){
queue<int> queue;
queue.push(node);
int curr,r,s;
bounds[cnt].f = mxS+1;
bounds[cnt].s = -1;
while (queue.size() > 0){
curr = queue.front();
queue.pop();
if (vis[curr] != -1 || grid[curr] == -1){
continue;
}
vis[curr] = cnt;
sums[cnt] += grid[curr];
r = curr / S;
s = curr % S;
bounds[cnt].f = min(bounds[cnt].f, s);
bounds[cnt].s = max(bounds[cnt].s, s);
if (r > 0){
queue.push(curr - S);
}
if (r < R-1){
queue.push(curr+S);
}
if (s > 0){
queue.push(curr-1);
}
if (s < S-1){
queue.push(curr+1);
}
}
cnt++;
}
int res[mxS+1];
int tres[mxS+1];
vector<vector<int>> cont(mxS+1);
vector<vector<int>> psums(mxS+1);
int added[mxS+1][mxS+1];
void dcdp(int l, int r, int sl, int sr){
int m = (l+r)/2;
tres[m] = 0;
int bidx = -1;
int cidx = 0;
if (cont[m].size() > 0){
cidx = lower_bound(cont[m].begin(),cont[m].end(),min(m-1,sr)) - cont[m].begin();
}
for (int i = min(m-1,sr); i >= sl; i--){
if (cont[m].size() > 0){
while (cidx > 0 && cont[m][cidx-1] >= i){
cidx--;
}
}
if (res[i] + psums[m][cidx] > tres[m]){
tres[m] = res[i] + psums[m][cidx];
bidx = i;
}
}
// cout << bidx << "\n";
if (l != m){
dcdp(l,m-1,sl,bidx);
}
if (r != m){
dcdp(m+1,r,bidx,sr);
}
}
int main()
{
memset(sums,0,sizeof(sums));
for (int i =0; i < mxN; i++){
vis[i] = -1;
}
// ios_base::sync_with_stdio(false);
//cin.tie(NULL);
cin >> R >> S;
string row;
for (int i =0; i < R; i++){
cin >> row;
for (int j =0; j < S; j++){
if (row[j] == '.'){
grid[i*S+j] = -1;
} else {
grid[i*S+j] = row[j]-'0';
}
}
}
for (int i = 0; i < R*S; i++){
if (vis[i] == -1 && grid[i] != -1){
bfs(i);
}
}
memset(added,0,sizeof(added));
for (int i =0; i < cnt; i++){
bounds[i].f += 1;
bounds[i].s += 1;
for (int j = bounds[i].f; j <= bounds[i].s; j++){
if (added[j][bounds[i].f-1] == 0){
cont[j].pb(bounds[i].f-1);
}
added[j][bounds[i].f-1] += sums[i];
}
}
for (int i = 0; i <= S; i++){
if (cont[i].size() == 0){
psums[i].pb(0);
continue;
}
sort(cont[i].begin(),cont[i].end());
psums[i].resize(cont[i].size());
psums[i][cont[i].size()-1] = added[i][cont[i][cont[i].size()-1]];
for (int j = cont[i].size()-2; j >= 0; j--){
psums[i][j] = added[i][cont[i][j]] + psums[i][j+1];
}
psums[i].pb(0);
}
memset(res,0,sizeof(res));
int tot;/*
cout << "RD\n";
for (int i = 0; i <= S; i++){
cout << cont[i].size() << ": ";
for (int next : cont[i]){
cout << next << ' ';
}
cout << '\n';
for (int next : psums[i]){
cout << next << " ";
}
cout << '\n';
}*/
for (int i = 0; i < S; i++){
dcdp(0,S,0,S);
tot = 0;
for (int j = 0; j <= S; j++){
/// cout << tres[j] << " ";
tot = max(tot,tres[j]);
}
// cout << '\n';
cout << tot << '\n';
swap(tres,res);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
51292 KB |
Output is correct |
2 |
Incorrect |
8 ms |
51208 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
51292 KB |
Output is correct |
2 |
Incorrect |
8 ms |
51208 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
51292 KB |
Output is correct |
2 |
Incorrect |
8 ms |
51208 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |