牛客网暑期ACM多校训练营(第一场)J. Different Integers(树状数组,离线处理)

题目描述

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.

输入

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.

思路

首先把[1,n]这个区间加一倍
然后原来的询问就变成了[r,n+l]
这样问题就变成了求[r,n+l]这个区间内出现不相同的元素的个数。
现在题目就和SPOJ DQUERY - D-query(树状数组,离线查询)一样了
首先按照右端点从小到大排序,然后依次处理每个区间
树状数组维护以r为结尾的区间元素种类数,从下标1开始扫描,扫描到排序后的第一个区间的右端点,如果当前数字出现过,就在树状数组原先对应位置-1,然后在新的对应位置+1,然后更新当前元素出现的位置。然后每一个区间的结果就是新的sum[r]-sum[l-1],离线保存起来

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 3e5 + 10;
const int inf = 0x3f3f3f3f;
int a[N], n, m, ans[N], c[N];
map<int, int> mp;
int lowbit(int x)
{
    return x & -x;
}
void add(int i, int k)
{
    while (i <= n)
    {
        c[i] += k;
        i += lowbit(i);
    }
}
int sum(int i)
{
    int res = 0;
    while (i > 0)
    {
        res += c[i];
        i -= lowbit(i);
    }
    return res;
}
struct node
{
    int l, r, id;
} query[N];
bool cmp(node x, node y)
{
    return x.r < y.r;
}
int main()
{
    //freopen("in.txt", "r", stdin);
    while (~scanf("%d%d", &n, &m))
    {
        mem(c, 0);
        mp.clear();
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            a[i + n] = a[i];
        }

        for (int i = 0; i < m; i++)
        {
            scanf("%d%d", &query[i].r, &query[i].l);
            query[i].r += n;
            query[i].id = i;
        }
        n <<= 1;
        sort(query, query + m, cmp);

        int cur = 1; //从下标为1的位置开始扫描
        for (int i = 0; i < m; i++)
        {
            for (int j = cur; j <= query[i].r; j++)
            {
                if (mp.find(a[j]) != mp.end())
                {
                    add(mp[a[j]], -1);
                }
                add(j, 1);
                mp[a[j]] = j;
            }
            cur = query[i].r + 1;
            ans[query[i].id] = sum(query[i].r) - sum(query[i].l - 1);
        }
        for (int i = 0; i < m; i++)
            printf("%d\n", ans[i]);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/riba2534/article/details/81129114