题解:AT_abc482_c [ABC482C] Band Count
QyMeteor76 · · 题解
[ABC482C] Band Count
:::info[main idea]
题目大意
给定一个
有
1 u d:求第u 行到第d 行内的棋子总数。2 l r:求第l 列到第r 列内的棋子总数。
| 输出每次查询的棋子数。 |
|---|
:::success[Solutions]
做法
容易想到的暴力做法是开一个二维数组
但我们敏锐地注意到,每次查询的行和列是相互独立、互不干扰的。并且棋子的总数仅有 lower_bound 和 upper_bound)锁定区间的左右端点,相减即可得到区间内的棋子个数。
时间复杂度
:::
:::success[AC Code]
#include<bits/stdc++.h>
#define Hex return
#define AK 0
#define IOI ;
using namespace std;
const int maxn=300005;
int r[maxn],c[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h,w,n,q;
cin>>h>>w>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>r[i]>>c[i];
}
sort(r+1,r+n+1);
sort(c+1,c+n+1);
while(q--)
{
int op,l,rr;
cin>>op>>l>>rr;
if(op==1)
{
int p1=lower_bound(r+1,r+n+1,l)-r;
int p2=upper_bound(r+1,r+n+1,rr)-r;
cout<<p2-p1<<'\n';
}
else
{
int p1=lower_bound(c+1,c+n+1,l)-c;
int p2=upper_bound(c+1,c+n+1,rr)-c;
cout<<p2-p1<<'\n';
}
}
Hex AK IOI
}
| AC 记录 |
|---|
:::info[什么是 lower_bound 和 upper_bound?]
这是 C++ <algorithm> 库里的二分查找函数。
注意:必须用在已经排好序的数组上。
lower_bound(起始, 结束, x):返回第一个 大于等于x 的元素位置(下界)。upper_bound(起始, 结束, x):返回第一个 严格大于x 的元素位置(上界)。
:::
本题解使用了 deepseek-v4.1-flash 进行润色以保证格式的正确。