# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
286472 | evpipis | Towns (IOI15_towns) | C++11 | 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 "wombats.h"
#include <bits/stdc++.h>
using namespace std;
const int len_n = 5005, len_m = 205, inf = 1e9+10;
int hor[len_n][len_m], ver[len_n][len_m], n, m, k;
struct{
struct node{
int to[len_m][len_m];
node(){
for (int a = 0; a < m; a++)
for (int b = 0; b < m; b++)
to[a][b] = inf; // check this
}
node(int i){
for (int a = 0, suma = 0; a < m; a++){
for (int b = 0, dif = suma; b < m; b++){
to[a][b] = dif + ver[i][b];
if (b < a)
dif -= hor[i][b];
else
dif += hor[i][b];
}
suma += hor[i][a];
}
}
};
node buck[75], root;
node join(node fir, node sec){
node res;
queue<tuple<int, int, int, int> > myq;
for (int a = 0; a < m; a++){
myq.push(make_tuple(0, m-1, 0, m-1));
while (!myq.empty()){
tuple<int, int, int, int> fron = myq.front();
myq.pop();
int l = get<0>(fron), r = get<1>(fron), o1 = get<2>(fron), o2 = get<3>(fron);
int b = (l+r)/2, opt;
for (int c = o1; c <= o2; c++)
if (fir.to[a][c]+sec.to[c][b] < res.to[a][b])
res.to[a][b] = fir.to[a][c]+sec.to[c][b], opt = c;
if (l < b)
myq.push(make_tuple(l, b-1, o1, opt));
if (b < r)
myq.push(make_tuple(b+1, r, opt, o2));
}
}
return res;
}
void upd_buck(int b){
int st = b*k, en = min(n, (b+1)*k);
buck[b] = node(st);
for (int i = st+1; i < en; i++)
buck[b] = join(buck[b], node(i));
}
void upd_root(){
root = buck[0];
for (int b = 1; b*k < n; b++)
root = join(root, buck[b]);
}
int ask(int a, int b){
return root.to[a][b];
}
} data;
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++)
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];
k = sqrt(n);
for (int b = 0; k*b < n; b++)
data.upd_buck(b);
data.upd_root();
}
void changeH(int P, int Q, int W) {
hor[P][Q] = W;
data.upd_buck(P/k);
data.upd_root();
}
void changeV(int P, int Q, int W) {
ver[P][Q] = W;
data.upd_buck(P/k);
data.upd_root();
}
int escape(int V1, int V2) {
return data.ask(V1, V2);
}