当前位置:网站首页>Counting haybales (difference + discretization)
Counting haybales (difference + discretization)
2022-06-12 09:04:00 【Little wish】
Title Description
link :https://ac.nowcoder.com/acm/contest/7157/H
source : Cattle from
Farmer John has just arranged his N haybales (1≤N≤100,000) at various points along the one-dimensional road running across his farm. To make sure they are spaced out appropriately, please help him answer Q queries (1≤Q≤100,000), each asking for the number of haybales within a specific interval along the road.
Input description
The first line contains N and Q.
The next line contains N distinct integers, each in the range 0…1,000,000,000, indicating that there is a haybale at each of those locations.
Each of the next Q lines contains two integers A and B (0≤A≤B≤1,000,000,000) giving a query for the number of haybales between A and B, inclusive.
Output description
You should write Q lines of output. For each query, output the number of haybales in its respective interval.
The sample input
4 6
3 2 7 5
2 3
2 4
2 5
2 7
4 6
8 10
Sample output
2
2
3
4
1
0
The main idea of the topic
In the interval [0,1000000000] There are some places in the haybales, Find interval [A,B] Inside haybales The number of
Ideas
use sum[] The array holds prefixes and ,sum[i] Indicates the interval [0,i] Inside haybales The number of , The answer for sum[B]-sum[A-1]. But the array subscript cannot reach 1,000,000,000, We noticed the most 100,000 There are haybales, most 100,000 A query , Therefore, most subscripts are actually used 300,000 individual , We can deal with the subscript problem by discretization .
AC Code
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e5+7;
int mp[N*3], cnt, a[N], x[N], y[N], c[N*3], sum[N*3];
int n, Q;
int main() {
freopen("Counting Haybales.txt", "r", stdin);
scanf("%d%d", &n, &Q);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
mp[++cnt] = a[i];
}
for (int i = 1; i <= Q; i++) {
scanf("%d%d", &x[i], &y[i]);
mp[++cnt] = x[i];
mp[++cnt] = y[i];
}
sort(mp+1,mp+cnt+1);
cnt = unique(mp+1,mp+cnt+1)-mp;
for (int i = 1; i <= n; i++) {
int k = lower_bound(mp+1,mp+cnt+1,a[i])-mp;
c[k]++;
}
for (int i = 1; i <= cnt; i++) {
sum[i] = sum[i-1] + c[i];
}
for (int i = 1; i <= Q; i++) {
int xx = lower_bound(mp+1,mp+cnt+1,x[i])-mp;
int yy = lower_bound(mp+1,mp+cnt+1,y[i])-mp;
printf("%d\n", sum[yy]-sum[xx-1]);
}
return 0;
}
Something to watch out for
- After ordering , Use unique() Function de duplication
- lower_bound() Function returns the first position greater than or equal to ,upper_bound() Function returns the first position greater than , So here we use lower_bound() Find the discretized subscript
边栏推荐
- Jenkins Pipeline 语法
- 网页中加载二次元3D虚拟主播源码(1:项目介绍和源码)
- Background location case 1
- Background attribute compound writing
- Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock
- 第七章-更灵活定位内存地址
- 域名映射到指定IP
- Make a simple page with the websql database of HTML5:
- Building a cluster: and replacing with error
- consul 配置相关
猜你喜欢

分库分表会带来读扩散问题?怎么解决?

Node sample background setup

第八章-数据处理的两个基本问题

Inheritance of row height

Introduction to applet cloud development -- questionnaire evaluation applet practice (7)

Construction of memcached cache service under Linux:

Background fixing effect

Flink passes in custom parameters or profiles

Building a cluster: and replacing with error
![Sword finger offer:[day 9 dynamic planning (medium)] --- > maximum sum of continuous subarrays](/img/6b/6dcc86bfe0f48103ef8420b9996c1e.jpg)
Sword finger offer:[day 9 dynamic planning (medium)] --- > maximum sum of continuous subarrays
随机推荐
Bash tutorial
Can you migrate backwards before the first migration in the south- Can you migrate backwards to before the first migration in South?
The classic dog contract of smart contract (I)
The newline character with in the string is converted to an array
UMI packaging and subcontracting, and compressing to gzip
128. 最长连续序列-哈希表
Adjust SVG width and height
Minimum transfer times
Application method of new version UI of idea + use method of non test qualification and related introduction
最少换乘次数
Judge whether the object is empty
Background position - exact units
torch. logical_ And() method
Regular verification user name
机器学习笔记 - 循环神经网络备忘清单
Chapter IV - first procedure
2022 low voltage electrician retraining question bank and online simulation examination
Résoudre le problème de demander à l'élément d'être ouvert lorsque l'unit é est ouverte et que vous n'avez pas été ouvert auparavant (peut - être fermé anormalement auparavant)
Grab screen and ground glass effect
Xshell startup encountered "unable to continue code execution because mfc110.dll cannot be found"