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

3809: Gty的二逼妹子序列

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

Description

Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题。
对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数。
为了方便,我们规定妹子们的美丽度全都在[1,n]中。
给定一个长度为n(1<=n<=100000)的正整数序列s(1<=si<=n),对于m(1<=m<=1000000)次询问“l,r,a,b”,每次输出sl...sr中,权值∈[a,b]的权值的种类数。

Input

第一行包括两个整数n,m(1<=n<=100000,1<=m<=1000000),表示数列s中的元素数和询问数。
第二行包括n个整数s1...sn(1<=si<=n)。
接下来m行,每行包括4个整数l,r,a,b(1<=l<=r<=n,1<=a<=b<=n),意义见题目描述。
保证涉及的所有数在C++的int内。
保证输入合法。

Output

对每个询问,单独输出一行,表示sl...sr中权值∈[a,b]的权值的种类数。

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

HINT

样例的部分解释:
5 9 1 2
子序列为4 1 5 1 2
在[1,2]里的权值有1,1,2,有2种,因此答案为2。
3 4 7 9
子序列为5 1
在[7,9]里的权值有5,有1种,因此答案为1。
4 4 2 5
子序列为1
没有权值在[2,5]中的,因此答案为0。
2 3 4 7
子序列为4 5
权值在[4,7]中的有4,5,因此答案为2。
建议使用输入/输出优化。

Source

Solution

分块+莫队

很好想,一开始看错题,没写莫队,直接分块+lower_bound然后发现过不了样例...

其实挺好想,对权值分块,带上莫队搞搞就好...

启发:

序列操作统计颜色,可以优先往分块+莫队上搞

莫队的时候,询问的排序很关键..(手误打反了第1,2关键字,居然能过3组..)

Code

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
#define maxn 100010
#define maxm 1000100
int n,m,a[maxn],pos[maxn],num[maxn],an[maxn],bll,bln;
struct Asknode
{
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=;
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]-)*bll+; i<=r; i++) if (num[i]) ans++;
}
for (int i=pos[l]+; i<=pos[r]-; i++) ans+=an[i];
return ans;
}
void move1(int x)
{
num[a[x]]--; if (num[a[x]]==) an[pos[a[x]]]--;
}
void move2(int x)
{
num[a[x]]++; if (num[a[x]]==) an[pos[a[x]]]++;
}
int nl=,nr=;
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()
{
n=read(),m=read(); bll=sqrt(n/); if (n%bll) bln=n/bll+; else bln=n/bll;
// printf("%d %d\n",bll,bln);
for (int i=; i<=n; i++) a[i]=read(),pos[i]=(i-)/bll+;
// for (int i=1; i<=n; i++) printf("%d\n",pos[i]);
for (int i=; i<=m; i++)
q[i].l=read(),q[i].r=read(),q[i].a=read(),q[i].b=read(),q[i].id=i;
sort(q+,q+m+);
for (int i=; i<=m; i++) work(i);
for (int i=; i<=m; i++) printf("%d\n",ans[i]);
return ;
}

%%%Gty大哥,%%%块爷,%%%Basker学长

前排围观自己的傻逼错误:

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

上一篇:【UWP】FlipView绑定ItemsSource,Selectedindex的问题


下一篇:未能加载文件或程序集“BLL”或它的某一个依赖项。生成此程序集的运行时比当前加载的运行时新,无法加载此程序集。