#include "wombats.h"
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
const int inf=1e9;
int dist[300][202][202],costv[5005][202],costh[5005][200];
int n,m;
int pref[202],root,bound;
int ans[202][202],what[202][202];
void mul(vector<vector<int>>a,vector<vector<int>>b){
//knuth's op
vector<vector<int>>opt(m,vector<int>(m,-1));
for(int i=0;i<m;i++)for(int j=0;j<m;j++)what[i][j]=inf,opt[i][i]=i;
for(int i=0;i<m;i++){
for(int j=m-1;j>=0;j--){
int x=0,y=m-1;
if(i&&opt[i-1][j]!=-1)x=opt[i-1][j];
if(j+1<m&&opt[i][j+1]!=-1)y=opt[i][j+1];
for(int k=x;k<=y;k++)if(what[i][j]>a[i][k]+b[k][j]){
what[i][j]=a[i][k]+b[k][j];
opt[i][j]=k;
}
}
}
}
struct seg{
vector<vector<int>>v[1000];
void build(int l,int r,int pos){
v[pos].resize(m,vector<int>(m));
int mid=l+(r-l)/2;
if(l==r){
for(int i=0;i<m;i++)for(int j=0;j<m;j++)v[pos][i][j]=dist[l][i][j];
return;
}
build(l,mid,pos*2);
build(mid+1,r,pos*2+1);
mul(v[pos*2],v[pos*2+1]);
for(int i=0;i<m;i++)for(int j=0;j<m;j++)v[pos][i][j]=what[i][j];
}
void update(int l,int r,int qpos,int pos){
int mid=l+(r-l)/2;
if(l==r){
for(int i=0;i<m;i++)for(int j=0;j<m;j++)v[pos][i][j]=dist[l][i][j];
return;
}
if(qpos<=mid)update(l,mid,qpos,pos*2);
else update(mid+1,r,qpos,pos*2+1);
mul(v[pos*2],v[pos*2+1]);
for(int i=0;i<m;i++)for(int j=0;j<m;j++)v[pos][i][j]=what[i][j];
}
}t;
void cal(int id){
for(int st=0;st<m;st++)for(int go=0;go<m;go++){
if(st!=go)dist[id][st][go]=inf;
else dist[id][st][go]=0;
}
for(int r=(id*root);r<min(n,(id*root)+root);r++)for(int st=0;st<m;st++){
int mncost=inf,add=0;
for(int go=0;go<m;go++){
mncost=min(mncost,dist[id][st][go]-add);
pref[go]=mncost+add;
add+=costh[r][go];
}
mncost=inf,add=0;
for(int go=m-1;go>=0;go--){
mncost=min(mncost,dist[id][st][go]-add);
dist[id][st][go]=min(pref[go],mncost+add);
if(r!=n-1)dist[id][st][go]+=costv[r][go];
if(go)add+=costh[r][go-1];
}
}
}
void init(int R, int C, int H[5000][200], int V[5000][200]){
n=R,m=C;
//root=sqrt(n);
root=60;
bound=((n-1)/root);
for(int i=0;i<n;i++)for(int j=0;j<m-1;j++)costh[i][j]=H[i][j];
for(int i=0;i<n-1;i++)for(int j=0;j<m;j++)costv[i][j]=V[i][j];
for(int i=0;i<=bound;i++)cal(i);
t.build(0,bound,1);
}
void changeH(int P, int Q, int W){
costh[P][Q]=W;
cal(P/root);
t.update(0,bound,P/root,1);
}
void changeV(int P, int Q, int W){
costv[P][Q]=W;
cal(P/root);
t.update(0,bound,P/root,1);
}
int escape(int V1, int V2){
return t.v[1][V1][V2];
}
//70x200x200x500
/*
we can compute [start][at][row]
=200x200x5000
for each update
how to improve?
use segtree? and matrix [from][go] 200x200
too much memory?
do sqrt decomp instead? trading time for space
split block
what if we combine both sqrt and segtree 'o'?
we can do qry in
*/
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;
| ^~~
wombats.cpp:33:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
33 | #pragma GCC optimize ("03,unroll-lopps")
| ^
wombats.cpp:39:51: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
39 | void mul(vector<vector<int>>a,vector<vector<int>>b){
| ^
wombats.cpp:57:35: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
57 | void build(int l,int r,int pos){
| ^
wombats.cpp:69:45: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
69 | void update(int l,int r,int qpos,int pos){
| ^
wombats.cpp:81:16: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
81 | void cal(int id){
| ^
wombats.cpp:102:59: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
102 | void init(int R, int C, int H[5000][200], int V[5000][200]){
| ^
wombats.cpp:113:33: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
113 | void changeH(int P, int Q, int W){
| ^
wombats.cpp:118:33: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
118 | void changeV(int P, int Q, int W){
| ^
wombats.cpp:123:26: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
123 | int escape(int V1, int V2){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8540 KB |
Output is correct |
2 |
Correct |
7 ms |
8560 KB |
Output is correct |
3 |
Correct |
43 ms |
11348 KB |
Output is correct |
4 |
Correct |
5 ms |
8536 KB |
Output is correct |
5 |
Correct |
6 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
44 ms |
2856 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
1372 KB |
Output is correct |
2 |
Correct |
162 ms |
1368 KB |
Output is correct |
3 |
Correct |
109 ms |
1368 KB |
Output is correct |
4 |
Correct |
105 ms |
1372 KB |
Output is correct |
5 |
Correct |
117 ms |
1624 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
617 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16472 KB |
Output is correct |
2 |
Correct |
7 ms |
16476 KB |
Output is correct |
3 |
Correct |
10 ms |
16476 KB |
Output is correct |
4 |
Correct |
29 ms |
18000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
1372 KB |
Output is correct |
2 |
Correct |
115 ms |
1116 KB |
Output is correct |
3 |
Correct |
108 ms |
1300 KB |
Output is correct |
4 |
Correct |
116 ms |
1368 KB |
Output is correct |
5 |
Correct |
153 ms |
1372 KB |
Output is correct |
6 |
Correct |
8 ms |
16476 KB |
Output is correct |
7 |
Correct |
8 ms |
16476 KB |
Output is correct |
8 |
Correct |
9 ms |
16536 KB |
Output is correct |
9 |
Correct |
51 ms |
18004 KB |
Output is correct |
10 |
Correct |
5 ms |
8536 KB |
Output is correct |
11 |
Correct |
4 ms |
8536 KB |
Output is correct |
12 |
Correct |
45 ms |
11344 KB |
Output is correct |
13 |
Correct |
7 ms |
8536 KB |
Output is correct |
14 |
Correct |
5 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
840 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
484 KB |
Output is correct |
25 |
Correct |
40 ms |
2720 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
577 ms |
1372 KB |
Output is correct |
28 |
Correct |
1022 ms |
34752 KB |
Output is correct |
29 |
Correct |
1008 ms |
29008 KB |
Output is correct |
30 |
Correct |
915 ms |
28904 KB |
Output is correct |
31 |
Correct |
1014 ms |
36304 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
1368 KB |
Output is correct |
2 |
Correct |
119 ms |
1372 KB |
Output is correct |
3 |
Correct |
144 ms |
1372 KB |
Output is correct |
4 |
Correct |
141 ms |
1368 KB |
Output is correct |
5 |
Correct |
118 ms |
1368 KB |
Output is correct |
6 |
Correct |
9 ms |
16472 KB |
Output is correct |
7 |
Correct |
8 ms |
16476 KB |
Output is correct |
8 |
Correct |
9 ms |
16476 KB |
Output is correct |
9 |
Correct |
51 ms |
18000 KB |
Output is correct |
10 |
Correct |
4 ms |
8540 KB |
Output is correct |
11 |
Correct |
4 ms |
8540 KB |
Output is correct |
12 |
Correct |
43 ms |
11528 KB |
Output is correct |
13 |
Correct |
7 ms |
8540 KB |
Output is correct |
14 |
Correct |
5 ms |
8540 KB |
Output is correct |
15 |
Correct |
560 ms |
67432 KB |
Output is correct |
16 |
Correct |
3789 ms |
68616 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
40 ms |
2764 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
640 ms |
1372 KB |
Output is correct |
30 |
Correct |
951 ms |
34660 KB |
Output is correct |
31 |
Correct |
3751 ms |
66064 KB |
Output is correct |
32 |
Correct |
3779 ms |
66064 KB |
Output is correct |
33 |
Correct |
924 ms |
28944 KB |
Output is correct |
34 |
Correct |
3624 ms |
55412 KB |
Output is correct |
35 |
Correct |
979 ms |
28988 KB |
Output is correct |
36 |
Correct |
3518 ms |
55324 KB |
Output is correct |
37 |
Correct |
954 ms |
36592 KB |
Output is correct |
38 |
Correct |
3627 ms |
66672 KB |
Output is correct |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Correct |
3635 ms |
55500 KB |
Output is correct |