Buying Land

All submissions for this problem are available.
After recent success of cooking reality shows, Chef decided to borrow the idea and host his own "Devil's Kitchen" show. But his ambitions are even bigger. He plans to build a whole complex of hotels, gyms and other entertainment facilities along with one main restaurant where he'll host the show. But first of all, he needs to purhase some land to build on. The country is divided into a grid with R rows and C columns of smaller parcels. Chef wants to buy a rectangular piece of land H parcels high and W parcels wide. He also has a preferred layout in mind. The restaurant will be located at (i,j). For every parcel he defined its preferred height relative to the height of the restaurant parcel. For a particular piece of land, he computes height differences relative to the restaurant and compares them to the preferred layout. This comparison is done as a sum of squared differences over all H*W parcels. Given the map of the country, help him out by finding the K most suitable pieces of land.
Input
Input starts with a description of the country. It is described from north to south and from west to east. All coordinates are 1-based, which means that the parcel farthest to the north-west is at (1,1). Dimensions R and C are given in the first line, followed by R lines of C integers, describing the heights of individual parcels. Following is the description of the chef's preferred layout, starting with dimensions H and W. Position of the restaurant within the layout (i,j) is given in the next line. The j-th number in the i-th line of preferred layout will always be 0. Input ends with number K, the number of solutions you are required to find. It will be possible to buy the piece of land on at least K different positions. All numbers describing heights will have absolute value at most 30.
Output
Output exactly K lines containing the K best pieces of land chef can buy, ordered from best to worst. Each piece of land should be described by numbers BR, BC, S, where (BR,BC) represents the position of the upper-left corner of the purchased piece of land and S the corresponding score. In case of a tie, the chef prefers solutions to the north (direction of decreasing row numbers). If there is still a tie, choose the solution farthest to the west (direction of decreasing column numbers).
Constraints

 1 <= R, C <= 666 
 1 <= H <= (R+1)/2 
 1 <= W <= (C+1)/2 
 1 <= K <= 1000 
Example

Input:
5 8
6 1 8 6 0 0 8 1
6 5 8 6 8 6 8 1
6 5 8 6 8 6 8 2
6 5 8 6 8 6 8 2
6 5 8 6 8 6 0 2
3 4
-1 2 2 2
-1 2 0 2
-1 2 2 2
2 3
4

Output:
2 2 8
3 2 8
2 4 11
3 4 75
Explanation
If the chef decides to buy the land from (3,4) to (5,7), he will end up with the following layout:
0 2 0 2
0 2 0 2
0 2 0 -6
Comparing it to the preferred layout gives the penalty of 1+0+4+0+1+0+0+0+1+0+4+64=75, which is 4th best.

Example input:

Example output:


Solution:

    #include <cstdio>
    #include <cassert>
    #include <queue>
    #include <utility>
    #include <stack>
    using namespace std;
     
    const int MOD=68681729;
    const int TWO_INV=(MOD+1)/2, MAX_N=19;
    const int OMEGA=378, OMEGA_INV=64502682;
     
    inline int add(int a, int b){
    	a+=b;
    	if(a>=MOD)
    		a-=MOD;
    	return a;
    }
     
    inline int sub(int a, int b){
    	a-=b;
    	if(a<0)
    		a+=MOD;
    	return a;
    }
     
    inline int mul(int a, int b){
    	return (long long)a*b%MOD;
     
    }
     
    int power(int base, int exponent){
    	int sq=base, res=1;
    	for(; exponent; exponent>>=1){
    		if(exponent&1)
    			res=mul(res, sq);
    		sq=mul(sq, sq);
    	}
    	return res;
    }
     
     
    void bit_reverse(int *arr, int N, int bits=0, int a=0, int b=0){
    	if(bits==N){
    		if(a<b){
    			int tmp=arr[a];
    			arr[a]=arr[b];
    			arr[b]=tmp;
    		}
    	}else{
    		bit_reverse(arr, N, bits+1, a<<1, b);
    		bit_reverse(arr, N, bits+1, (a<<1)+1, b+(1<<bits));
    	}
    }
     
    void inplace_fft(int *arr, int N, int w){
    	if(N==1){
    		assert(w==(MOD-1));
    		int E=arr[0], O=arr[1];
    		arr[0]=add(E, O);
    		arr[1]=sub(E, O);
    	}else{
    		int ww=mul(w, w);
    		int sh=1<<(N-1);
    		inplace_fft(arr, N-1, ww);
    		inplace_fft(arr+sh, N-1, ww);
    		int p=1;
    		for(int i=0; i<sh; i++){
    			int E=arr[i], O=mul(arr[i+sh], p);
    			arr[i]=add(E, O);
    			arr[i+sh]=sub(E, O);
    			p=mul(p, w);
    		}
    	}
    }
     
    void fft(int *arr, int N){
    	bit_reverse(arr, N);
    	inplace_fft(arr, N, power(OMEGA, 1<<(MAX_N-N)));
    }
     
    void fft_inv(int *arr, int N){
    	bit_reverse(arr, N);
    	inplace_fft(arr, N, power(OMEGA_INV, 1<<(MAX_N-N)));
    	int p=power(TWO_INV, N);
    	for(int i=0; i<1<<N; i++)
    		arr[i]=mul(arr[i], p);
    }
     
    int land[666][666], pattern[666][666], score[666][666];
    int landsum[667][667], landsqsum[667][667];
    int buf1[1<<19], buf2[1<<19];
    int R, C, H, W, K, ri, rj;
    #define inrect(arr, r, c) (arr[r+H][c+W]-arr[r+H][c]-arr[r][c+W]+arr[r][c])
     
    void read(){
    	scanf("%d %d", &R, &C);
    	for(int i=0; i<R; i++)
    	for(int j=0; j<C; j++)
    		scanf("%d", &land[i][j]);
    	scanf("%d %d", &H, &W);
    	for(int i=0; i<H; i++)
    	for(int j=0; j<W; j++)
    		scanf("%d", &pattern[i][j]);
    	scanf("%d %d %d", &ri, &rj, &K);
    	ri--;
    	rj--;
    }
     
    void solve(){
    	for(int i=0; i<R; i++)
    	for(int j=0; j<C; j++){
    		landsum[i+1][j+1]=land[i][j]+landsum[i+1][j]+landsum[i][j+1]-landsum[i][j];
    		landsqsum[i+1][j+1]=land[i][j]*land[i][j]+landsqsum[i+1][j]+landsqsum[i][j+1]-landsqsum[i][j];
    	}
    	int psum=0, psqsum=0;
    	for(int i=0; i<H; i++)
    	for(int j=0; j<W; j++){
    		psum+=pattern[i][j];
    		psqsum+=pattern[i][j]*pattern[i][j];
    	}
    	for(int i=0; i<R; i++)
    	for(int j=0; j<C; j++){
    		buf1[i*C+j]=sub(land[i][j], 0);
    		buf2[i*C+j]=(i<H && j<W) ? sub(pattern[i][j], 0) : 0;
    	}
    	fft(buf1, 19);
    	fft(buf2, 19);
    	for(int i=0; i<1<<19; i++)
    		buf1[i]=mul(buf1[i], buf2[-i&((1<<19)-1)]);
    	fft_inv(buf1, 19);
    	for(int i=0; i<1<<19; i++)
    		if(buf1[i]>MOD/2)
    			buf1[i]-=MOD;
    	for(int i=0; i+H<=R; i++)
    	for(int j=0; j+W<=C; j++)
    		score[i][j]=W*H*land[i+ri][j+rj]*land[i+ri][j+rj]+inrect(landsqsum, i, j)+psqsum
    			+2*land[i+ri][j+rj]*(psum-inrect(landsum, i, j))-2*buf1[i*C+j];
    }
     
    void print(){
    	priority_queue<pair<int, pair<int, int> > > pq;
    	for(int i=0; i+H<=R; i++)
    	for(int j=0; j+W<=C; j++){
    		pq.push(make_pair(score[i][j], make_pair(i, j)));
    		if(pq.size()>K)
    			pq.pop();
    	}
    	stack<pair<int, pair<int, int> > > st;
    	while(!pq.empty()){
    		st.push(pq.top());
    		pq.pop();
    	}
    	while(!st.empty()){
    		printf("%d %d %d\n", st.top().second.first+1, st.top().second.second+1, st.top().first);
    		st.pop();
    	}
    }
     
    int main(){
    	read();
    	solve();
    	print();
    	return 0;
    }