/* □駒□駒□ 駒□□□駒 □□×□□ 駒□□□駒 □駒□駒□ K=2 になるようなサブエリア例 □■■■□ □■■■□ □■■■□ □□□□□ □□□□□ K=2 になるようなサブエリア例 □□■■■ □□■■■ □□■■■ □□□□□ □□□□□ K=2 になるようなサブエリア例 □□■■□ □□■■□ □□■■□ □□■■□ □□■■□ */
(あとで)
↓あとで (accepted in practice)
class HyperKnight { public: long long countCells(int A, int B, int NR, int NC, int K) { VVI pat(5, VI(5, 0)); int eX[] = {0,0,1,1,3,3,4,4}; int eY[] = {1,3,0,4,0,4,1,3}; REP(i, 8) pat[eY[i]][eX[i]]=1; //cout<<pat<<endl; if(B>A) swap(A, B); ll ans = 0; REP(y0, 5) { REP(x0, 5) { RANGE(y1, y0, 5) { RANGE(x1, x0, 5) { if(x1<2||2<x0||y1<2||2<y0) continue; int co=0; RANGE(y, y0, y1+1) RANGE(x, x0, x1+1) co+=pat[y][x]; //cout<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<" ++ "<<co<<endl; if(co==K) { //cout<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<" --- "<<endl; int tb[] = {0, B, A, A+1}; int a0=x0, a1=x1, N=NC; ll cc[2]; int ok=1; REP(loop, 2) { int i0 = abs(a0-2); int i1 = abs(a1-2); ll cmin0 = tb[i0]; ll cmin1 = tb[i1]; ll cmax0 = tb[i0+1]-1; ll cmax1 = tb[i1+1]-1; // ながさ in [c0, c1] ll c0 = cmin0 + 1 + cmin1; ll c1 = cmax0 + 1 + cmax1; if(!(i0==2||i1==2)) { if(!(c0<=N&&N<=c1)) ok=0; } if(c0>N) ok=0; c1 = min((ll)N, c1); //cout<<c0<<" "<<c1<<endl; ll c = 0; if(i0==2&&i1==2) c = N-c0+1; else c = c1-c0+1; a0=y0, a1=y1, N=NR; cc[loop] = c; } if(ok) { ans += cc[0] * cc[1]; //cout<<x0<<" "<<y0<<" "<<x1<<" "<<y1<<" --- "<<cc[0]<<" "<<cc[1]<<endl; } } } } } } return ans; } };