如题。

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int Num,CH[12],f,c;
inline void R(int &x){
c=0;f=1;
for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(x=0;c>='0'&&c<='9';c=getchar())(x*=10)+=(c-'0');
x*=f;
}
void P(int x){
if(x<10)putchar(x+'0');
else{P(x/10);putchar(x%10+'0');}
}
#define N 100001
#define M 1000001
#define BN 330
int sum=1,l[BN],num2[N],num[N],r[BN],a[N],b[N],cnts[BN];
int anss[M],n,m;
struct Ask{int l,r,a,b,p;void Read(const int &I){R(l);R(r);R(a);R(b);p=I;}}Q[M];
bool operator < (const Ask &a,const Ask &b)
{return num[a.l]!=num[b.l] ? num[a.l]<num[b.l] : a.r<b.r;}
void makeblock()
{
int sz=sqrt(n); if(!sz) sz=1;
for(;sum*sz<n;++sum)
{
l[sum]=r[sum-1]+1; r[sum]=sum*sz;
for(int i=l[sum];i<=r[sum];++i) num[i]=sum;
}
l[sum]=r[sum-1]+1;
for(int i=l[sum];i<=n;++i) num[i]=sum;
}
inline void Query(const int &L,const int &R,const int &P)
{
int res=0;
if(num[L]==num[R]) for(int i=L;i<=R;++i) res+=(b[i]?1:0);
else
{
for(int i=L;i<=r[num[L]];++i) res+=(b[i]?1:0);
for(int i=l[num[R]];i<=R;++i) res+=(b[i]?1:0);
for(int i=num[L]+1;i<num[R];++i) res+=cnts[i];
}
anss[P]=res;
}
inline void Insert(const int &x){if(!b[x]) ++cnts[num[x]]; ++b[x];}
inline void Delete(const int &x){--b[x]; if(!b[x]) --cnts[num[x]];}
int main()
{
R(n);R(m);
for(int i=1;i<=n;++i) R(a[i]);
for(int i=1;i<=m;++i) Q[i].Read(i);
makeblock(); sort(Q+1,Q+m+1);
for(int i=Q[1].l;i<=Q[1].r;++i) Insert(a[i]);
Query(Q[1].a,Q[1].b,Q[1].p);
for(int i=2;i<=m;++i)
{
if(Q[i].l<Q[i-1].l) for(int j=Q[i-1].l-1;j>=Q[i].l;--j) Insert(a[j]);
else for(int j=Q[i-1].l;j<Q[i].l;++j) Delete(a[j]);
if(Q[i].r<Q[i-1].r) for(int j=Q[i-1].r;j>Q[i].r;--j) Delete(a[j]);
else for(int j=Q[i-1].r+1;j<=Q[i].r;++j) Insert(a[j]);
Query(Q[i].a,Q[i].b,Q[i].p);
}
for(int i=1;i<=m;++i) P(anss[i]),puts("");
return 0;
}

  

【莫队算法】【权值分块】bzoj3809 Gty的二逼妹子序列的更多相关文章

  1. [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业

    [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj   bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...

  2. [bzoj3809]Gty的二逼妹子序列_莫队_分块

    Gty的二逼妹子序列 bzoj-3809 题目大意:给定一个n个正整数的序列,m次询问.每次询问一个区间$l_i$到$r_i$中,权值在$a_i$到$b_i$之间的数有多少个. 注释:$1\le n\ ...

  3. 2019.01.08 bzoj3809: Gty的二逼妹子序列(莫队+权值分块)

    传送门 题意:多组询问,问区间[l,r]中权值在[a,b]间的数的种类数. 看了一眼大家应该都知道要莫队了吧. 然后很容易想到用树状数组优化修改和查询做到O(mnlogamax)O(m\sqrt nl ...

  4. bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...

  5. [BZOJ3809]Gty的二逼妹子序列[莫队+分块]

    题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...

  6. BZOJ3809: Gty的二逼妹子序列

    Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题.   对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数.   为了方 ...

  7. 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块

    [BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...

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

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1072  Solved: 292[Submit][Status][Di ...

  9. Bzoj 3809: Gty的二逼妹子序列 莫队,分块

    3809: Gty的二逼妹子序列 Time Limit: 35 Sec  Memory Limit: 28 MBSubmit: 868  Solved: 234[Submit][Status][Dis ...

随机推荐

  1. Switf与OC混合开发流程

    看着身边越来越多的小伙伴转入Swift,本人也跟随潮流,转战Swift了~下面是初步写入的一个Swift项目框架. 1.创建项目,这个应该不用说了,语言swift 2.CocoaPods 导入第三方 ...

  2. java迭代map

    java迭代map: import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.ut ...

  3. java类中获取WEB-INF路径

    WEB-INF路径 String path = WsTestBOImpl.class.getClass().getResource("/").getPath(); path = p ...

  4. php多虚拟主机配置

    一.配置httpd.conf# Virtual hosts#Include conf/extra/httpd-vhosts.conf       //取消这一行的# 二.配置httpd-vhosts. ...

  5. 百练3383:Cell Phone Network

    传送门:http://bailian.openjudge.cn/practice/3383/ [题解] 题目就是最小支配集. 学习了最小支配集的解法: 树形dp(有空可以推一推) 贪心:DFS遍历后逆 ...

  6. 【CF103D】Time to Raid Cowavans(分块)

    题意: 思路:院赛防AK题,然而还没来得及做就被数据出锅的题坑了…… #include<cstdio> #include<cstring> #include<string ...

  7. POJ2680(动态规划,大数)

    Computer Transformation Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4548   Accepted ...

  8. javascript的有效校验

    //年月日期有效性检验 function yearAndMonthCheck() { var flag = true; var currentyear = new Date().getFullYear ...

  9. oracle 一行转多行

    比如sql: select zyxdm from table where bindid=2265254 查询结果为:1|4|8|9|10 将这个查询结果转成多行,结果如下: ID 1 4 8 9 10 ...

  10. 使用echarts展示线状图信息的时候数据部分数据因为x轴的数据显示不全而隐藏的问题

    在使用echarts来展示数据时,因为数据很多的原因导致x轴显示不全,然后有些数据也隐藏在图表中,所以这个时候我们要在 series 中设置一个属性,让所有的数据都能够展示出来,这里我们需要添加的属性 ...