标签:hang https register int gcc 完整 ret tchar etc
inline bool cmp(register query a,register query b)
{
return a.bl==b.bl?a.r<b.r:a.bl<b.bl;
}
inline bool cmp(register query a,register query b)
{
return a.bl!=b.bl?a.l<b.l:((a.bl&1)?a.r<b.r:a.r>b.r);
}
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define N 500005
using namespace std;
inline int read()
{
register int x=0,f=1;register char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘)x=(x<<3)+(x<<1)+ch-‘0‘,ch=getchar();
return x*f;
}
inline void write(register int x)
{
if(!x)putchar(‘0‘);if(x<0)x=-x,putchar(‘-‘);
static int sta[25];int tot=0;
while(x)sta[tot++]=x%10,x/=10;
while(tot)putchar(sta[--tot]+48);
}
struct query{
int l,r,id,bl;
}q[N];
int num[1000001],blocksize=0,a[N],ans[N];
inline bool cmp(register query a,register query b)
{
return a.bl!=b.bl?a.l<b.l:((a.bl&1)?a.r<b.r:a.r>b.r);
}
int main()
{
int n=read();
blocksize=sqrt(n);
for(register int i=1;i<=n;++i)
a[i]=read();
int m=read();
for(register int i=1;i<=m;++i)
{
int l=read(),r=read();
q[i]=(query){l,r,i,l/blocksize};
}
sort(q+1,q+1+m,cmp);
int l=1,r=0,sum=0;
for(register int i=1;i<=m;++i)
{
int ll=q[i].l,rr=q[i].r;
while(ll<l)
sum+=!num[a[--l]]++;
while(ll>l)
sum-=!--num[a[l++]];
while(rr>r)
sum+=!num[a[++r]]++;
while(rr<r)
sum-=!--num[a[r--]];
ans[q[i].id]=sum;
}
for(register int i=1;i<=m;++i)
write(ans[i]),printf("\n");
return 0;
}
【题解】Luogu P1972 [SDOI2009]HH的项链
标签:hang https register int gcc 完整 ret tchar etc
原文地址:https://www.cnblogs.com/yzhang-rp-inf/p/10004039.html