牛客多校 J Different Integers(线段树)

链接:https://www.nowcoder.com/acm/contest/139/J
来源:牛客网

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
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.

思路:就是把1到i出现的数字种数+n到j出现的数字种数-他们之间的重复数字

#include<bits/stdc++.h>
using namespace  std;
#define ll long long
#define pb push_back
#define inf 2099999999
#define mod 1000000007
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep1(i,a,b) for(int i=a;i>=b;i--)
const int N=1e5+100;
int arr[N];
int vis1[N];
int vis2[N];
int a[N],b[N];
int sum[N*4+5];
int ersum(int l,int r,int x,int y,int t ,int z)
{

    if(x>y||l>r) return 0;
    if(l==x&&y==r)
    {
        sum[t]+=z;
        return sum[t];
    }
    int a, b;
    int mid=(l+r)/2;
    a=ersum(l,mid,max(l,x),min(mid,y),t*2,z);
    b=ersum(mid+1,r ,max(mid+1,x),y,t*2+1,z);
    sum[t]=sum[t*2]+sum[t*2+1];
    return a+b;
}

struct node
{
    int x,y,z,s;
}app[N];


bool cmp(node a,node b)
{
    return a.y>b.y;
}

bool cmp1(node a,node b)
{
    return a.z<b.z;
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&arr[i]);
        int se=0;
        memset(sum,0,sizeof(sum));
        memset(vis1,0,sizeof vis1);
        memset(vis2,0,sizeof vis2);
        for(int i=1;i<=n;i++)
        {
            if(!vis1[arr[i]])
            {
                vis1[arr[i]]=i;
                se++;
            }
            a[i]=se;
        }
        int x,y;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&app[i].x,&app[i].y);
            app[i].z=i;
        }
        sort(app,app+m,cmp);
        int j=n;
        se=0;
        for(int i=0;i<m;i++)
        {
            for(;j>=app[i].y;j--)
            {
                int l=vis1[arr[j]];//如果l在1到i出现过,那么1到app[i].x求和就会加一,
                if(!vis2[arr[j]])//倒序数字第一次出现进行更新,这次更新要么在1到app【i].x或者 1到n,前者算的是重复的,后者则是贡献
                {
                    vis2[arr[j]]=1;
                    ersum(1,n,l,l,1,1);
                }
            }
            int ans=ersum(1,n,1,app[i].x,1,0);
            int sans=ersum(1,n,1,n,1,0);
            app[i].s=a[app[i].x]+sans-ans;
        }

        sort(app,app+m,cmp1);
        for(int i=0;i<m;i++)
        {
           printf("%d\n",app[i].s);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/ffgcc/article/details/81130120