洛谷 P3184 [USACO16DEC]Counting Haybales数草垛

题目描述

Farmer John has just arranged his NN haybales (1 \leq N \leq 100,0001≤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 QQ queries (1 \leq Q \leq 100,0001≤Q≤100,000 ), each asking for the number of haybales within a specific interval along the road.

农夫John在一条穿过他的农场的路上(直线)放了N个干草垛(1<=N<=100,000),每个干草垛都在不同的点上。为了让每个干草垛之间都隔着一定的距离,请你回答农夫John的Q个问题(1<=Q<=100,000),每个问题都会给出一个范围,询问在这个范围内有多少个干草垛。

(其实就是有一条数轴上有N个不重复的点,再问Q个问题,每个问题是给出一个范围,问此范围内有多少个点?)

(在给出范围的边界上也算)

输入输出格式

输入格式:

The first line contains NN and QQ .

The next line contains NN distinct integers, each in the range 0 \ldots 1,000,000,0000…1,000,000,000 , indicating that there is a haybale at each of those locations.

Each of the next QQ lines contains two integers AA and BB (0 \leq A \leq B \leq 1,000,000,0000≤A≤B≤1,000,000,000 ) giving a query for the number of haybales between AA and BB , inclusive.

第一行包括N和Q

第二行有N个数字,每个数字的范围在0~1,000,000,000,表示此位置有一个干草垛。

接下来的Q行,每行包括两个数字,A和B(0<=A<=B<=1,000,000,000)表示每个询问的范围

输出格式:

You should write QQ lines of output. For each query, output the number of haybales in its respective interval.

总共Q行,每行输出此范围内的干草垛数量

输入输出样例

输入样例#1: 复制

4 6
3 2 7 5
2 3
2 4
2 5
2 7
4 6
8 10

输出样例#1: 复制

2
2
3
4
1
0

思路:区间求和                            难度:普及/提高-

暴力:线段树。只能过一个点,因为测试数据太大(109),而线段树最多能开到107

#include<cstdio>
#define N 10000005
using namespace std;
int n, m, x, y;
struct nond {int ll, rr;int sum;
}tree[4*N];
void up(int now) {tree[now].sum = tree[now*2].sum+tree[now*2+1].sum;
}
void build(int now, int l, int r) {tree[now].ll = l; tree[now].rr = r;if(l == r) {tree[now].sum = 0;return ;}int mid = (l+r) / 2;build(now*2, l, mid);build(now*2+1, mid+1, r);up(now);
}
void change(int now, int s) {if(tree[now].ll == tree[now].rr) {tree[now].sum ++;return ;}int mid = (tree[now].ll+tree[now].rr) / 2;if(s<=mid) change(now*2, s);else change(now*2+1, s);up(now);
}
int query(int now, int l, int r) {if(tree[now].ll==l && tree[now].rr==r) {return tree[now].sum;}int mid = (tree[now].ll+tree[now].rr) / 2;if(l<=mid && mid<r) return query(now*2, l, mid) + query(now*2+1, mid+1, r);else if(r<=mid) return query(now*2, l, r);else return query(now*2+1, l, r);
}
int main() {int a[100005] = {0}, maxn = -1;scanf("%d%d", &n, &m);for(int i = 1; i <= n; i++) {scanf("%d", &a[i]);if(a[i] > maxn) maxn = a[i];}build(1, 1, maxn);for(int i = 1; i <= n; i++) change(1, a[i]);for(int i = 1; i <= m; i++) {scanf("%d%d", &x, &y);if(x > maxn) { printf("0\n"); continue; }printf("%d\n", query(1, x, y));}return 0;
}

线段树代码

正解:运用 c++中的 lower_bound、upper_bound

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 100010
using namespace std;
int n, m, q, tot;
int pos[MAXN], hash[MAXN];
int main() {scanf("%d%d", &n, &q);for(int i = 1; i <= n; i++) scanf("%d", &pos[i]);sort(pos+1, pos+1+n);for(int i = 1; i <= q; i++) {int x, y; scanf("%d%d", &x, &y);cout << upper_bound(pos+1, pos+n+1, y) - lower_bound(pos+1, pos+n+1, x) << ‘\n’;}return 0;
}

AC

PS:lower_bound(val) :返回容器中第一个值大于等于val 的元素的 iterator 位置

upper_bound(val) :返回容器中第一个值大于val 的元素的 iterator 位置

转载于:https://www.cnblogs.com/v-vip/p/8746161.html

洛谷 P3184 [USACO16DEC]Counting Haybales数草垛相关推荐

  1. 信息学奥赛一本通 1316:【例4.6】数的计数(Noip2001) | 1914:【01NOIP普及组】数的计数 | 洛谷 P1028 [NOIP2001 普及组] 数的计算

    [题目链接] ybt 1316:[例4.6]数的计数(Noip2001) ybt 1914:[01NOIP普及组]数的计数 洛谷 P1028 [NOIP2001 普及组] 数的计算 [题目考点] 1. ...

  2. 10.10做题——USACO1.2/洛谷1207回文平方数(Dual Palindromes)

    first thing:我市杨家巷发生一起爆炸事故,死亡17人,愿逝者安息! second thing:明天NOIP初赛,RP++,排名–;分数++; third thing:好像USACO评测系统真 ...

  3. 洛谷P1246C语言,codevs1246 丑数

    题目描述 Description 对于一给定的素数集合 S = {p1, p2, ..., pK}, 来考虑那些质因数全部属于S 的数的集合.这个集合包括,p1, p1p2, p1p1, 和 p1p2 ...

  4. hash:奶牛看地图(洛谷P3405 [USACO16DEC]Cities and States S)

    洛谷传送门 解析 其实就是每组2个长度为2的字符串统计交叉相等的个数 每个序列可以用一个26进制的数来表示,总要加个标签,勉强算hash..(想叫<水题> ) 最大值为26*26(ZZ) ...

  5. 洛谷P3405 [USACO16DEC]Cities and States省市

    P3405 [USACO16DEC]Cities and States省市 题目描述 To keep his cows intellectually stimulated, Farmer John h ...

  6. 洛谷回文数c语言,【普及-】洛谷P1015:回文数 一种解法

    解法 这里考虑到进制的问题,需要把所输入的数字作为字符串(数组名为origin,16进制为大写字母),然后通过转换化为一个个的十进制数位,作为数组的数据元素,这样,在判断是否回文的时候直接从数组两边取 ...

  7. 洛谷P3413 SAC#1 - 萌数(数位dp)

    题目描述 辣鸡蒟蒻SOL是一个傻逼,他居然觉得数很萌! 好在在他眼里,并不是所有数都是萌的.只有满足"存在长度至少为2的回文子串"的数是萌的--也就是说,101是萌的,因为101本 ...

  8. 【洛谷P1430】序列取数【dp】

    题目大意: 题目链接:https://www.luogu.org/problemnew/show/P1430 给定一个长为n的整数序列,由A和B轮流取数(A先取).每个人可从序列的左端或右端取若干个数 ...

  9. [洛谷 OJ]P1015 回文数

    题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个十进制数56,将56加65(即把5656从右向左读),得到121是一个回文数. 又如:对于十进制数 ...

最新文章

  1. oracle11g到底是什么6,Oracle11g六个重要进程
  2. linux安全狗 nginx,linux 下 safedog 防护 Nginx
  3. VTK:Video之FFMPEG
  4. 单实例单向rac搭建gg流
  5. Java 8:CompletableFuture的权威指南
  6. ninja: error: 'LIBSOUNDIO_LIB-NOTFOUND', needed by 'bin/k4aviewer', missing and no known rule to mak
  7. 高并发网络编程之epoll(个人遇到最好理解的一篇文章、易懂)
  8. MySQL表联合查询 理论基础:外键、操作关联表(一)
  9. 实现多线程爬取数据并保存到mongodb
  10. dm8148 开发之---4路解码器tvp5158
  11. 玩转html5(三)---智能表单(form),使排版更加方便
  12. mysql-备份和还原(普通还原和binlog还原)
  13. 1000:有一分数序列: 2/1 3/2 5/3 8/5 13/8 21/13...... 求出这个数列的前N项之和,保留两位小数:
  14. 基于51单片机远近光灯切换电路设计方案
  15. 微信开发者工具调试公众号网页提示: 未绑定为公众号的网页开发者
  16. 【经典收藏】深度技术ghost官方原版XP系统sp3下载地址 ...
  17. 国产Linux操作系统Prayaya Q3国庆发行版发布下载linux操作系统下载
  18. 爱国者新品发布 智能果汁机助力双创 居家创业双赢新产能
  19. 【操作系统】进程:管程
  20. 基础编程题目集(15 分题)

热门文章

  1. Nagios学习实践系列
  2. IO流入门-第十二章-ObjectInputStream_ObjectOutputStream
  3. 【现代程序设计】【homework-07】
  4. 重构机房收费系统(一)
  5. 不更新安装包,实现文件更新 奕婷特许授权发布
  6. PHPstudy搭建本地环境的网页加载速度慢的解决方案
  7. 作为一名前端开发工程师,你必须掌握的WEB模板引擎:Handlebars
  8. NGUI EventDelagate事件委托
  9. 图片预览------photoswipe 使用
  10. 《树莓派实战秘籍》——1.17 技巧17添加重启按钮