您的位置:首页 > 其它

AC日记——采花 洛谷 P2056

2017-06-04 21:11 393 查看
采花

思路:

  莫队;

代码:

#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
int bel[maxn];
struct QueryType {
int l,r,id;
bool operator <(const QueryType pos)const
{
if(bel[l]==bel[pos.l]) return r<pos.r;
else return bel[l]<bel[pos.l];
}
};
struct QueryType qu[maxn];
int n,c,m,num[maxn],ans[maxn],now,blo,ai[maxn];
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();
}
}
inline void updata(int x,int di)
{
x=ai[x];
if(di<0)
{
if(num[x]==2) now--;
num[x]--;
}
else
{
if(num[x]==1) now++;
num[x]++;
}
}
int main()
{
in(n),in(c),in(m),blo=sqrt(n);
for(int i=1;i<=n;i++) in(ai[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);int l=1,r=0;
for(int no=1;no<=m;no++)
{
while(r<qu[no].r) updata(++r,1);
while(r>qu[no].r) updata(r--,-1);
while(l>qu[no].l) updata(--l,1);
while(l<qu[no].l) updata(l++,-1);
ans[qu[no].id]=now;
}
for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: