【2018黑龙江省赛】A Sequence Game RMQ+离散化+莫队

A Sequence Game

时间限制: 1 Sec  内存限制: 128 MB

题目描述

One day, WNJXYK found a very hard problem on an Online Judge. This problem is so hard that he had been thinking about the solutions for a couple of days. And then he had a surprise that he misunderstood that problem and easily figured out a solution using segment tree. Now he still wonders that solution for the misread problem.
There is a sequence with N positive integers A1,A2,…,An and M queries. Each query will give you an interval [L,R] and require an answer with YES / NO indicates that whether the numbers in this interval are continuous in its integer range. 
Let us assume that the maximal number in an interval is mx and the minimal   number is mi. The numbers in this interval are continuous in its integer range means that each number from mi to mx appears at least once in this interval.

输入

The input starts with one line contains exactly one positive integer T which is the number of test cases. And then there are T cases follow.
The first line contains two positive integers n,m which has been explained above.
The second line contains n positive integers A1,A2,…,An.
Then there will be m lines followed. Each line contains to positive numbers Li,Ri indicating that the i th query’s interval is [Li,Ri].

输出

For each test case, output m line.
Each of following m lines contains a single string “YES”/ “NO” which is the answer you have got.

样例输入

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

样例输出

YES
YES
NO
NO
YES
YES

提示

T=5
1≤n≤100000
1≤Ai≤10^9
1≤m≤100000
The input file is very large, so you are recommend to use scanf() and printf() for IO.

RMQ查询询问区间的最小值和最大值,如果最大值-最小值+1==区间内不重复的数的个数,则为YES,否则为NO

其中RMQ查询后进行离散化,因为数据最大是1e9,离散化后才能在后续的莫队中记录当前数字出现的次数

如果移动指针后该数字的数量为0,则ans--,如果数量为1,则ans++

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN = 100010;
#define memset(a) memset(a,0,sizeof(a))
int dp[MAXN][20],dp2[MAXN][20];
int a[MAXN],k,nownum[MAXN];
int mm[MAXN],temp[MAXN]; 
struct forma
{
    int l,r,num,ans;
}Q[MAXN];
void initRMQ(int n,int b[])
{
    mm[0] = -1;
    for(int i = 1; i <= n; i++)
    {
        mm[i] = ((i&(i-1)) == 0)?mm[i-1]+1:mm[i-1];
        dp[i][0] = b[i];
        dp2[i][0] = b[i];
    }
    for(int j = 1; j <= mm[n]; j++)
        for(int i = 1; i + (1<<j) -1 <= n; i++){
            dp[i][j] = max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
            dp2[i][j] = min(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]);
        }
} 
int rmq(int x,int y)
{
    int k = mm[y-x+1];
    return max(dp[x][k],dp[y-(1<<k)+1][k]);
}
int rmq2(int x,int y)
{
    int k = mm[y-x+1];
    return min(dp2[x][k],dp2[y-(1<<k)+1][k]);
}
bool cmp(forma a,forma b)
{
     if(a.l/k!=b.l/k)
         return a.l/k<b.l/k;
     else
         return a.r/k<b.r/k;
}
bool cmp2(forma a,forma b)
{
     return a.num<b.num;
}
void init()
{
     memset(dp);
     memset(dp2);
     memset(a);
     memset(nownum);
     memset(mm);
}
int main()
{
    int testnum;
    scanf("%d", &testnum);
    while(testnum--)
    {
        init();
        int num,qnum;
        scanf("%d%d", &num, &qnum);
        for(int i=1;i<=num;i++){
            scanf("%d", &a[i]);
            temp[i]=a[i];
        }
        initRMQ(num,a);
        sort(temp+1,temp+1+num);
        int top=unique(temp+1,temp+1+num)-temp-1;
        for(int i=1;i<=num;i++)a[i]=lower_bound(temp+1,temp+1+top,a[i])-temp;
        for(int i=1;i<=qnum;i++){
            scanf("%d%d", &Q[i].l, &Q[i].r);
            Q[i].num=i;
        }
        k=sqrt(num);
        sort(Q+1,Q+1+qnum,cmp);
        int L=1,R=0;
        int ans=0;
        for(int i=1;i<=qnum;i++){
            while(R<Q[i].r){
                nownum[a[R+1]]++;
                if(nownum[a[R+1]]==1)ans++;
                R++;
            }
            while(L>Q[i].l){
                nownum[a[L-1]]++;
                if(nownum[a[L-1]]==1)ans++;
                L--;
            }
            while(R>Q[i].r){
                nownum[a[R]]--;
                if(nownum[a[R]]==0)ans--;
                R--;
            }
            while(L<Q[i].l){
                nownum[a[L]]--;
                if(nownum[a[L]]==0)ans--;
                L++;
            }
            Q[i].ans=ans;
        }
        sort(Q+1,Q+1+qnum,cmp2);
        for(int i=1;i<=qnum;i++){
            int kind=rmq(Q[i].l,Q[i].r)-rmq2(Q[i].l,Q[i].r)+1;
            if(kind==Q[i].ans)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41548233/article/details/82492267