#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<set<pii>> cont(mxS+1);
void dcdp(int l, int r, int sl, int sr){
int m = (l+r)/2;
tres[m] = 0;
int cadd = 0;
int bidx = -1;
auto it = cont[m].end();
if (cont[m].size() > 0){
it--;
}
for (int i = min(m-1,sr); i >= sl; i--){
if (cont[m].size() > 0){
if ((*it).f == i){
cadd += (*it).s;
if (it != cont[m].begin()){
it--;
}
}
}
if (res[i] + cadd > tres[m]){
tres[m] = res[i] + cadd;
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);
}
}
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++){
auto it = cont[j].lower_bound({bounds[i].f-1,-1});
if (it != cont[j].end() && (*it).f == bounds[i].f-1){
cont[j].insert({bounds[i].f-1,sums[i]+(*it).s});
cont[j].erase(it);
} else {
cont[j].insert({bounds[i].f-1,sums[i]});
}
}
}
memset(res,0,sizeof(res));
int tot;
/* cout << "RD\n";
for (int i = 0; i <= S; i++){
cout << cont[i].size() << ": ";
for (auto it = cont[i].begin(); it != cont[i].end(); it++){
cout << (*it).f << "," << (*it).s << " ";
}
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 |
Incorrect |
7 ms |
35676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
35676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
35676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |