HDU 6483 A Sequence Game --ST表+莫队/主席树

莫队推荐链接 

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 NN positive integers A1A1,A2A2,...,AnAn and MM 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 mxmx and the minimal number is mimi. The numbers in this interval are continuous in its integer range means that each number from mimi to mxmx appears at least once in this interval.

Input

The input starts with one line contains exactly one positive integer TT which is the number of test cases. And then there are T cases follow. 
The first line contains two positive integers nn,mm which has been explained above.The second line contains positive integers A1A1,A2A2,...,AnAn. 
Then there will be m lines followed. Each line contains to positive numbers LiLi,RiRiindicating that the ith query’s interval is [LiLi,RiRi].

Output

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.

Sample Input

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

Sample Output

YES
YES
NO
NO
YES
YES

        
  

Hint

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

题意:询问子区间数是否连续(比如1,3,2可以,1,3不行)(提示存在最大和最小值)

这道题不用修改,因此线段树啥的显然大材小用啦~

①求最值,ST表

②求出现次数,可以主席树(or权值线段树),当然这道题莫队更棒。最经典的D-QUERY

存一个莫队的模板

一.莫队

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 1e5 + 10;
int n,m, t,L,R,block,cnt;
int a[N],b[N];
int maxl[N][19], minl[N][19]; 
//map<int,int>mp;TLE
int mp[N];

int lowbit(int x)
{
    return x&(-x);
}

//莫队 
struct node{
	int id,l,r,cnt,block,ma,mi; 
}q[N];


bool cmp(node p,node q){
     return p.block<q.block || p.block==q.block && p.r<q.r;
 }
 
 void add(int x){
 	int t=a[x];
 	if(mp[t]==0)
 	cnt++;
 	mp[t]++;
 }
 
 void del(int x){
 	int t=a[x];
 	if(mp[t]==1)--cnt;
 	mp[t]--;
 }

void S_table(){
	//主函数还有一段初始化! 
    int l = int(log((double)n)/log(2.0));
    for (int j=1;j<=l;j++){
        for (int i=1; i + (1 << (j-1) ) - 1 <=n;++i){
            maxl[i][j] = max(maxl[i][j-1], maxl[i + (1 << (j-1) )][j-1]);
            minl[i][j] = min(minl[i][j-1], minl[i + (1 << (j-1) )][j-1]);
        }
    }
}
int rmq(int l, int r,int &ma,int&mi){
	if(l>r) swap(l,r);
    int k = int(log((double)(r-l+1))/log(2.0));
    ma = max(maxl[l][k], maxl[r - (1<<k) + 1][k]);
    mi = min(minl[l][k], minl[r - (1<<k) + 1][k]);
    
    //printf("Max: %d Min: %d\n", a1, a2);
}
vector<int>v;
int ans[N];

int main() {
	scanf("%d", &t);
	while(t--){
		scanf("%d%d", &n,&m);
		block=sqrt(n);
		v.clear();
		for(int i=1; i<=n; i++) scanf("%d", &a[i]),v.push_back(a[i]), maxl[i][0] = minl[i][0] = a[i];
		S_table();
		
		//离散化,不然mp数组存是否出现,放不下,原数组重复元素其实还在
		
		sort(v.begin(),v.end());
		v.erase(unique(v.begin(),v.end()),v.end());
		for(int i=1;i<=n;i++){
			a[i]=lower_bound(v.begin(),v.end(),a[i])-v.begin()+1;
			mp[i]=0;
		} 
		//莫队 离线处理询问
		L=1,R=0; cnt=0;
		for (int i=0;i<m;i++){
            scanf("%d%d",&q[i].l,&q[i].r);
            q[i].id=i; 
            q[i].block=q[i].l/block;
            	rmq(q[i].l,q[i].r,q[i].ma,q[i].mi);
         }

         sort(q,q+m,cmp);
         
         for (int i=0;i<m;i++){
             while (L<q[i].l) del(L++);
            while (L>q[i].l) add(--L);
            while (R<q[i].r) add(++R);
            while (R>q[i].r) del(R--); 
            if (q[i].ma-q[i].mi+1==cnt)
           ans[q[i].id]=1;
           else ans[q[i].id]=0;
         }
        for (int i=0;i<m;i++)
             if (ans[i]) printf("YES\n");
             else printf("NO\n");
		

	}
}

二.主席树(待更)

猜你喜欢

转载自blog.csdn.net/zjyang12345/article/details/90049434