G - For Fans of Statistics

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status

Description

Have you ever thought about how many people are transported by trams every year in a city with a ten-million population where one in three citizens uses tram twice a day?
Assume that there are n cities with trams on the planet Earth. Statisticians counted for each of them the number of people transported by trams during last year. They compiled a table, in which cities were sorted alphabetically.
Since city names were inessential for statistics, they were later replaced by numbers from 1 to n. A search engine that works with these data must be able to answer quickly a query of the following type: is there among the cities with numbers from l to r such
that the trams of this city transported exactly x people during last year. You must implement this module of the system.

Input

The first line contains the integer n, 0 < n < 70000. The second line contains statistic data in the form of a list of integers separated with a space. In this list, the ith number is the number of
people transported by trams of the ith city during last year. All numbers in the list are positive and do not exceed 10 9 − 1. In the third line, the number of queries q is given, 0 < q < 70000. The next q lines
contain the queries. Each of them is a triple of integers lr, and x separated with a space; 1 ≤ l ≤ r ≤ n; 0 < x < 10 9.

Output

Output a string of length q in which the ith symbol is “1” if the answer to the ith query is affirmative, and “0” otherwise.

Sample Input

input output
5
1234567 666666 3141593 666666 4343434
5
1 5 3141593
1 5 578202
2 4 666666
4 4 7135610
1 1 1234567
10101

没发现和stl有什么关系。。。。。用二分过的,先以值得大小二分,假设值同样的话,加一组推断,以当前的id与给出的l和r进行二分,看最后能不能得出符合条件的值

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
int id , k ;
} p[80000];
bool cmp(node a,node b)
{
return a.k<b.k || ( a.k==b.k && a.id < b.id );
}
void f(int l,int r,int x,int n)
{
int low = 0 , top = n-1 ;
while(low <= top)
{
int mid = (low+top)/2 ;
if( p[mid].k == x && p[mid].id >= l && p[mid].id <= r )
{
printf("1");
return ;
}
else if( p[mid].k < x || ( p[mid].k == x && p[mid].id < l ) )
low = mid+1 ;
else
top = mid - 1 ;
}
printf("0");
return ;
}
int main()
{
int i , n , m , l , r , x ;
while(scanf("%d", &n)!=EOF)
{
for(i = 0 ; i < n ; i++)
{
scanf("%d", &p[i].k);
p[i].id = i+1 ;
}
sort(p,p+n,cmp);
scanf("%d", &m);
while(m--)
{
scanf("%d %d %d", &l, &r, &x);
f(l,r,x,n);
}
printf("\n");
}
}

最新文章

  1. NodeJs之Path
  2. webpack入门之简单例子跑起来
  3. UIMenuItem
  4. Wix 安装部署教程(十四) -- 多语言安装包之用户许可协议
  5. java之Timer
  6. 百度地图API示例之移动地图
  7. 微信中得到的GPS经纬度放在百度,腾迅地图中不准的原因及处理
  8. sql查询结果本身要被使用两次
  9. mysql 源码调试方法
  10. CentOS 6.5下NFS安装配置
  11. 网站建设常用JQuery插件整理
  12. JS作用域就这么几句话
  13. js判断是否在微信中打开
  14. [python爬虫] Selenium常见元素定位方法和操作的学习介绍
  15. Unity和Android混合开发
  16. libnids
  17. leetcode263
  18. TCP系列44—拥塞控制—7、SACK关闭的快速恢复
  19. openresty 配置 mongodb 可操作插件
  20. Android应用程序用真机调试步骤

热门文章

  1. [Swust OJ 799]--Superprime Rib(DFS)
  2. __call重载方法
  3. 基于visual Studio2013解决C语言竞赛题之0403字符统计
  4. BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
  5. java多线程制作计时器
  6. poj 1256 Anagram(dfs)
  7. pxe网络安装操作系统 原理与详细过程
  8. Unity Notes调制粒子系统的颗粒的最大数目
  9. Swing布局管理器
  10. ThinkPHP快捷函数