# 【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法

## 3809: Gty的二逼妹子序列

Time Limit: 80 Sec  Memory Limit: 28 MB
Submit: 1072  Solved: 292
[Submit][Status][Discuss]

## Description

Autumn和Bakser又在研究Gty的妹子序列了！但他们遇到了一个难题。

## Sample Input

10 10
4 4 5 1 4 1 5 1 2 1
5 9 1 2
3 4 7 9
4 4 2 5
2 3 4 7
5 10 4 4
3 9 1 1
1 4 5 9
8 9 3 3
2 2 1 6
8 9 1 4

2
0
0
2
1
1
1
0
1
2

5 9 1 2

3 4 7 9

4 4 2 5

2 3 4 7

## Code

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define maxn 100010
#define maxm 1000100
int n,m,a[maxn],pos[maxn],num[maxn],an[maxn],bll,bln;
{
int l,r,a,b,id;
bool operator < (const Asknode & A) const
{
if (pos[l]==pos[A.l]) return r<A.r;
return l<A.l;
}
}q[maxm];
int ans[maxm],qn;
int Query(int l,int r)
{
int ans=0;
if (pos[l]==pos[r])
for (int i=l; i<=r; i++) if (num[i]) ans++; else continue;
else
{
for (int i=l; i<=pos[l]*bll; i++) if (num[i]) ans++;
for (int i=(pos[r]-1)*bll+1; i<=r; i++) if (num[i]) ans++;
}
for (int i=pos[l]+1; i<=pos[r]-1; i++) ans+=an[i];
return ans;
}
void move1(int x)
{
num[a[x]]--; if (num[a[x]]==0) an[pos[a[x]]]--;
}
void move2(int x)
{
num[a[x]]++; if (num[a[x]]==1) an[pos[a[x]]]++;
}
int nl=1,nr=0;
void work(int x)
{
int L=q[x].l,R=q[x].r,id=q[x].id;
while (nl<L) move1(nl),nl++;
while (nr>R) move1(nr),nr--;
while (nl>L) nl--,move2(nl);
while (nr<R) nr++,move2(nr);
ans[id]=Query(q[x].a,q[x].b);
//    printf("%d %d %d %d %d\n",x,L,R,id,ans[id]);
}
int main()
{
//    printf("%d %d\n",bll,bln);
for (int i=1; i<=n; i++) a[i]=read(),pos[i]=(i-1)/bll+1;
//    for (int i=1; i<=n; i++) printf("%d\n",pos[i]);
for (int i=1; i<=m; i++)
}