您的位置:首页 > 其它

牛客网暑期ACM多校训练营(第一场)J题

2018-07-24 11:31 309 查看

链接:https://www.nowcoder.com/acm/contest/139/J
来源:牛客网
牛客网暑期acm多校训练营第一场J题

题目描述

Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l2, r2), ..., count(lq, rq) where count(i, j) is the number of different integers among a1, a2, ..., ai, aj, aj + 1, ..., an.

输入描述:

The input consists of several test cases and is terminated by end-of-file.
The first line of each test cases contains two integers n and q.
The second line contains n integers a1, a2, ..., an.
The i-th of the following q lines contains two integers li and ri.

输出描述:

For each test case, print q integers which denote the result.

示例1

输入

复制

3 2
1 2 1
1 2
1 3
4 1
1 2 3 4
1 3

输出

复制

2
1
3

备注:

* 1 ≤ n, q ≤ 105
* 1 ≤ ai ≤ n
* 1 ≤ li, ri ≤ n
* The number of test cases does not exceed 10.

题意:给你一个n个数的序列,q次询问,每次询问输入l,r,要求输出序列中1-l,r-n项中不同数的个数;

题解:这题其实不难,不需要任何算法,只是很容易超时,将每次询问的l,r保存,再按照l对sqrt(n)的比值以及r从小到大排序,注意要维护询问的顺序,我按照出题人的题解直接按照r的大小排序,t了好几次,排序之后再依次处理询问,只需要定义l,r,遍历原数组,并定义一个数组记录每个数字出现的次数即可。

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
struct node {
    int l, r, id;
}qry[100005];
int a[100005], num[100005], s[100005],b[100005];
bool cmp(node a, node c) {
    if (b[a.l] == b[c.l])return a.r < c.r;
    return b[a.l] < b[c.l];
}
int main() {
    int n, q;
    while (scanf("%d%d", &n, &q) != EOF) {
        int i,len=sqrt(n);
        for (i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            b[i] = (i) / len;
        }
        int l, r;
        for (i = 0; i < q; i++) {
            scanf("%d%d", &l, &r);
            qry[i].l = l - 1;
            qry[i].r = r - 1;
            qry[i].id = i;
        }
        int ans = 0;
        l = -1; r = n;
        memset(num, 0, sizeof num);
        sort(qry, qry + q, cmp);
        for (i = 0; i < q; i++) {
            if (l < qry[i].l) {
                for (; l < qry[i].l;) {
                    l++;
                    if (num[a[l]] == 0)
                        ans++;
                    num[a[l]]++;
                }
            }
            else {
                for (; l > qry[i].l;) {
                    num[a[l]]--;
                    if (num[a[l]] == 0)
                        ans--;
                    l--;
                }
            }
            if (r > qry[i].r) {
                for (; r > qry[i].r;) {
                    r--;
                    if (num[a[r]] == 0)
                        ans++;
                    num[a[r]]++;
                }
            }
            else {
                for (; r < qry[i].r;) {
                    num[a[r]]--;
                    if (num[a[r]] == 0)
                        ans--;
                    r++;
                }
            }
            s[qry[i].id] = ans;
        }
        for (i = 0; i < q; i++) {
            printf("%d\n", s[i]);
        }
    }
    return 0;
}

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: