离散化+二位前缀和的计数题 [USACO20DEC] Rectangular Pasture S

USACO20DEC Rectangular Pasture S

显然奶牛坐标最高可达 1 e 9 1e9 1e9, 所以我们先离散化.

cpp 复制代码
for(int i=1;i<=n;P(i)){
	std::cin>>a[i].first>>a[i].second;
	kkk[0].push_back(a[i].first);
	kkk[1].push_back(a[i].second);
}
std::sort(kkk[0].begin(),kkk[0].end());
std::sort(kkk[1].begin(),kkk[1].end());
auto p0=std::unique(kkk[0].begin(),kkk[0].end());
auto p1=std::unique(kkk[1].begin(),kkk[1].end());
kkk[0].erase(p0,kkk[0].end());
kkk[1].erase(p1,kkk[1].end());
for(int i=1;i<=n;P(i)){
	a[i].first=std::lower_bound(kkk[0].begin(),kkk[0].end(),a[i].first)-kkk[0].begin()+1;
	a[i].second=std::lower_bound(kkk[1].begin(),kkk[1].end(),a[i].second)-kkk[1].begin()+1;
	f[a[i].first][a[i].second]=1;
}

显然我们的目的是要去枚举子集. 我们不妨先把坐标排序, 枚举矩形的上下界, 再统计矩形左右两边有相同上下界牛的个数, 相乘即可.

我们以 y y y 为关键字排序, 这样我们就可以直接枚举 y ∈ i , j y\ini,j y∈i,j 了.

cpp 复制代码
std::sort(a+1,a+1+n,[](const PII& A, const PII& B){return A.second<B.second;});
for(int i=1,max,min;i<=n;P(i))
for(int j=i;j<=n;P(j)){
	max=a[i].first,min=a[j].first;
	if(max<min)std::swap(max,min);
	ans+=1ll*(f[n][j]-f[max-1][j]-f[n][i-1]+f[max-1][i-1])*1ll*(f[min][j]-f[0][j]-f[min][i-1]+f[0][i-1]);
}

但我们也要注意, 我们要 O ( 1 ) O(1) O(1) 统计牛的数量, 于是我们选择前缀和.

cpp 复制代码
for(int i=1;i<=n;P(i))
	for(int j=1;j<=n;P(j))
		f[i][j]+=f[i-1][j]+f[i][j-1]-f[i-1][j-1];

最终答案补上空集即可.

cpp 复制代码
ans++;

总代码:

cpp 复制代码
#include<iostream>
#include<vector>
#include<algorithm>
#define P(A) A=-~A
typedef std::pair<int,int> PII;
typedef long long LL;
#define NUMBER1 2500
int n,f[NUMBER1+5][NUMBER1+5];
PII a[NUMBER1+5];
LL ans(0);
std::vector<int>kkk[2];
signed main(){
	std::cin.tie(nullptr)->std::ios::sync_with_stdio(false);
	std::cout.tie(nullptr);
	std::cin>>n;
	for(int i=1;i<=n;P(i)){
		std::cin>>a[i].first>>a[i].second;
		kkk[0].push_back(a[i].first);
		kkk[1].push_back(a[i].second);
	}
	std::sort(kkk[0].begin(),kkk[0].end());
	std::sort(kkk[1].begin(),kkk[1].end());
	auto p0=std::unique(kkk[0].begin(),kkk[0].end());
	auto p1=std::unique(kkk[1].begin(),kkk[1].end());
	kkk[0].erase(p0,kkk[0].end());
	kkk[1].erase(p1,kkk[1].end());
	for(int i=1;i<=n;P(i)){
		a[i].first=std::lower_bound(kkk[0].begin(),kkk[0].end(),a[i].first)-kkk[0].begin()+1;
		a[i].second=std::lower_bound(kkk[1].begin(),kkk[1].end(),a[i].second)-kkk[1].begin()+1;
		f[a[i].first][a[i].second]=1;
	}
	for(int i=1;i<=n;P(i))
		for(int j=1;j<=n;P(j))
			f[i][j]+=f[i-1][j]+f[i][j-1]-f[i-1][j-1];
	std::sort(a+1,a+1+n,[](const PII& A, const PII& B){return A.second<B.second;});
	for(int i=1,max,min;i<=n;P(i))
		for(int j=i;j<=n;P(j)){
			max=a[i].first,min=a[j].first;
			if(max<min)std::swap(max,min);
			ans+=1ll*(f[n][j]-f[max-1][j]-f[n][i-1]+f[max-1][i-1])*1ll*(f[min][j]-f[0][j]-f[min][i-1]+f[0][i-1]);
		}
	std::cout<<ans+1;
	return 0;
}
相关推荐
not coder21 小时前
一文讲通--从树到红黑树
数据结构·算法
octopus_c21 小时前
C++ 入门
开发语言·c++·算法
ZhengEnCi21 小时前
2026年10月30日AI早报:Gemini 4 Carbon登顶、DeepSeek融资1055亿、智元机器人上岗实测
人工智能·算法
强哥聊算力1 天前
《算力江湖·Intel帝国往事》第三集:安迪·格鲁夫的铁血时代:Intel为什么能把对手逼到墙角?
运维·人工智能·科技·算法·ai·gpu算力
Shaoshing1 天前
LeetCode100 11. 盛最多水的容器
算法·leetcode
骑着蜗牛撵大象3271 天前
558 万美元训出 671B 模型:DeepSeek-V3 算法与系统的协同拆解
分布式·算法·moe·deepseek-v3·mla·fp8训练
Eyfcom1 天前
〖阶段性回顾〗物流系统架构与算法的 Day 1-5 知识图谱与实战答疑
算法·系统架构·知识图谱·物流管理系统·快递管理系统
xunberg1 天前
【Agent 生产化手记-02】Agent 怎么向人提问
人工智能·算法·架构
海绵宝宝转agent1 天前
Leetcode二叉树算法个人理解
笔记·学习·算法·leetcode·深度优先
pjj198541 天前
YOLO 目标检测算法系列梳理:从 V1 到 V5 的原理演进
算法·yolo·目标检测