题目链接:Memento Mori
#include <bits/stdc++.h>
using namespace std ;
typedef long long LL ;
#define clr( a , x ) memset ( a , x , sizeof a )
const int MAXN = 2005 ;
struct Node {
int x , y ;
Node () {}
Node ( int x , int y ) : x ( x ) , y ( y ) {}
} ;
Node a[MAXN] , b[MAXN] , c[MAXN] , X[4] ;
int n , m , K ;
int R[2][MAXN] ;
int G[MAXN][MAXN] ;
int p[4] ;
int cmp1 ( const Node& a , const Node& b ) {
return a.x < b.x ;
}
int cmp2 ( const Node& a , const Node& b ) {
return a.y > b.y ;
}
int cmp3 ( const Node& a , const Node& b ) {
return a.y < b.y ;
}
int cnt ( int x1 , int x2 , int y1 , int y2 ) {
return G[x2][y2] - G[x1 - 1][y2] - G[x2][y1 - 1] + G[x1 - 1][y1 - 1] ;
}
int is () {
int x1 = n , x2 = 0 , y1 = m , y2 = 0 ;
for ( int i = 0 ; i < 4 ; ++ i ) {
x1 = min ( x1 , X[i].x ) ;
x2 = max ( x2 , X[i].x ) ;
y1 = min ( y1 , X[i].y ) ;
y2 = max ( y2 , X[i].y ) ;
for ( int j = i + 1 ; j < 4 ; ++ j ) {
if ( ( p[j] - p[i] ) * ( X[j].y - X[i].y ) <= 0 ) return 0 ;
}
}
if ( cnt ( x1 , x2 , y1 , y2 ) != 4 ) return 0 ;
return 1 ;
}
void solve () {
scanf ( "%d%d%d" , &n , &m , &K ) ;
for ( int i = 0 ; i <= n ; ++ i ) for ( int j = 0 ; j <= m ; ++ j ) G[i][j] = 0 ;
scanf ( "%d%d%d%d" , &p[0] , &p[1] , &p[2] , &p[3] ) ;
int X1 = 0 , X2 = 0 , res = 0 ;
p[1] < p[0] ? X1 ++ : X2 ++ ;
p[2] < p[0] ? X1 ++ : X2 ++ ;
for ( int i = 1 ; i <= K ; ++ i ) {
int x , y ;
scanf ( "%d%d" , &x , &y ) ;
G[x][y] = 1 ;
a[i] = b[i] = c[i] = Node ( x , y ) ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
for ( int j = 1 ; j <= m ; ++ j ) G[i][j] += G[i][j - 1] ;
for ( int j = 1 ; j <= m ; ++ j ) G[i][j] += G[i - 1][j] ;
}
sort ( a + 1 , a + K + 1 , cmp1 ) ;
sort ( b + 1 , b + K + 1 , cmp2 ) ;
sort ( c + 1 , c + K + 1 , cmp3 ) ;
for ( int i = 1 ; i <= K ; ++ i ) {
for ( int j = 0 ; j <= K ; ++ j ) {
R[0][j] = R[1][j] = j == K ? 0 : j + 1 ;
}
X[0] = a[i] ;
for ( int j = K ; j > i ; -- j ) {
if ( a[i].x == a[j].x ) break ;
if ( ( a[j].y - a[i].y ) * ( p[3] - p[0] ) <= 0 ) continue ;
X[3] = a[j] ;
X[1] = X[2] = Node ( -1 , -1 ) ;
int x1 = X1 , x2 = X2 , t = 1 ;
int o = 0 , p ;
while ( x1 -- ) for ( p = o , o = R[0][o] ; o ; R[0][p] = R[0][o] , p = o , o = R[0][o] ) {
if ( a[i].x < b[o].x && b[o].x < a[j].x && b[o].y < a[i].y ) {
X[t ++] = b[o] ;
break ;
}
}
o = 0 ;
while ( x2 -- ) for ( p = o , o = R[1][o] ; o ; R[1][p] = R[1][o] , p = o , o = R[1][o] ) {
if ( a[i].x < c[o].x && c[o].x < a[j].x && c[o].y > a[i].y ) {
X[t ++] = c[o] ;
break ;
}
}
if ( t < 3 || X[1].x == X[2].x ) continue ;
if ( X[1].x > X[2].x ) swap ( X[1] , X[2] ) ;
if ( is () ) res ++ ;
}
}
printf ( "%d\n" , res ) ;
}
int main () {
int T ;
scanf ( "%d" , &T ) ;
for ( int i = 1 ; i <= T ; ++ i ) solve () ;
return 0 ;
}