AC日记——Little Elephant and Array codeforces 221d
2017-05-10 22:31
344 查看
221D - Little Elephant and Array
思路:
莫队;
代码:
思路:
莫队;
代码:
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 100005 struct QueryType { int l,r,id; }; struct QueryType qu[maxn]; int n,m,ai[maxn],bi[maxn],ans[maxn],ci[maxn],num[maxn]; int size,now,bel[maxn],blo; inline void in(int &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0') Cget=getchar(); while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } } bool cmp(QueryType aa,QueryType bb) { if(bel[aa.l]==bel[bb.l]) { if(bel[aa.r]==bel[bb.r]) { if(aa.l==bb.l) return aa.r<bb.r; else return aa.l<bb.l; } else return bel[aa.r]<bel[bb.r]; } else return bel[aa.l]<bel[bb.l]; } inline void updata(int x,int dis) { if(num[ai[x]]==ci[x]) now--; num[ai[x]]+=dis; if(num[ai[x]]==ci[x]) now++; } int main() { in(n),in(m); for(int i=1;i<=n;i++) in(ai[i]),bi[i]=ai[i],ci[i]=ai[i]; sort(bi+1,bi+n+1),size=unique(bi+1,bi+n+1)-bi-1; for(int i=1;i<=n;i++) ai[i]=lower_bound(bi+1,bi+size+1,ai[i])-bi; blo=sqrt(n);for(int i=1;i<=n;i++) bel[i]=(i+1)/blo; for(int i=1;i<=m;i++) in(qu[i].l),in(qu[i].r),qu[i].id=i; sort(qu+1,qu+m+1,cmp);int l=1,r=0; for(int no=1;no<=m;no++) { int ll=qu[no].l,rr=qu[no].r; if(l<ll) for(int i=l;i<ll;i++) updata(i,-1); else for(int i=l-1;i>=ll;i--) updata(i,1); if(r<rr) for(int i=r+1;i<=rr;i++) updata(i,1); else for(int i=r;i>rr;i--) updata(i,-1); l=ll,r=rr,ans[qu[no].id]=now; } for(int i=1;i<=m;i++) printf("%d\n",ans[i]); return 0; }
相关文章推荐
- AC日记——Little Elephant and Function codeforces 221a
- AC日记——Little Elephant and Numbers codeforces 221b
- AC日记——Little Elephant and Shifts codeforces 221e
- AC日记——Little Elephant and Problem codeforces 221c
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- CodeforcesRound #136 (Div. 2) D. Little Elephant andArray
- AC日记——Red and Blue Balls codeforces 399b
- AC日记——Sagheer and Nubian Market codeforces 812c
- Codeforces Round #129 (Div. 1)B Little Elephant and Cards
- Codeforces Round #129 (Div. 1) A Little Elephant and Interval
- CodeForces 221D Little Elephant and Array
- 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- 【Code Forces】221D - Little Elephant and Array(线段树,思维做法)
- Codeforces Round #136 (Div. 2) A. Little Elephant and Function
- AC日记——Andryusha and Colored Balloons codeforces 780c
- Codeforces Round #157 (Div. 2) B. Little Elephant and Magic Square
- Codeforces Round #129 (Div. 2) D. Little Elephant and Cards
- <codeforces>A. Little Elephant and Rozdil
- <codeforces>A. Little Elephant and Rozdil