[Wc2009]shortest
终于把这题过了,了了我两年前写堵塞的交通一晚上无果的心结
因为是6要注意蛇皮走位啊!!这种-> S
1 //Achen 2 #include<bits/stdc++.h> 3 #define For(i,a,b) for(int i=(a);i<=(b);i++) 4 #define Rep(i,a,b) for(int i=(a);i>=(b);i--) 5 #define Formylove return 0 6 const int N=1e5+7; 7 typedef long long LL; 8 typedef double db; 9 using namespace std; 10 int n,q,a[6][N],tpa[6],mx; 11 12 template<typename T> void read(T &x) { 13 char ch=getchar(); T f=1; x=0; 14 while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); 15 if(ch=='-') f=-1,ch=getchar(); 16 for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f; 17 } 18 19 #define inf 1e15 20 LL dl[N<<2][6][6],dr[N<<2][6][6],dm[N<<2][6][6]; 21 void copyit(int x,int y) { 22 For(i,0,5) For(j,0,5) dl[y][i][j]=dl[x][i][j],dr[y][i][j]=dr[x][i][j],dm[y][i][j]=dm[x][i][j]; 23 } 24 void upd(int y,int ll,int rr) { 25 int x=mx+5,lc=mx+6,rc=mx+7; 26 copyit(ll,lc); copyit(rr,rc); 27 For(i,0,5) For(j,0,5) For(k,0,5) { 28 dm[lc][i][j]=min(dm[lc][i][j],dm[lc][i][k]+dl[rc][k][j]); 29 dm[rc][i][j]=min(dm[rc][i][j],dm[rc][k][j]+dr[lc][k][i]); 30 } 31 For(i,0,5) For(j,0,5) { 32 dm[x][i][j]=inf; 33 For(k,0,5) dm[x][i][j]=min(dm[x][i][j],dm[lc][i][k]+dm[rc][k][j]); 34 } 35 For(i,0,5) For(j,0,5) { 36 dl[x][i][j]=dl[lc][i][j]; 37 dr[x][i][j]=dr[rc][i][j]; 38 For(k,0,5) dl[x][i][j]=min(dl[x][i][j],dm[lc][i][k]+dm[lc][j][k]); 39 For(k,0,5) dr[x][i][j]=min(dr[x][i][j],dm[rc][k][i]+dm[rc][k][j]); 40 } 41 copyit(x,y); 42 } 43 44 #define lc (x<<1) 45 #define rc ((x<<1)|1) 46 #define mid ((l+r)>>1) 47 void updone(int x,int l) { 48 For(i,0,5) tpa[i]=(i?tpa[i-1]:0)+a[i][l]; 49 For(i,0,5) For(j,0,5) { 50 LL ds=i>j?(tpa[i]-(j?tpa[j-1]:0)):(tpa[j]-(i?tpa[i-1]:0)); 51 dl[x][i][j]=dr[x][i][j]=dm[x][i][j]=ds; 52 } 53 } 54 55 void build(int x,int l,int r) { 56 if(l==r) { updone(x,l); return ; } 57 build(lc,l,mid); build(rc,mid+1,r); 58 upd(x,lc,rc); 59 } 60 61 void change(int x,int l,int r,int pos) { 62 if(l==r) { updone(x,l); return; } 63 if(pos<=mid) change(lc,l,mid,pos); 64 else change(rc,mid+1,r,pos); 65 upd(x,lc,rc); 66 } 67 68 int fl; 69 void qry(int x,int l,int r,int ql,int qr,int t) { 70 if(ql>qr) return ; 71 if(l>=ql&&r<=qr) { 72 if(!fl) copyit(x,t),fl=1; 73 else upd(t,t,x); return ; 74 } 75 if(ql<=mid) qry(lc,l,mid,ql,qr,t); 76 if(qr>mid) qry(rc,mid+1,r,ql,qr,t); 77 } 78 79 int main() { 80 //freopen("2104.in","r",stdin); 81 //freopen("2104.out","w",stdout); 82 read(n); mx=(n<<2); 83 For(i,0,5) For(j,1,n) read(a[i][j]); 84 build(1,1,n); 85 read(q); 86 For(i,1,q) { 87 int op,x,y,v,sx,sy,tx,ty; 88 read(op); 89 if(op==1) { 90 read(x); read(y); read(v); 91 a[x-1][y]=v; change(1,1,n,y); 92 } 93 else { 94 read(sx); read(sy); sx--; 95 read(tx); read(ty); tx--; 96 if(sy>ty) swap(sx,tx),swap(sy,ty); 97 fl=0; qry(1,1,n,sy,ty,mx+1); 98 fl=0; qry(1,1,n,1,sy-1,mx+2); 99 fl=0; qry(1,1,n,ty+1,n,mx+3); 100 LL ans=dm[mx+1][sx][tx]; 101 if(sy!=1) { 102 For(j,0,5) For(k,0,5) dr[mx+2][sx][j]=min(dr[mx+2][sx][j],dl[mx+1][sx][k]+dr[mx+2][k][j]-dl[mx+1][sx][sx]); 103 For(k,0,5) ans=min(ans,dl[mx+1][sx][sx]+dr[mx+2][sx][k]+dm[mx+1][k][tx]); 104 } 105 if(ty!=n) { 106 For(j,0,5) For(k,0,5) dl[mx+3][j][tx]=min(dl[mx+3][j][tx],dr[mx+1][k][tx]+dl[mx+3][j][k]-dr[mx+1][tx][tx]); 107 For(k,0,5) ans=min(ans,dm[mx+1][sx][k]+dl[mx+3][k][tx]+dr[mx+1][tx][tx]); 108 } 109 if(sy!=1&&ty!=n) { 110 For(k,0,5) For(l,0,5) 111 ans=min(ans,dr[mx+2][sx][k]+dm[mx+1][k][l]+dl[mx+3][l][tx]+dl[mx+1][sx][sx]+dr[mx+1][tx][tx]); 112 } 113 printf("%lld\n",ans); 114 } 115 } 116 Formylove; 117 }