-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chef and Rectangle Array.cpp
206 lines (144 loc) · 3.86 KB
/
Chef and Rectangle Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
RAJAT SINGH
VIT VELLORE
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
//#define max 706000
#define gc getchar_unlocked
#define pc putchar_unlocked
inline void get(int *p){register int n=0,sign=1,a=gc();while(a<'0'||a>'9'){ if(a=='-'){sign=-1;a=gc();break;} a=gc();}while(a>='0' && a<='9'){n=(n<<1)+(n<<3)+a-'0';a=gc();}*p=n*sign;}
inline void put(int n){char d[10];int i=0;if(n<0){pc('-');n=-n;}do{d[i++]=n%10+'0';n=n/10;}while(n);while(i)pc(d[--i]);pc('\n');}
#define sc(a) scanf("%d",&a)
# define ll long long
#define p(a) printf("%d\n",a)
#define l(i,a) for(int i=0;i<a;i++)
#define re(j,a,b) for(ll int j=a;j<=b;j++)
#define modu 1000000007
/*ll modular_pow(ll base, ll exponent)
{
ll result = 1;
while (exponent > 0)
{
if (exponent % 2 == 1)
result = (result * base) % MOD;
exponent = exponent >> 1;
base = (base * base) % MOD;
}
return result;
}
*/
int gcd(int a,int b){
if(b==0)
return a;//gcd
else return gcd(b,a%b);
}
bool chek(int x){
return (x&&!(x&(x-1)));// power of 2
}
int onebits(int n){
return __builtin_popcount(n);//count number od set bits in a number
}
//ll max(ll x, ll y){
// if(x>=y) return x;
// else return y;
//}
string s;
void swap(char *x, char *y){
char t;
t= *x;
*x= *y;
*y=t;
}
int c=0;
void pr(int st, int e){
if(st==e){
cout<<s<<endl;
}
else re(i,st,e){
swap(&s[i],&s[st]);
pr(st+1,e);
swap(&s[i],&s[st]);
}
}
//int a[300001];
int a[1000][1000];
int b[1000][1000];
int dp[1000][1000];
vector< vector< pair< int , int > > >mm;
int n, m;
int ans[1000][1000][10][10];
/*int calmax(int x, int y, int g, int h ){
for(int u=1000;u>=1;u--){
if(mm[u].size()>0){
int k= mm[u].size();
for(int j=0;j<k;j++){
pair< int , int >pp=mm[u][j];
if(pp.first>=x&&pp.first<=g && pp.second>=y && pp.second<=h) return u;
}
}
}
}*/
// use sparse matrix logic!!!
void precomp(){
for (int i=0;(1<<i)<=n;i++){
for(int j=0;(1<<j)<=m;j++){
for (int x=0;x+(1<<i)-1<n;x++){
for (int y=0;y+(1<<j)-1< m;y++){
if (i==0&&j==0)
ans[x][y][i][j] =a[x][y];
else if (i == 0)
ans[x][y][i][j] =max(ans[x][y][i][j-1], ans[x][y+(1<<(j-1))][i][j-1]);
else if (j == 0)
ans[x][y][i][j] = max(ans[x][y][i-1][j], ans[x+(1<<(i-1))][y][i-1][j]);
else
ans[x][y][i][j] = max(max(max(ans[x][y][i-1][j-1], ans[x + (1<<(i-1))][y][i-1][j-1]), ans[x][y+(1<<(j-1))][i-1][j-1]), ans[x + (1<<(i-1))][y+(1<<(j-1))][i-1][j-1]);
}
}
}
}
}
int calmax(int x, int y, int x1, int y1){
int k = log2(x1 - x + 1);
int l = log2(y1 - y + 1);
return max(max(max(ans[x][y][k][l], ans[x1 - (1<<k) + 1][y][k][l]), ans[x][y1 - (1<<l) + 1][k][l]), ans[x1 - (1<<k) + 1][y1 - (1<<l) + 1][k][l]);
}
int main() {
// freopen("inp.txt", "r", stdin);
get(&n);
get(&m);
//mm=vector< vector< pair< int , int > > >(1001);
re(i,0, m) dp[0][i]=0;
re(i,0,n) dp[i][0]=0;
re(i, 0,n-1){
re(j,0,m-1){
get(&a[i][j]);//=0;
b[i+1][j+1]= a[i][j];
dp[i+1][j+1]=b[i+1][j+1]+dp[i][j+1]+dp[i+1][j]- dp[i][j];
}
}
precomp();
int q;
get(&q);
// main logic comes here man!!!!
while(q--){
int st, en;
get(&st);
get(&en);
//sc(st);
//sc(en);
int blx, bty;
int ans=INT_MAX;
for(int u=1;u<=n-st+1;u++){
for(int k=1;k<=m-en+1;k++){
blx= u+st-1;
bty= k+en-1;
ans= min(ans, abs((blx-u+1)*(bty-k+1)*calmax(u-1, k-1, blx-1, bty-1)-(dp[blx][bty]-dp[blx][k-1]-dp[u-1][bty]+dp[u-1][k-1])));
// cout<<calmax(u, k, blx, bty)<<endl;
}
}
// cout<<"\n";
put(ans);}
return 0;
}