码迷,mamicode.com
首页 > 其他好文 > 详细

For Fans of Statistics

时间:2016-08-18 21:11:31      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

For Fans of Statistics

Time limit: 1.0 second
Memory limit: 64 MB
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 109 − 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 < 109.

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

inputoutput
5
1234567 666666 3141593 666666 4343434
5
1 5 3141593
1 5 578202
2 4 666666
4 4 7135610
1 1 1234567
10101

 

分析:map+二分;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t;
string ans;
map<int,set<int> >a;
set<int>::iterator it;
int main()
{
    int i,j;
    scanf("%d",&n);
    rep(i,1,n)scanf("%d",&j),a[j].insert(i);
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int l,r,p;
        scanf("%d%d%d",&l,&r,&p);
        if((it=a[p].lower_bound(l))!=a[p].end()&&*it<=r)
            ans+=1;
        else ans+=0;
    }
    cout<<ans<<endl;
    //system("pause");
    return 0;
}

 

For Fans of Statistics

标签:

原文地址:http://www.cnblogs.com/dyzll/p/5785154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!