原题
Problem A. Dull Chocolates
Input file: dull.in
Output file: standard output
Time limit: 9 seconds
Memory limit: 1024 megabytes
Fouad wants to eat a chocolate bar, so he bought a rectangular chocolate bar that has N rows and M
columns of chocolate.
Fouad found that most of the cells in that chocolate bar are dark chocolate except for K cells which are
white chocolate. Now, he wants to eat a prefix sub-grid of the bar. A prefix sub-grid is a rectangular
sub-grid that starts at the first cell (1, 1) and ends at any cell (i, j). Fouad is also wondering how many
perfect prefix sub-grids of the bar. A perfect prefix sub-grid is a one that contains an odd number of white
chocolate cells.
Help Fouad find the number of perfect prefix sub-grids and non-perfect prefix sub-grids.
Input
The first line of the input containing a single integer T specifying the number of test cases.
Each test case begins with a line containing three integers N, M, and K (1 ≤ N, M ≤ 109, 0 ≤ K ≤ 103),
in which N and M are the number of rows and columns in the chocolate bar, respectively, and K is the
number of white chocolate cells.
Then K lines follow, each line containing two integers Xi and Yi (1 ≤ Xi ≤ N, 1 ≤ Yi ≤ M), giving the
positions of the white chocolate cells. It is guaranteed that all the given positions are unique.
Output
For each test case, print a single line containing two space-separated integers representing the number of
perfect prefix sub-grids and non-perfect prefix sub-grids, respectively.
Example
dull.in
3
3 3 0
3 3 1
2 2
5 5 5
1 5
2 1
3 3
4 2
5 4
standard output
0 9
4 5
14 11
Note
In the second test case, the chocolate bar can be represented as follows:
ddd
dwd
ddd
in which ‘d’ represents dark chocolate cells while ‘w’ represents white chocolate cells. There are four perfect
prefix sub-grids that end at cells: (2, 2), (2, 3), (3, 2), and (3, 3).
解题思路
注意到n,m的值较大,k值较小,可以进行离散化,使之成为一个1000*1000 的矩阵
然后,为了统计点的奇偶性,可以采用二维前缀和 F(i,j)=F(i-1,j)+F(i,j-1)-F(i-1,j-1),就可以确定该点的奇偶性,然后利用面积就可以求出矩形的个数
AC代码
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+7;
typedef long long ll;
int dp[maxn][maxn];
int x[maxn],y[maxn];
int mx[maxn],my[maxn];
int t,n,m,k;
int main(){
freopen("dull.in","r",stdin);
cin>>t;
while(t--){
memset(dp,0,sizeof dp);
cin>>n>>m>>k;
for(int i=1;i<=k;i++){
scanf("%d%d",&x[i],&y[i]);
mx[i]=x[i],my[i]=y[i];//用于进行离散化
}
//离散化的过程
sort(mx+1,mx+k+1);
sort(my+1,my+k+1);
//确定排序
int nx=unique(mx+1,mx+1+k)-mx-1;
int ny=unique(my+1,my+1+k)-my-1;
//去除重复
mx[++nx]=n+1;
my[++ny]=m+1;
//保证所有的点都能找到
for(int i=1;i<=k;i++){
x[i]=lower_bound(mx+1,mx+1+nx,x[i])-mx;
y[i]=lower_bound(my+1,my+1+ny,y[i])-my;//得到离散化后的点
dp[x[i]][y[i]]++;
//标记该点
}
//得到每个点的函数
for(int i=1;i<nx;i++)
for(int j=1;j<ny;j++){
dp[i][j]+=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];
} //二维前缀和
ll ans=0;
for(int i=1;i<nx;i++)
for(int j=1;j<ny;j++){
if(dp[i][j]%2==0) continue;
ans++;
ans+=mx[i+1]-mx[i]-1;
ans+=my[j+1]-my[j]-1;
ans+=(ll)(mx[i+1]-mx[i]-1)*(ll)(my[j+1]-my[j]-1);
}
printf("%lld %lld\n",ans,(ll)n*(ll)m-ans);
}
}