## Editorial for VM 08 Bài 02 - Bàn cờ

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

#### Code mẫu của flashmt

const fi='';
fo='';
maxn=500;
var n,i,j,t,m,u,x,y:longint;
f:array[0..maxn,0..maxn] of longint;

begin
assign(input,fi); reset(input);
assign(output,fo); rewrite(output);
for i:=1 to n do
begin
x:=0;
for j:=1 to n do
begin
if not odd(j+i) then x:=x+t
else x:=x-t;
f[i,j]:=f[i-1,j]+x;
end;
end;
for i:=1 to m do
begin
writeln(abs(f[x,y]-f[x,u-1]-f[t-1,y]+f[t-1,u-1]));
end;
close(input); close(output);
end.


#### Code mẫu của happyboy99x

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define sz(a) int((a).size())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(), _e = (c).end(); i != _e; ++i)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define rep(i,n) for(int i = 0, _n = (n); i < _n; ++i)
#define repd(i,n) for(int i = (n)-1; i >= 0; --i )
#define fo(i,a,b) for(int i = (a), _b = (b); i <= _b; ++i)
#define fod(i,a,b) for(int i = (a), _b = (b); i >= _b; --i)

#define INF 1000000000
#define N 505

int a[N][N], f[N][N], g[N][N], n, q;

int main() {
#ifndef ONLINE_JUDGE
freopen( "input.txt", "r", stdin );
//freopen( "output.txt", "w", stdout );
#endif
scanf("%d",&n);
fo(i,1,n) fo(j,1,n) scanf("%d",&a[i][j]);
fo(i,1,n) fo(j,1,n) {
f[i][j] = f[i-1][j] + f[i][j-1] - f[i-1][j-1] + a[i][j];
g[i][j] = g[i-1][j] + g[i][j-1] - g[i-1][j-1] + ((i+j)%2?a[i][j]:0);
}
scanf("%d",&q);
for(int i = 0, a,b,x,y; i < q; ++i) {
scanf("%d%d%d%d",&a,&b,&x,&y);
printf("%d\n", abs(f[x][y]-f[a-1][y]-f[x][b-1]+f[a-1][b-1]-2*(g[x][y]-g[a-1][y]-g[x][b-1]+g[a-1][b-1])));
}
return 0;
}


program vboard;
uses    math;
const   fi='';
maxN = 505;
var     fw,fb,a:array[0..maxN,0..maxN] of longint;
n,q,i,x,y,u,v:longint;
inp:text;
procedure input;
var     i,j:longint;
begin
assign(inp,fi);
reset(inp);
for i:=1 to n do
begin
for j:=1 to n do
end;
end;

function isWhite(i,j:longint):boolean;
begin
if (odd(i) and odd(j))
or (not odd(i)) and (not odd(j))
then exit(true);
exit(false);
end;

procedure init;
var     i,j:longint;
begin
for i:=1 to n do
for j:=1 to n do
begin

fw[i,j]:=fw[i-1,j]+fw[i,j-1]-fw[i-1,j-1];
fb[i,j]:=fb[i-1,j]+fb[i,j-1]-fb[i-1,j-1];
if isWhite(i,j) then
inc(fw[i,j],a[i,j])
else
inc(fb[i,j],a[i,j]);
end;
end;

function query(i,j,m,n:longint):longint;
var     res,w,b:longint;
begin
w:=fw[m,n]+fw[i-1,j-1]-fw[i-1,n]-fw[m,j-1];
b:=fb[m,n]+fb[i-1,j-1]-fb[i-1,n]-fb[m,j-1];
exit(abs(w-b));
end;

begin
input;
init;
for i:=1 to q do
begin
writeln(query(x,y,u,v));
end;
close(inp);
end.


#### Code mẫu của RR

var
i,j,n,m,x,y,u,v:longint;
a,sum:array[0..1011,0..1011] of longint;

begin
for i:=1 to n do
for j:=1 to n do
begin
read(a[i,j]); if (i+j) and 1=1 then a[i,j]:=-a[i,j];
sum[i,j]:=sum[i-1,j]+sum[i,j-1]-sum[i-1,j-1]+a[i,j];
end;

for i:=1 to m do
begin
writeln(abs(sum[u,v]-sum[x-1,v]-sum[u,y-1]+sum[x-1,y-1]));
end;
end.


#### Code mẫu của hieult

#include <stdio.h>
//#include <conio.h>
main()
{
long n,a[501][501],b[501][501],T,x1,y1,x2,y2,KQ;
scanf("%ld",&n);
for(long i=1;i<=n;i++)
{
b[i][0]=0;
for(long j=1;j<=n;j++)
{
scanf("%ld",&a[i][j]);
if((i-j)%2==0)
b[i][j]=b[i][j-1]+a[i][j];
else
b[i][j]=b[i][j-1]-a[i][j];
}
}
scanf("%ld",&T);
for(long i=1;i<=T;i++)
{
scanf("%ld %ld %ld %ld",&x1,&y1,&x2,&y2);
KQ=0;
for(long j=x1;j<=x2;j++)
KQ=KQ+b[j][y2]-b[j][y1-1];
if(KQ<0)
KQ=-KQ;
printf("%ld\n",KQ);
}
//getch();
}


#### Code mẫu của ll931110

Program VBOARD;
Const
input  = '';
output = '';
Var
a,s: array[0..500,0..500] of longint;
n,q: longint;

Procedure optimize;
Var
i,j: longint;
Begin
Fillchar(s, sizeof(s), 0);
For i:= 1 to n do
For j:= 1 to n do
Begin
s[i,j]:= s[i - 1,j] + s[i,j - 1] - s[i - 1,j - 1];
If odd(i + j) then s[i,j]:= s[i,j] - a[i,j]
else s[i,j]:= s[i,j] + a[i,j];
End;
End;

Procedure solve;
Var
fi,fo: text;
t,i,j,k: longint;
i1,i2,j1,j2: longint;
Begin
Assign(fi, input);
Reset(fi);

For i:= 1 to n do
For j:= 1 to n do read(fi, a[i,j]);

optimize;
Assign(fo, output);
Rewrite(fo);

For k:= 1 to q do
Begin
t:= s[i2,j2] - s[i1 - 1,j2] - s[i2,j1 - 1] + s[i1 - 1,j1 - 1];
Writeln(fo, abs(t));
End;
Close(fo);
Close(fi);
End;

Begin
solve;
End.


#### Code mẫu của skyvn97

#include<stdio.h>
#define MAX   555
int a[MAX][MAX];
int b[MAX][MAX];
int w[MAX][MAX];
int n,q;
int abs(int x) {
if (x<0) return (-x); else return (x);
}
void init(void) {
scanf("%d",&n);
int i,j;
for (i=0;i<=n;i=i+1) {
b[i][0]=0;
b[0][i]=0;
w[i][0]=0;
w[0][i]=0;
}
for (i=1;i<=n;i=i+1)
for (j=1;j<=n;j=j+1) {
scanf("%d",&a[i][j]);
b[i][j]=b[i-1][j]+b[i][j-1]-b[i-1][j-1]+a[i][j]*((i+j)%2==1);
w[i][j]=w[i-1][j]+w[i][j-1]-w[i-1][j-1]+a[i][j]*((i+j)%2==0);
}
}
void process(void) {
int i,sb,sw,x,y,z,t;
scanf("%d",&q);
for (i=1;i<=q;i=i+1) {
scanf("%d",&x);
scanf("%d",&y);
scanf("%d",&z);
scanf("%d",&t);
sb=b[z][t]-b[x-1][t]-b[z][y-1]+b[x-1][y-1];
sw=w[z][t]-w[x-1][t]-w[z][y-1]+w[x-1][y-1];
printf("%d\n",abs(sw-sb));
}
}
int main(void) {
init();
process();
}


#### Code mẫu của khuc_tuan

#include <iostream>
#include <sstream>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

#define Rep(i,n) for(int i=0;i<(n);++i)
#define Lap(i,n) for(int i=1;i<=(n);++i)
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Ford(i,a,b) for(int i=(a);i>=(b);--i)
#define Fit(i,v) for(__typeof(v.begin()) i=v.begin();i!=v.end();++i)
#define Fill(a,b) memset((a), (b), sizeof(a))
#define pb push_back
#define MP make_pair
#define pause system("pause");

typedef long long LL;

int a[505][505];
int n;
int den[505][505], tr[505][505];

int main() {
scanf("%d", &n);
Lap(i,n) Lap(j,n) scanf("%d", &a[i][j]);
Lap(i,n) Lap(j,n) {
den[i][j] = den[i-1][j] + den[i][j-1] - den[i-1][j-1];
tr[i][j] = tr[i-1][j] + tr[i][j-1] - tr[i-1][j-1];
if((i+j)%2==0) tr[i][j] += a[i][j];
else den[i][j] += a[i][j];
}
int q;
scanf("%d", &q);
Lap(kk,q) {
int x, y, u, v;
scanf("%d%d%d%d", &x, &y, &u, &v);
int trang = tr[u][v] - tr[x-1][v] - tr[u][y-1] + tr[x-1][y-1];
int _den = den[u][v] - den[x-1][v] - den[u][y-1] + den[x-1][y-1];
int res = abs(trang-_den);
printf("%d\n", res);
}
//pause
return 0;
}