当前位置:网站首页>求区间(L, R)小于k的数有多少个
求区间(L, R)小于k的数有多少个
2022-08-09 00:12:00 【朴小明】
二位数点典中点。
题目链接:数数
以前写的博客链接:
小沙的remake
那题刚好询问的是小于等于a[i]的数有多少个,所以方法二也很好做,但是这题询问的是其他的数 H[i],所以如果用方法二的话,还得离散化,十分麻烦。
用方法一就简单多了,将数组a中小于H[i]的数的下标pos都插入树状数组中,直接查询que - que(l - 1)就是答案。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 5, mod = 1e9 + 7;
int c[N];
int n;
struct node{
int val, pos;
bool operator < (const node &k) const {
if (val != k.val) return val < k.val;
return pos < k.pos;
}
} a[N];
struct kk{
int l, r, val, pos;
bool operator < (const kk &k) const{
return val < k.val;
}
} d[N];
int ans[N];
void add(int i, int x)
{
while(i <= n){
c[i] += x;
i += (i & -i);
}
}
int que(int i)
{
int ret = 0;
while(i){
ret += c[i];
i -= (i & -i);
}
return ret;
}
signed main()
{
int tt;
cin >> tt;
while(tt--){
int q;
scanf("%lld%lld", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i].val);
a[i].pos = i;
}
for (int i = 1; i <= q; i++){
scanf("%lld%lld%lld", &d[i].l, &d[i].r, &d[i].val);
d[i].pos = i;
}
sort(a + 1, a + 1 + n);
sort(d + 1, d + 1 + q);
int k = 1;
for (int i = 1; i <= q; i++){
while (a[k].val <= d[i].val && k <= n){
add(a[k].pos, 1);
k++;
}
ans[d[i].pos] = que(d[i].r) - que(d[i].l - 1);
}
for (int i = 1; i <= q; i++) printf("%lld ", ans[i]);
puts("");
for (int i = 1; i <= n; i++) {
a[i].val = a[i].pos = d[i].val = 0;
c[i] = 0;
}
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
STM32的HAL库初体会
GaN图腾柱无桥 Boost PFC(单相)三(预测模型)
SyntaxError line:3546,column:96577,SyntaxError: Unexpected token '...'. Expected a property name.
将板子芯片从ST32F101改为STM32F103要改的地方
整流十二 -有效值、平均值、瞬时值、幅值的关系以及相关方法
Why software development methodology make you feel bad?
穿越派·派盘 + OmniFocus = 私人项目管理库
ReflectCubeMap
移动web开发-插件&事件篇
ShadowMap-Example
[Deep Learning] TensorFlow Learning Road 2: Introduction to ANN and TensorFlow Implementation
unity自学笔记--变色跑酷
PhongAndBelinnPhong光照模型
Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [33,755] milliseco
Flutter TextField边框颜色
GaN图腾柱无桥 Boost PFC(单相)五-细节处理
最新豆瓣top250爬虫案例代码分析[注释齐全]
win10出现次磁盘占用率百分之百的情况
ResNet 6大变体对比
顶点动画-VetexAnimation