//#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 < n; i++){
fill(v[i].begin(), v[i].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);
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 = int(sqrt(n));
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 < max(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];
}
#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 < n; i++){
fill(v[i].begin(), v[i].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);
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 = int(sqrt(n));
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 < max(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 wombats.cpp:217:
grader.c: In function 'int main()':
grader.c:15:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
15 | int res;
| ^~~
wombats.cpp: At global scope:
wombats.cpp:228:11: error: redefinition of 'const int INF'
228 | const int INF = 1e9;
| ^~~
wombats.cpp:12:11: note: 'const int INF' previously defined here
12 | const int INF = 1e9;
| ^~~
wombats.cpp:229:11: error: redefinition of 'const int MAXN'
229 | const int MAXN = 200;
| ^~~~
wombats.cpp:13:11: note: 'const int MAXN' previously defined here
13 | const int MAXN = 200;
| ^~~~
wombats.cpp:234:8: error: redefinition of 'struct matriz'
234 | struct matriz{
| ^~~~~~
wombats.cpp:18:8: note: previous definition of 'struct matriz'
18 | struct matriz{
| ^~~~~~
wombats.cpp:286:8: error: redefinition of 'struct SegTree'
286 | struct SegTree{
| ^~~~~~~
wombats.cpp:70:8: note: previous definition of 'struct SegTree'
70 | struct SegTree{
| ^~~~~~~
wombats.cpp:319:2: error: conflicting declaration 'int st'
319 | }st;
| ^~
wombats.cpp:103:2: note: previous declaration as 'SegTree st'
103 | }st;
| ^~
wombats.cpp:321:5: error: redefinition of 'int n'
321 | int n, m;
| ^
wombats.cpp:105:5: note: 'int n' previously declared here
105 | int n, m;
| ^
wombats.cpp:321:8: error: redefinition of 'int m'
321 | int n, m;
| ^
wombats.cpp:105:8: note: 'int m' previously declared here
105 | int n, m;
| ^
wombats.cpp:322:5: error: redefinition of 'int len'
322 | int len, bct;
| ^~~
wombats.cpp:106:5: note: 'int len' previously declared here
106 | int len, bct;
| ^~~
wombats.cpp:322:10: error: redefinition of 'int bct'
322 | int len, bct;
| ^~~
wombats.cpp:106:10: note: 'int bct' previously declared here
106 | int len, bct;
| ^~~
wombats.cpp:324:13: error: redefinition of 'matrix<int> hor'
324 | matrix<int> hor;
| ^~~
wombats.cpp:108:13: note: 'matrix<int> hor' previously declared here
108 | matrix<int> hor;
| ^~~
wombats.cpp:325:13: error: redefinition of 'matrix<int> ver'
325 | matrix<int> ver;
| ^~~
wombats.cpp:109:13: note: 'matrix<int> ver' previously declared here
109 | matrix<int> ver;
| ^~~
wombats.cpp:327:8: error: redefinition of 'matriz build_in_row(int)'
327 | matriz build_in_row(int linha){
| ^~~~~~~~~~~~
wombats.cpp:111:8: note: 'matriz build_in_row(int)' previously defined here
111 | matriz build_in_row(int linha){
| ^~~~~~~~~~~~
wombats.cpp:340:8: error: redefinition of 'matriz build_next(int, matriz&, matriz&)'
340 | matriz build_next(int linha, matriz& antes, matriz& depois){
| ^~~~~~~~~~
wombats.cpp:124:8: note: 'matriz build_next(int, matriz&, matriz&)' previously defined here
124 | matriz build_next(int linha, matriz& antes, matriz& depois){
| ^~~~~~~~~~
wombats.cpp:349:6: error: redefinition of 'void buildblock(int)'
349 | void buildblock(int bid){
| ^~~~~~~~~~
wombats.cpp:133:6: note: 'void buildblock(int)' previously defined here
133 | void buildblock(int bid){
| ^~~~~~~~~~
wombats.cpp:379:6: error: redefinition of 'void init(int, int, int (*)[200], int (*)[200])'
379 | void init(int R, int C, int H[5000][200], int V[5000][200]) {
| ^~~~
wombats.cpp:163:6: note: 'void init(int, int, int (*)[200], int (*)[200])' previously defined here
163 | void init(int R, int C, int H[5000][200], int V[5000][200]) {
| ^~~~
wombats.cpp:410:6: error: redefinition of 'void changeH(int, int, int)'
410 | void changeH(int P, int Q, int W) {
| ^~~~~~~
wombats.cpp:194:6: note: 'void changeH(int, int, int)' previously defined here
194 | void changeH(int P, int Q, int W) {
| ^~~~~~~
wombats.cpp:419:6: error: redefinition of 'void changeV(int, int, int)'
419 | void changeV(int P, int Q, int W) {
| ^~~~~~~
wombats.cpp:203:6: note: 'void changeV(int, int, int)' previously defined here
203 | void changeV(int P, int Q, int W) {
| ^~~~~~~
wombats.cpp:427:5: error: redefinition of 'int escape(int, int)'
427 | int escape(int V1, int V2) {
| ^~~~~~
wombats.cpp:211:5: note: 'int escape(int, int)' previously defined here
211 | int escape(int V1, int V2) {
| ^~~~~~