首页 > 代码库 > [Luogu 3901]Difference

[Luogu 3901]Difference

Description

技术分享

Input

技术分享

Output

技术分享

Sample Input

4 21 2 3 21 32 4

Sample Output

YesNo

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 long15 #define RE register16 #define IL inline17 using namespace std;18 const int N=1e5;19 20 int n,q,lim;21 int cnt[N+5],a[N+5];22 struct tt23 {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\n":"No\n");48     return 0;49 }

 

[Luogu 3901]Difference