# [BZOJ3809]Gty的二逼妹子序列

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

Input

Output

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

Sample Output
2
0
0
2
1
1
1
0
1
2

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
#define lowbit(x) ((x)&(-x))
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
}
inline int frd(){
int x=0,f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')	f=-1;
for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
int x=0,f=1; char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0)	putchar('-'),x=-x;
if (x>9)	print(x/10);
putchar(x%10+'0');
}
const int N=1e5,M=1e6;
int pos[N+10],col[N+10],Ans[M+10],cnt[N+10];
int n,m,size;
struct S1{
int l,r,ID,a,b;
bool operator <(const S1 &tis)const{return pos[l]!=pos[tis.l]?l<tis.l:r<tis.r;}
}A[M+10];
struct S2{
int val[350];
void insert(int x,int v){val[pos[x]]+=v;}
int Query(int l,int r){
int res=0;
for (int i=l;i<=min(pos[l]*size,r);i++)	res+=(cnt[i]>0);
if (pos[l]!=pos[r])
for (int i=(pos[r]-1)*size+1;i<=r;i++)
res+=(cnt[i]>0);
for (int i=pos[l]+1;i<pos[r];i++)	res+=val[i];
return res;
}
}Block;
void insert(int x,int v){
int tmp=cnt[col[x]]+v;
if (cnt[col[x]]&&!tmp)	Block.insert(col[x],-1);
if (!cnt[col[x]]&&tmp)	Block.insert(col[x], 1);
cnt[col[x]]+=v;
}
int main(){
for (int i=1;i<=m;i++)	A[i].insert(i);
sort(A+1,A+1+m);
for (int i=1,l=1,r=0;i<=m;i++){
while (r<A[i].r)	insert(++r, 1);
while (r>A[i].r)	insert(r--,-1);
while (l<A[i].l)	insert(l++,-1);
while (l>A[i].l)	insert(--l, 1);
Ans[A[i].ID]=Block.Query(A[i].a,A[i].b);
}
for (int i=1;i<=m;i++)	print(Ans[i]),putchar('\n');
return 0;
}


posted @ 2018-11-22 21:33  Wolfycz  阅读(151)  评论(0编辑  收藏  举报