Ural 1613 For Fans of Statistics(vector應用),ural1613

來源:互聯網
上載者:User

Ural 1613 For Fans of Statistics(vector應用),ural1613

題目:

For Fans of StatisticsTime Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64

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

這個題想多了。。這個方法想到過,但是想到了一種特殊資料,就是當好多數都一樣的時候,然後每次遍曆都要遍曆好多,這樣的話很容易就逾時。。。一直沒想出更好的辦法,就搜了搜題解,結果都是這麼做的。。。好吧。。

這題的思路就是雜湊,利用vector和模數來解決衝突問題。然後找。

代碼如下:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include<algorithm>using namespace std;int a[710000];struct node{    int x, num;};vector <node> vec[110000];const int mod=1e5+7;int main(){    int n, q, i, j, y, l, r, x;    scanf("%d",&n);    node tmp;    for(i=1;i<=n;i++)    {        scanf("%d",&a[i]);        tmp.x=a[i];        tmp.num=i;        vec[a[i]%mod].push_back(tmp);    }    scanf("%d",&q);    while(q--)    {        scanf("%d%d%d",&l, &r, &x);        if(a[l]==x||a[r]==x)        {            printf("1");            continue ;        }        int len=vec[x%mod].size(), flag=0;        for(i=0;i<len;i++)        {            if(vec[x%mod][i].x==x&&vec[x%mod][i].num>=l&&vec[x%mod][i].num<=r)            {                flag=1;                break;            }        }        if(flag)        {            printf("1");        }        else            printf("0");    }    printf("\n");    return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.