Description
Input
Output
Sample Input
4 2
1 2 3 2
1 3
2 4
Sample Output
Yes
No
HINT
题解
莫队。加个标记数组维护该数在区间中出现了几次,再加个变量统计有几个数是重复的。
1 #include<map> 2 #include<set> 3 #include<ctime> 4 #include<cmath> 5 #include<queue> 6 #include<stack> 7 #include<cstdio> 8 #include<string> 9 #include<vector> 10 #include<cstdlib> 11 #include<cstring> 12 #include<iostream> 13 #include<algorithm> 14 #define LL long long 15 #define RE register 16 #define IL inline 17 using namespace std; 18 const int N=1e5; 19 20 int n,q,lim; 21 int cnt[N+5],a[N+5]; 22 struct tt 23 { 24 int l,r,id; 25 }query[N+5]; 26 bool keep[N+5]; 27 28 IL bool comp(const tt &a,const tt &b) {return a.l/lim==b.l/lim ? a.r<b.r : a.l<b.l;} 29 30 int main() 31 { 32 scanf("%d%d",&n,&q); 33 lim=sqrt(n); 34 for (RE int i=1;i<=n;i++) scanf("%d",&a[i]); 35 for (RE int i=1;i<=q;i++) scanf("%d%d",&query[i].l,&query[i].r),query[i].id=i; 36 sort(query+1,query+1+q,comp); 37 int curl=1,curr=0,ans=0,l,r; 38 for (RE int i=1;i<=q;i++) 39 { 40 l=query[i].l,r=query[i].r; 41 while (curl<l) cnt[a[curl]]--,ans-=(cnt[a[curl++]]==1); 42 while (curl>l) cnt[a[--curl]]++,ans+=(cnt[a[curl]]==2); 43 while (curr<r) cnt[a[++curr]]++,ans+=(cnt[a[curr]]==2); 44 while (curr>r) cnt[a[curr]]--,ans-=(cnt[a[curr--]]==1); 45 if (!ans) keep[query[i].id]=1; 46 } 47 for (RE int i=1;i<=q;i++) printf(keep[i] ? "Yes ":"No "); 48 return 0; 49 }