# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
537948 | couplefire | Wombats (IOI13_wombats) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
const int M = 205;
const int MX = 1010;
const int INF = 0x3f3f3f3f;
int n, m, q;
int vert[N][M], hori[N][M], shori[N][M];
int seg[MX][M][M], best[M][M];
void calc(int f[M][M], int tl, int tr){
memset(f, INF, sizeof(int)*M*M);
for(int i = 1; i<=m; ++i)
for(int j = tl; j<=tr; ++j){
for(int k = 1; k<=m; ++k)
f[i][k] = (j==tl?abs(shori[tl][i]-shori[tl][k]):f[i][k]+vert[j-1][k]);
int lmn = f[i][1];
for(int k = 2; k<=m; ++k)
f[i][k] = min(f[i][k], lmn+shori[j][k]),
lmn = min(lmn, f[i][k]-shori[j][k]);
int rmn = f[i][m]+shori[j][m];
for(int k = m-1; k>=1; --k)
f[i][k] = min(f[i][k], rmn-shori[j][k]),
rmn = min(rmn, f[i][k]+shori[j][k]);
}
}
void comb(int f[M][M], int lf[M][M], int rf[M][M], int bruh[M]){
for(int i = 1; i<=m; ++i)
for(int j = m; j>=1; --j){
int l = 1, r = m;
if(i>1) l = best[i-1][j];
if(j<m) r = best[i][j+1];
f[i][j] = INF;
for(int k = l; k<=r; ++k)
if(lf[i][k]+rf[k][j]+bruh[k]<f[i][j])
best[i][j] = k, f[i][j] = lf[i][k]+rf[k][j]+bruh[k];
}
}
void build(int v, int tl, int tr){
if(tr-tl<=16)
return calc(seg[v], tl, tr);
int tm = (tl+tr)>>1;
build(v<<1, tl, tm);
build(v<<1|1, tm+1, tr);
comb(seg[v], seg[v<<1], seg[v<<1|1], hori[tm]);
}
void upd(int pos, int v, int tl, int tr){
if(tr-tl<=16)
return calc(seg[v], tl, tr);
int tm = (tl+tr)>>1;
if(pos<=tm) upd(pos, v<<1, tl, tm);
else upd(pos, v<<1|1, tm+1, tr);
comb(seg[v], seg[v<<1], seg[v<<1|1], hori[tm]);
}
int main(){
// freopen("a.in", "r", stdin);
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for(int i = 1; i<=n; ++i)
for(int j = 1; j<m; ++j)
cin >> hori[i][j], shori[i][j+1] = shori[i][j]+hori[i][j];
for(int i = 1; i<n; ++i)
for(int j = 1; j<=m; ++j)
cin >> vert[i][j];
build(1, 1, n);
cin >> q;
while(q--){
int t, x, y;
cin >> t >> x >> y;
++x; ++y;
if(t==1){
int val; cin >> val;
int dif = val-hori[x][y];
hori[x][y] = val;
for(int i = y+1; i<=m; ++i)
shori[x][i] += dif;
upd(x, 1, 1, n);
}
else if(t==2){
int val; cin >> val;
vert[x][y] = val;
upd(x, 1, 1, n);
}
else
cout << seg[1][x][y] << '\n';
}
}