Memento Mori (二维前缀和 + 枚举剪枝)

时间:2023-12-21 17:31:32

枚举指的是枚举矩阵的上下界,然后根据p0, p1, p2的关系去找出另外的中间2个点。然后需要记忆化一些地方防止重复减少时间复杂度。这应该是最关键的一步优化时间,指的就是代码中to数组。然后就是子矩阵的一个计算了,需要用二维前缀和预处理数据,然后判断的时候直接O(1)查询就好了。

#include<bits/stdc++.h>
using namespace std; const int inf = 0x3f3f3f3f;
const int maxn = 2e3 + ;
struct node{
int x, y;
node(){}
node(int x, int y) : x(x), y(y){}
}a[maxn], b[maxn], c[maxn], X[];
int p[], matdp[maxn][maxn], to[maxn][]; bool cmp1(const node &a, const node &b){
return a.x < b.x;
}
bool cmp2(const node &a, const node &b){
return a.y > b.y;
}
bool cmp3(const node &a, const node &b){
return a.y < b.y;
} int howmany(int x1, int y1, int x2, int y2){
return matdp[x2][y2] + matdp[x1 - ][y1 - ] - matdp[x1 - ][y2] - matdp[x2][y1 - ];
}
bool isOK(){
int x1 = inf, y1 = inf, x2 = , y2 = ;
for(int i = ; i < ; 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 + ; j < ; j ++)
if((p[j] - p[i]) * (X[j].y - X[i].y) <= ) return false;
}
return howmany(x1, y1, x2, y2) == ;
} int main(){
int T, n, m, k, x, y;scanf("%d",&T);
while(T --){
scanf("%d%d%d", &n, &m, &k);
scanf("%d%d%d%d", &p[], &p[], &p[], &p[]);
int big = , small = , ans = ;
(p[] > p[]) ? small ++ : big ++;
(p[] > p[]) ? small ++ : big ++;
memset(matdp, , sizeof(matdp));
for(int i = ; i <= k; i ++){
scanf("%d%d", &x, &y);matdp[x][y] = ;
a[i] = b[i] = c[i] = node(x, y);
}
for(int i = ; i <= n; i ++){
for(int j = ; j <= m; j ++)
matdp[i][j] += matdp[i][j - ];
for(int j = ; j <= m; j ++)
matdp[i][j] += matdp[i - ][j];
}
sort(a + , a + k + , cmp1);
sort(b + , b + k + , cmp2);
sort(c + , c + k + , cmp3);
for(int i = ; i <= k; i ++){
X[] = a[i];
for(int j = ; j <= k; j ++)
to[j][] = to[j][] = j == k ? : j + ;
for(int j = k; j > i; j --){
if(a[i].x == a[j].x) break;
if((p[] - p[]) * (a[j].y - a[i].y) <= ) continue;
X[] = a[j]; X[] = X[] = node(-, -);
int u = , v, bi = big, sm = small, t = ;
while(sm --){
for(v = u, u = to[u][]; u; to[v][] = to[u][], v = u, u = to[u][])
if(a[i].x < b[u].x && b[u].x < a[j].x && b[u].y < a[i].y){
X[t ++] = b[u];break;
}
}
u = ;
while(bi --){
for(v = u, u = to[u][]; u; to[v][] = to[u][], v = u, u = to[u][])
if(a[i].x < c[u].x && c[u].x < a[j].x && c[u].y > a[i].y){
X[t ++] = c[u];break;
}
}
if(t < || X[].x == X[].x) continue;
if(X[].x > X[].x) swap(X[], X[]);
if(isOK()) ans ++;
}
}
printf("%d\n",ans);
}
return ;
}