STL--G - For Fans of Statistics(兩個判斷條件-二分)

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   strong   io   for   

G - For Fans of StatisticsTime Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64uSubmit 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 l, r, 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
51234567 666666 3141593 666666 434343451 5 31415931 5 5782022 4 6666664 4 71356101 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");    }}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.