Ural 1613 For Fans of Statistics (vector application), ural1613
Question:
For Fans of Statistics
Time Limit:1000 MS
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
NCities 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
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
LTo
RSuch that the trams of this city transported exactly
XPeople 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,
ITh number is the number of people transported by trams of
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
QIs given, 0 <
Q<1, 70000. The next
QLines contain the queries. Each of them is a triple of integers
L,
R, And
XSeparated with a space; 1 ≤
L≤
R≤
N ; 0 <
X<10 9.
Output
Output a string of length
QIn which
ITh symbol is "1" if the answer to
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 |
I think too much about this question .. This method was thought of, but thought of a special type of data, that is, when most of the data is the same, and then a lot of traversal is required each time, so that it is easy to time out... I have never come up with a better solution, so I searched for questions and found the results... Okay ..
The idea of this question is to hash and use vector and modulo to solve the conflict problem. Then find.
The Code is as follows:
#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;}