#include #include #include #include #include using namespace std; ofstream fout ("regions.out"); ifstream fin ("regions.in"); int ASD=24; struct st { int x,y,z; }; struct Comp { bool operator()(const st& a,const st& b) { return a.zb.z; } }; struct Comp3 { bool operator()(const st& a,const st& b) { return (a.x+a.y+a.z)>(b.x+b.y+b.z); } }; struct Comp4 { bool operator()(const st& a,const st& b) { return (42*a.z+a.x*13+a.y*666)%1024>(42*b.z+b.x*13+b.y*666)%1024; } }; struct Comp5 { bool operator()(const st& a,const st& b) { return (42*a.z+a.x*13+a.y*666)%1024<(42*b.z+b.x*13+b.y*666)%1024; } }; struct Comp6 { bool operator()(const st& a,const st& b) { return (a.x+a.y+a.z)<(b.x+b.y+b.z); } }; struct Comp7 { bool operator()(const st& a,const st& b) { return a.z*42/(a.x*a.y)>b.z*42/(b.x*b.y); } }; struct Comp8 { bool operator()(const st& a,const st& b) { return a.z*42/(a.x*a.y)(b.z*42/(b.x*b.y))%2; } }; struct Comp11 { bool operator()(const st& a,const st& b) { return a.z*b.xb.z*a.y; } }; struct Comp13 { bool operator()(const st& a,const st& b) { return a.z*454+a.x*546+a.y*102b.z*454+b.x*546+b.y*102; } }; struct Comp15 { bool operator()(const st& a,const st& b) { return (a.z*781/a.x*a.y)%666<(b.z*781/b.x*b.y)%666; } }; struct Comp16 { bool operator()(const st& a,const st& b) { return (a.z*781/a.x*a.y)%666>(b.z*781/b.x*b.y)%666; } }; struct Comp17 { bool operator()(const st& a,const st& b) { return a.z%7b.z%7; } }; struct Comp19 { bool operator()(const st& a,const st& b) { return a.z/3*54*a.x/13*a.y/5*19b.z/3*54*b.x/13*b.y/5*19; } }; struct Comp21 { bool operator()(const st& a,const st& b) { return a.z/3*54*a.x/13*a.y/5*19*654%53>b.z/3*54*b.x/13*b.y/5*19*654%53; } }; struct Comp22 { bool operator()(const st& a,const st& b) { return a.z/3*54*a.x/13*a.y/5*19*654%53b.z*b.x-b.y*3; } }; st t; priority_queue ,Comp> pq; priority_queue ,Comp2> pq2; priority_queue ,Comp3> pq3; priority_queue ,Comp4> pq4; priority_queue ,Comp5> pq5; priority_queue ,Comp6> pq6; priority_queue ,Comp7> pq7; priority_queue ,Comp8> pq8; priority_queue ,Comp9> pq9; priority_queue ,Comp10> pq10; priority_queue ,Comp11> pq11; priority_queue ,Comp12> pq12; priority_queue ,Comp13> pq13; priority_queue ,Comp14> pq14; priority_queue ,Comp15> pq15; priority_queue ,Comp16> pq16; priority_queue ,Comp17> pq17; priority_queue ,Comp18> pq18; priority_queue ,Comp19> pq19; priority_queue ,Comp20> pq20; priority_queue ,Comp21> pq21; priority_queue ,Comp22> pq22; priority_queue ,Comp23> pq23; priority_queue ,Comp24> pq24; int n,r,k; int a[128][128][128],b[128][128][128],ans[128][128][128],ansbr[128]; int bfs(int x,int y,int z,int Q) { int br=0; queue q; t.x=x; t.y=y; t.z=z; q.push(t); while(!q.empty()) { t=q.front(); q.pop(); x=t.x; y=t.y; z=t.z; if(x<1||y<1||x>n||y>n)continue; if(b[Q][x][y]==-1)continue; b[Q][x][y]=-1; br++; if(br==z)break; if(x+1<=n) if(b[Q][x+1][y]==0||b[Q][x+1][y]==z) { t.x=x+1; t.y=y; q.push(t); } if(y+1<=n) if(b[Q][x][y+1]==0||b[Q][x][y+1]==z) { t.x=x; t.y=y+1; q.push(t); } if(x-1<=n) if(b[Q][x-1][y]==0||b[Q][x-1][y]==z) { t.x=x-1; t.y=y; q.push(t); } if(y-1<=n) if(b[Q][x][y-1]==0||b[Q][x][y-1]==z) { t.x=x; t.y=y-1; q.push(t); } } return br; } void bfs2(int x,int y,int z,int Q) { int br=0; queue q; t.x=x; t.y=y; t.z=z; q.push(t); while(!q.empty()) { t=q.front(); q.pop(); x=t.x; y=t.y; z=t.z; if(x<1||y<1||x>n||y>n)continue; if(a[Q][x][y]==-1)continue; a[Q][x][y]=-1; ans[Q][x][y]=k; br++; if(br==z)break; if(x+1<=n) if(a[Q][x+1][y]==0||a[Q][x+1][y]==z) { t.x=x+1; t.y=y; q.push(t); } if(y+1<=n) if(a[Q][x][y+1]==0||a[Q][x][y+1]==z) { t.x=x; t.y=y+1; q.push(t); } if(x-1>=1) if(a[Q][x-1][y]==0||a[Q][x-1][y]==z) { t.x=x-1; t.y=y; q.push(t); } if(y-1>=1) if(a[Q][x][y-1]==0||a[Q][x][y-1]==z) { t.x=x; t.y=y-1; q.push(t); } } } int main() { fin>>n>>r; int x,y,z; int u; for(int i=1;i<=r;i++) { fin>>t.x>>t.y>>t.z; // for(int j=0;j<=ASD;j++) { a[j][t.x][t.y]=t.z; b[j][t.x][t.y]=t.z; } pq.push(t); pq2.push(t); pq3.push(t); pq4.push(t); pq4.push(t); pq5.push(t); pq6.push(t); pq7.push(t); pq8.push(t); pq9.push(t); pq10.push(t); pq11.push(t); pq12.push(t); pq13.push(t); pq14.push(t); pq15.push(t); pq16.push(t); pq17.push(t); pq18.push(t); pq19.push(t); pq20.push(t); pq21.push(t); pq22.push(t); pq23.push(t); pq24.push(t); } while(!pq.empty()) { t=pq.top(); pq.pop(); u=0; x=t.x; y=t.y; z=t.z; if(ans[1][x][y]==0) { u=bfs(x,y,z,1); } if(u==t.z) { ansbr[1]+=u; k++; bfs2(x,y,z,1); } else { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { b[1][i][j]=a[1][i][j]; } } } } k=0; while(!pq2.empty()) { t=pq2.top(); pq2.pop(); u=0; x=t.x; y=t.y; z=t.z; if(ans[2][x][y]==0) { u=bfs(x,y,z,2); } if(u==t.z) { ansbr[2]+=u; k++; bfs2(x,y,z,2); } else {//fout<<1<0) { u=bfs(i,j,a[0][i][j],0); if(u==a[0][i][j]) { ansbr[0]+=u; k++; bfs2(i,j,a[0][i][j],0); } else {//fout<<1<ansbr[e])e=i; // cout<