洛谷 P3901 数列找不同 (普通莫队)
作者:
quiet_
,
2023-04-20 18:31:46
,
所有人可见
,
阅读 197
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
typedef struct query
{
int l;
int r;
int order;
}query_list[N];
query_list q;
int n, m;
int a[N], ans[N], cnt[N];//a[N]是题目中的数组,answer[N] 用来输出最后的答案, cnt[N]用来计数
int block_size;
int curL = 1, curR = n;
int ans1;
bool cmp1(query a, query b)
{
if(a.l / block_size == b.l / block_size)
{
return a.r < b.r;
}
else return a.l < b.l;
}
void del(int x)
{
cnt[a[x]] --;
if(cnt[a[x]] == 0) ans1 --;
}
void add(int x)
{
cnt[a[x]] ++;
if(cnt[a[x]] == 1) ans1 ++;
}
int main()
{
cin >> n >> m;
block_size = sqrt(n);
for(int i = 1 ; i <= n ; i ++) scanf("%d",&a[i]);
for(int i = 1 ; i <= m ; i ++)
{
int x, y;
scanf("%d%d",&x,&y);
q[i].order = i;
q[i].l = x;
q[i].r = y;
}
sort(q + 1, q + m + 1, cmp1);
for(int i = 1 ; i <= m ; i ++)
{
int L = q[i].l;
int R = q[i].r;
while(curL < L) del(curL), curL ++;
while(curL > L) -- curL, add(curL);
while(curR < R) ++ curR, add(curR);
while(curR > R) del(curR), --curR;
if(ans1 == R - L + 1) ans[q[i].order] = 1;
}
for(int i = 1 ; i <= m ; i ++)
{
if(ans[i] == 1) printf("Yes\n");
else printf("No\n");
}
return 0;
}