// #include "grader.c"
#include"wombats.h"
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
using namespace std;
using ll = long long;
using pii = pair<int,int>;
template<typename T>
using matrix = vector<vector<T>>;
const int INF = 1e9;
const int MAXN = 200;
template<typename T>
using mat = array<array<int,MAXN>,MAXN>;
struct matriz{
mat<int> v;
int n;
matriz(int sz = 0, int val = INF){
n = sz;
for(int i = 0; i < sz; i++){
fill(v.begin(), v.begin()+n, val);
}
}
matriz operator*(matriz & other){
matriz ret(n);
auto solve =[&](int source, int l, int r, int optl, int optr, auto solve) -> void {
if(r < l)
return;
int m = (l+r)>>1;
int opt = optl;
for (int i = optl+1; i <= optr; i++)
opt = min(opt,i,[&](int a, int b){
return v[source][a]+other.v[a][m] < v[source][b]+other.v[b][m];
});
ret.v[source][m] = min(INF,v[source][opt]+other.v[opt][m]);
solve(source,m+1,r,opt,optr,solve);
solve(source,l,m-1,optl,opt,solve);
};
for(int i = 0; i < n; i++)
solve(i,0,n-1,0,n-1,solve);
// for(int i = 0; i < n; i++){
// for(int j = 0; j < n; j++){
// for(int k = 0; k < n; k++){
// ret.v[i][j] = min(v[i][k]+other.v[k][j], ret.v[i][j]);
// }
// }
// }
return ret;
}
void swap(matriz& other){
v.swap(other.v);
}
};
struct SegTree{
vector<matriz> tree;
int sz, q;
matriz val;
SegTree(int n = 0, int m = 0){
sz = n;
tree = vector<matriz>(4*n,matriz(m));
}
void update(int id, int l, int r){
if(l > q || r < q)
return;
if(l == q && r == q){
tree[id] = val;
return;
}
int e = id*2+1, d = id*2+2, m =(l+r)>>1;
update(e,l,m);
update(d,m+1,r);
tree[id] = tree[e]*tree[d];
}
void makeupd(int id, matriz v){
val = v;
q = id;
update(0,0,sz-1);
}
}st;
int n, m;
int len, bct;
matrix<int> hor;
matrix<int> ver;
matriz build_in_row(int linha){
matriz ret(m,0);
for(int j = 0; j < m; j++){
int cur = 0;
for(int k = j; k < m-1; k++){
cur+=hor[linha][k];
ret.v[j][k+1] = ret.v[k+1][j] = cur;
}
}
return ret;
}
matriz build_next(int linha, matriz& antes, matriz& depois){
matriz col(m);
for(int i = 0; i < m; i++)
col.v[i][i] = ver[linha][i];
return antes*col*depois;
}
void buildblock(int bid){
int l = bid*len, r = min((bid+1)*len,n-1);
// cerr << bid << ' ' << l << ' ' << r << endl;
matriz antes = build_in_row(l), depois(m);
matriz res(m,INF);
for(int i = 0; i < m; i++)
res.v[i][i] = 0;
for(int i = l; i < r; i++){
depois = build_in_row(i+1);
matriz cur = build_next(i,antes,depois);
res = res*cur;
antes.swap(depois);
}
st.makeupd(bid,res);
// cerr << l << ' ' << r << ":\n";
// for(int i = 0; i < m; i++){
// for(int j = 0; j < m; j++){
// cerr << res.v[i][j] << ' ';
// }
// cerr << '\n';
// }
}
void init(int R, int C, int H[5000][200], int V[5000][200]) {
// cerr << "ok" << endl;
n = R;
m = C;
// cerr << n << ' ' << m << endl;
hor = matrix<int>(n,vector<int>(m-1));
ver = matrix<int>(n-1,vector<int>(m));
// cerr << "ok" << endl;
for(int i = 0; i < n; i++){
for(int j = 0; j < m-1; j++)
hor[i][j] = H[i][j];
}
for(int i = 0; i < n-1; i++){
for(int j = 0; j < m; j++){
ver[i][j] = V[i][j];
}
}
len = 10;
bct = (n-2+len)/len;
st = SegTree(bct,m);
for(int i = 0; i < bct; i++){
buildblock(i);
}
}
void changeH(int P, int Q, int W) {
// cerr << P << ' ' << Q << endl;
hor[P][Q] = W;
int bid = P/len;
for(int i = max(0,bid-1); i < min(bid+1,bct); i++)
buildblock(i);
}
void changeV(int P, int Q, int W) {
// cerr << P << ' ' << Q << endl;
ver[P][Q] = W;
int bid = P/len;
buildblock(bid);
}
int escape(int V1, int V2) {
// cerr << V1 << ' ' << V2 << endl;
return st.tree[0].v[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;
| ^~~
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from wombats.cpp:3:
/usr/include/c++/10/bits/stl_algobase.h: In instantiation of 'typename __gnu_cxx::__enable_if<std::__is_scalar<_Tp>::__value, void>::__type std::__fill_a1(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = std::array<int, 200>*; _Tp = int; typename __gnu_cxx::__enable_if<std::__is_scalar<_Tp>::__value, void>::__type = void]':
/usr/include/c++/10/bits/stl_algobase.h:914:21: required from 'void std::__fill_a(_FIte, _FIte, const _Tp&) [with _FIte = std::array<int, 200>*; _Tp = int]'
/usr/include/c++/10/bits/stl_algobase.h:944:20: required from 'void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = std::array<int, 200>*; _Tp = int]'
wombats.cpp:24:45: required from here
/usr/include/c++/10/bits/stl_algobase.h:873:11: error: no match for 'operator=' (operand types are 'std::array<int, 200>' and 'const int')
873 | *__first = __tmp;
| ~~~~~~~~~^~~~~~~
In file included from /usr/include/c++/10/tuple:39,
from /usr/include/c++/10/functional:54,
from /usr/include/c++/10/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/10/algorithm:74,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from wombats.cpp:3:
/usr/include/c++/10/array:94:12: note: candidate: 'constexpr std::array<int, 200>& std::array<int, 200>::operator=(const std::array<int, 200>&)'
94 | struct array
| ^~~~~
/usr/include/c++/10/array:94:12: note: no known conversion for argument 1 from 'const int' to 'const std::array<int, 200>&'
/usr/include/c++/10/array:94:12: note: candidate: 'constexpr std::array<int, 200>& std::array<int, 200>::operator=(std::array<int, 200>&&)'
/usr/include/c++/10/array:94:12: note: no known conversion for argument 1 from 'const int' to 'std::array<int, 200>&&'