#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include "wombats.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn=5000+10,maxm=200+10,t=25,mt=(maxn/t)+5,lg=log2(mt)+1;
int n,m,inf=1e9+5;
struct bakhsh{
int allr[t+5][maxm],allc[t+5][maxm],dp[maxm][maxm],ps[maxm],suf[maxm],mxr=0;
void calps(int i){
for(int j=1;j<=m;j++){
ps[j]=ps[j-1]+allr[i][j-1];
}
}
void calc(){
for(int k=1;k<=m;k++){
calps(0);
for(int j=1;j<=m;j++){
dp[k][j]=abs(ps[k]-ps[j]);
}
for(int i=1;i<=mxr;i++){
int mn=inf;
for(int j=1;j<=m;j++){
mn=min(mn+allr[i][j-1],dp[k][j]+allc[i-1][j]);
ps[j]=mn;
}
mn=inf;
for(int j=m;j>=1;j--){
mn=min(mn+allr[i][j],dp[k][j]+allc[i-1][j]);
dp[k][j]=min(ps[j],mn);
}
}
for(int j=1;j<=m;j++){
dp[k][j]+=allc[mxr][j];
}
}
}
void updr(int i,int j,int w){
j++;
mxr=max(mxr,i);
allr[i][j]=w;
}
void updc(int i,int j,int w){
j++;
mxr=max(mxr,i);
allc[i][j]=w;
}
}allsq[mt];
int kaf=(1<<lg);
struct segment{
int seg[(1<<(lg+1))][maxm][maxm];
int now[maxm][maxm];
segment(){
for(int i=0;i<(1<<(lg+1));i++){
for(int r=0;r<maxm;r++){
for(int c=0;c<maxm;c++){
seg[i][r][c]=inf;
}
}
}
}
pair<int,int>stf[maxm][maxm];
void por(int ind,int ez[maxm][maxm]){
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++){
seg[ind][i][j]=ez[i][j];
}
}
}
vector<pair<int,int>>allv;
void dfs(int cl,int cr,int r=m,int c=1){
allv.clear();
allv.push_back(make_pair(r,c));
int l=0;
while(l<(int)allv.size()){
r=allv[l].first;
c=allv[l].second;
l++;
if(r==0||c==m+1){
continue;
}
if(r==m){
stf[r][c].second=m;
}
if(c==1){
stf[r][c].first=1;
}
now[r][c]=inf;
int opt=stf[r][c].first;
for(int i=stf[r][c].first;i<=stf[r][c].second;i++){
if(seg[cl][r][i]+seg[cr][i][c]<now[r][c]){
opt=i;
now[r][c]=seg[cl][r][i]+seg[cr][i][c];
}
}
stf[r-1][c].second=opt;
stf[r][c+1].first=opt;
allv.push_back(make_pair(r-1,c));
if(r==m){
allv.push_back(make_pair(r,c+1));
}
}
}
void calc(int i){
if(seg[(i<<1)][1][1]==inf){
por(i,seg[(i<<1)^1]);
return ;
}
if(seg[(i<<1)^1][1][1]==inf){
por(i,seg[(i<<1)]);
return ;
}
dfs((i<<1),(i<<1)^1);
por(i,now);
}
void upd(int i,int dp[maxm][maxm]){
i+=kaf;
por(i,dp);
i>>=1;
while(i>0){
calc(i);
i>>=1;
}
}
}seg;
void init( int R, int C, int H[5000][200], int V[5000][200]) {
n=R;
m=C;
for(int i=0;i<n;i++){
for(int j=0;j<m-1;j++){
allsq[i/t].updr(i%t,j,H[i][j]);
}
}
for(int i=0;i<n-1;i++){
for(int j=0;j<m;j++){
allsq[i/t].updc(i%t,j,V[i][j]);
}
}
for(int i=0;i<=(n-1)/t;i++){
allsq[i].calc();
seg.upd(i,allsq[i].dp);
}
}
void changeH( int P, int Q, int W) {
allsq[P/t].updr(P%t,Q,W);
allsq[P/t].calc();
seg.upd(P/t,allsq[P/t].dp);
}
void changeV( int P, int Q, int W) {
allsq[P/t].updc(P%t,Q,W);
allsq[P/t].calc();
seg.upd(P/t,allsq[P/t].dp);
}
int escape( int V1, int V2) {
V1++;
V2++;
return seg.seg[1][V1][V2];
}
Compilation message
grader.c: In function 'int main()':
grader.c:15:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
15 | int res;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
101344 KB |
Output is correct |
2 |
Correct |
44 ms |
101292 KB |
Output is correct |
3 |
Correct |
85 ms |
104240 KB |
Output is correct |
4 |
Correct |
43 ms |
101456 KB |
Output is correct |
5 |
Correct |
44 ms |
101460 KB |
Output is correct |
6 |
Correct |
38 ms |
91748 KB |
Output is correct |
7 |
Correct |
38 ms |
91732 KB |
Output is correct |
8 |
Correct |
38 ms |
91732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
91736 KB |
Output is correct |
2 |
Correct |
37 ms |
91736 KB |
Output is correct |
3 |
Correct |
38 ms |
91712 KB |
Output is correct |
4 |
Correct |
38 ms |
91984 KB |
Output is correct |
5 |
Correct |
39 ms |
91992 KB |
Output is correct |
6 |
Correct |
49 ms |
91732 KB |
Output is correct |
7 |
Correct |
51 ms |
91984 KB |
Output is correct |
8 |
Correct |
38 ms |
91756 KB |
Output is correct |
9 |
Correct |
38 ms |
91972 KB |
Output is correct |
10 |
Correct |
38 ms |
91988 KB |
Output is correct |
11 |
Correct |
75 ms |
94284 KB |
Output is correct |
12 |
Correct |
38 ms |
91988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
92240 KB |
Output is correct |
2 |
Correct |
93 ms |
92248 KB |
Output is correct |
3 |
Correct |
96 ms |
92264 KB |
Output is correct |
4 |
Correct |
96 ms |
92240 KB |
Output is correct |
5 |
Correct |
102 ms |
92244 KB |
Output is correct |
6 |
Correct |
44 ms |
91732 KB |
Output is correct |
7 |
Correct |
38 ms |
91736 KB |
Output is correct |
8 |
Correct |
38 ms |
91896 KB |
Output is correct |
9 |
Correct |
309 ms |
92240 KB |
Output is correct |
10 |
Correct |
38 ms |
91736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
109140 KB |
Output is correct |
2 |
Correct |
45 ms |
109136 KB |
Output is correct |
3 |
Correct |
46 ms |
109268 KB |
Output is correct |
4 |
Correct |
81 ms |
110672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
92248 KB |
Output is correct |
2 |
Correct |
94 ms |
92420 KB |
Output is correct |
3 |
Correct |
96 ms |
92244 KB |
Output is correct |
4 |
Correct |
98 ms |
92244 KB |
Output is correct |
5 |
Correct |
94 ms |
92400 KB |
Output is correct |
6 |
Correct |
46 ms |
109148 KB |
Output is correct |
7 |
Correct |
47 ms |
109204 KB |
Output is correct |
8 |
Correct |
46 ms |
109140 KB |
Output is correct |
9 |
Correct |
65 ms |
110672 KB |
Output is correct |
10 |
Correct |
43 ms |
101460 KB |
Output is correct |
11 |
Correct |
44 ms |
101460 KB |
Output is correct |
12 |
Correct |
77 ms |
104016 KB |
Output is correct |
13 |
Correct |
42 ms |
101456 KB |
Output is correct |
14 |
Correct |
45 ms |
101460 KB |
Output is correct |
15 |
Correct |
38 ms |
91732 KB |
Output is correct |
16 |
Correct |
39 ms |
91732 KB |
Output is correct |
17 |
Correct |
38 ms |
91728 KB |
Output is correct |
18 |
Correct |
41 ms |
91988 KB |
Output is correct |
19 |
Correct |
38 ms |
91984 KB |
Output is correct |
20 |
Correct |
37 ms |
91740 KB |
Output is correct |
21 |
Correct |
38 ms |
91732 KB |
Output is correct |
22 |
Correct |
38 ms |
91988 KB |
Output is correct |
23 |
Correct |
38 ms |
91984 KB |
Output is correct |
24 |
Correct |
40 ms |
91864 KB |
Output is correct |
25 |
Correct |
74 ms |
94288 KB |
Output is correct |
26 |
Correct |
39 ms |
91864 KB |
Output is correct |
27 |
Correct |
304 ms |
92448 KB |
Output is correct |
28 |
Correct |
763 ms |
129248 KB |
Output is correct |
29 |
Correct |
781 ms |
122708 KB |
Output is correct |
30 |
Correct |
738 ms |
122704 KB |
Output is correct |
31 |
Correct |
806 ms |
131156 KB |
Output is correct |
32 |
Correct |
38 ms |
91732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
92504 KB |
Output is correct |
2 |
Correct |
92 ms |
92244 KB |
Output is correct |
3 |
Correct |
95 ms |
92240 KB |
Output is correct |
4 |
Correct |
94 ms |
92404 KB |
Output is correct |
5 |
Correct |
95 ms |
92408 KB |
Output is correct |
6 |
Correct |
46 ms |
109140 KB |
Output is correct |
7 |
Correct |
46 ms |
109140 KB |
Output is correct |
8 |
Correct |
59 ms |
109180 KB |
Output is correct |
9 |
Correct |
65 ms |
110572 KB |
Output is correct |
10 |
Correct |
51 ms |
101224 KB |
Output is correct |
11 |
Correct |
45 ms |
101460 KB |
Output is correct |
12 |
Correct |
79 ms |
104148 KB |
Output is correct |
13 |
Correct |
42 ms |
101460 KB |
Output is correct |
14 |
Correct |
42 ms |
101460 KB |
Output is correct |
15 |
Correct |
732 ms |
151352 KB |
Output is correct |
16 |
Correct |
2950 ms |
152128 KB |
Output is correct |
17 |
Correct |
38 ms |
91732 KB |
Output is correct |
18 |
Correct |
38 ms |
91740 KB |
Output is correct |
19 |
Correct |
38 ms |
91740 KB |
Output is correct |
20 |
Correct |
38 ms |
91988 KB |
Output is correct |
21 |
Correct |
38 ms |
91984 KB |
Output is correct |
22 |
Correct |
39 ms |
91996 KB |
Output is correct |
23 |
Correct |
39 ms |
91984 KB |
Output is correct |
24 |
Correct |
38 ms |
91984 KB |
Output is correct |
25 |
Correct |
38 ms |
91988 KB |
Output is correct |
26 |
Correct |
38 ms |
91736 KB |
Output is correct |
27 |
Correct |
75 ms |
94288 KB |
Output is correct |
28 |
Correct |
43 ms |
91992 KB |
Output is correct |
29 |
Correct |
307 ms |
92244 KB |
Output is correct |
30 |
Correct |
757 ms |
129376 KB |
Output is correct |
31 |
Correct |
2751 ms |
149752 KB |
Output is correct |
32 |
Correct |
2893 ms |
149728 KB |
Output is correct |
33 |
Correct |
771 ms |
122976 KB |
Output is correct |
34 |
Correct |
2790 ms |
139980 KB |
Output is correct |
35 |
Correct |
737 ms |
122704 KB |
Output is correct |
36 |
Correct |
2842 ms |
139992 KB |
Output is correct |
37 |
Correct |
815 ms |
130852 KB |
Output is correct |
38 |
Correct |
2932 ms |
150276 KB |
Output is correct |
39 |
Correct |
40 ms |
91724 KB |
Output is correct |
40 |
Correct |
3052 ms |
140224 KB |
Output is correct |