URAL 1613. For Fans of Statistics (2017 9 6 noip模拟赛T3)

1613. 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
Problem Author: Alexander Ipatov
Problem Source: The 12th Urals Collegiate Programing Championship, March 29, 2008

 

 

 

 

题意

给你n个数字,编号从1开始。   然后m个查询,问你l到r编号中有没有num这个数字。

#include<cstdio>
#include<cstring>
const int N=100010;
const int mod=1e7+19;
struct node
{
    int x,next,order,tag;
}e[N<<1];
struct edgt
{
    int x,next;
}f[N];
int first[mod+10],last[mod+10];
int sum[N];
int map[N],ans[N];
int cnt=0;
inline void add(int d,int w,int di,int flag)
{ 
    e[++cnt].x=w,e[cnt].tag=flag,e[cnt].order=di,e[cnt].next=first[d];first[d]=cnt;
}
int tot=0;
inline int gethash(int x)
{
    int t=x%mod;
    for(int k=last[t];k;k=f[k].next) if(f[k].x==x) return k;
    f[++tot].x=x,f[tot].next=last[t];
    last[t]=tot;
    return tot;
} 
inline int find(int x)
{
    int t=x%mod;
    for(int k=last[t];k;k=f[k].next) if(f[k].x==x) return k;
    return 0;
}
int main()
{
    freopen("statistic.in","r",stdin);
    freopen("statistic.out","w",stdout);
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&map[i]);
    int m;
    scanf("%d",&m);
    int l,r,p;
    for(int i=1;i<=m;i++)
    {
        scanf("%d %d %d",&l,&r,&p);
        add(l-1,p,i,-1);add(r,p,i,1);
    }
    for(int i=1;i<=n;i++)
    {
        int tr=gethash(map[i]);
        sum[tr]++;
        for(int j=first[i];j;j=e[j].next)
        {
            int tr1=find(e[j].x);
            ans[e[j].order]+=e[j].tag*sum[tr1];
        }
    }
    for(int i=1;i<=m;i++) 
    {
        if(ans[i]>0) printf("1");
        else printf("0");
    }
    return 0;
}
View Code

可以去看看Brian551的csdn:

http://blog.csdn.net/brian551/article/details/77876635

还有用stl的:

#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 100010;
int n, m, l, r, a, v[maxn];
pair<int, int> P[maxn];

int main(){
    for (int cases = 1; cases <= 10; cases++){
    
    char inf[30], outf[30];
    sprintf(inf, "statistic%d.in", cases);
    sprintf(outf, "statistic%d.out", cases);
    freopen(inf, "r", stdin); freopen(outf, "w", stdout);
    scanf("%d", &n);
    for (int i = 1; i <= n; i++){
        scanf("%d", &a); v[i - 1] = a;
        P[i - 1] = make_pair(a, i);
    }
    sort(P, P + n);
    for (scanf("%d", &m); m--;){
        scanf("%d%d%d", &l, &r, &a);
        int index = lower_bound(P, P + n, make_pair(a, l)) - P;
        if (P[index].first == a && P[index].second <= r) putchar('1'); else putchar('0');
    }
    printf("\n");
    fclose(stdin); fclose(stdout);

    }
}
View Code

 

posted @ 2017-09-08 14:01  12fs  阅读(160)  评论(0编辑  收藏  举报