Different Integers

链接:https://www.nowcoder.com/acm/contest/139/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.

题意:

数组从L往左,从R往右组成一个新的数组,在数组中有几个不一样的数

分析:

树状数组离线处理进行更新删除

代码:

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int N = 300010;
int n;
mapmp;
struct BIT
{
    int c[N];
    void init(int n)
    {
        for (int i=0;i<=2*n;i++) c[i]=0;
    }
    void add(int loc,int v)
    {
        while (loc<=2*n)
        {
            c[loc]+=v;
            loc+=loc&(-loc);
        }
    }
    int sum(int loc)
    {
        int ret=0;
        while (loc){
            ret+=c[loc];
            loc-=loc&(-loc);
        }
        return ret;
    }
}T;
struct node
{
    int l,r,id;
    bool operator <(const node&rhs) const{
        //if (l==rhs.l) return r

 

你可能感兴趣的:(树状数组)