#include "wombats.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
int row[10000][200];
ll row_pref[10000][201];
int column[10000][200];
pii opt[200][200];
int r,c;
struct node
{
    int l,r;
    int** ans;
    int type = 1;
    int get_ans(int p1, int p2)
    {
        if(type == 0) return ans[p1][p2];
        if(p1 > p2) swap(p1,p2);
        return min((ll)1e8,row_pref[l][p2] - row_pref[l][p1]);
    }
};
const int tree_siz = 1024*16-1;
node segtree[tree_siz+1];
void merge(node& left, node& right, node& ans)
{
    ans.l = left.l;
    ans.r = right.r;
    rep(i,c) rep(j,c) opt[i][j] = {1e9,0};
    rep(op,c)
    {
        opt[0][c-1] = min(opt[0][c-1],{left.get_ans(0,op) + column[left.r][op] + right.get_ans(op,c-1),op});
        opt[c-1][0] = min(opt[c-1][0],{left.get_ans(c-1,op) + column[left.r][op] + right.get_ans(op,0),op});
    }
    for(int len = c-2; len >= 0; len--)
    {
        rep(start,c-len)
        {
            int l_opt = 0;
            int r_opt = c-1;
            if(start != 0)
            {
                l_opt = opt[start-1][start+len].ss;
            }
            if(start+len != c-1)
            {
                r_opt = opt[start][start+len+1].ss;
            }
            rep2(op,l_opt,r_opt)
            {
                opt[start][start+len] = min(opt[start][start+len],{left.get_ans(start,op) + column[left.r][op] + right.get_ans(op,start+len),op});
            }
        }
        for(int start = c-1; start >= len; start--)
        {
            int l_opt = 0;
            int r_opt = c-1;
            if(start-len != 0)
            {
                l_opt = opt[start][start-len-1].ss;
            }
            if(start != c-1)
            {
                r_opt = opt[start+1][start-len].ss;
            }
            rep2(op,l_opt,r_opt)
            {
                opt[start][start-len] = min(opt[start][start-len],{left.get_ans(start,op) + column[left.r][op] + right.get_ans(op,start-len),op});
            }
        }
    }
    rep(i,c) rep(j,c) 
    {
        ans.ans[i][j] = opt[i][j].ff;
    }
}
void upd(int v)
{
    merge(segtree[v*2],segtree[v*2+1],segtree[v]);
    if(v != 1) upd(v/2);
}
void init(int R, int C, int H[5000][200], int V[5000][200]) 
{
    r = R;
    c = C;
    rep(i,10000) rep(j,c-1) row[i][j] = 1e8;
    rep(i,10000) rep2(j,1,c) row_pref[i][j] = row_pref[i][j-1] + row[i][j-1];
    rep(i,r) rep(j,c-1) row[i][j] = H[i][j];
    rep(i,r-1) rep(j,c) column[i][j] = V[i][j];
    rep(i,r)
    {
        int cur_sum = 0;
        rep(j,c-1)
        {
            cur_sum += row[i][j];
            row_pref[i][j+1] = cur_sum;
        }
    }
    rep2(i,tree_siz/2+1,tree_siz)
    {
        segtree[i].l = i-(tree_siz/2+1);
        segtree[i].r = i-(tree_siz/2+1);
        segtree[i].type = 1;
    }
    for(int i = tree_siz/2; i >= 1; i--)
    {
        segtree[i].type = 0;
        segtree[i].ans = new int*[c];
        rep(j,c)
        {
            segtree[i].ans[j] = new int[c];
        }
        merge(segtree[i*2],segtree[i*2+1],segtree[i]);
    }
}
void changeH(int P, int Q, int W) 
{
    row[P][Q] = W;
    int cur_sum = 0;
    rep(j,c-1)
    {
        cur_sum += row[P][j];
        row_pref[P][j+1] = cur_sum;
    }
    upd((tree_siz/2+P+1)/2);
}
void changeV(int P, int Q, int W) 
{
    column[P][Q] = W;
    upd((tree_siz/2+P+1)/2);
}
int escape(int V1, int V2) 
{
    return segtree[1].get_ans(V1,V2);
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |