您的位置:首页 > 其它

AC日记——[SDOI2009]HH的项链 洛谷 P1972

2017-06-05 15:46 253 查看
[SDOI2009]HH的项链

思路;

  莫队;

代码:

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