BZOJ 3809 Gty的二逼妹子序列(莫队+分块)
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3809
【题目大意】
给定一个长度为n(1<=n<=100000)的正整数序列s(1<=si<=n),
对于m(1<=m<=1000000)次询问“l,r,a,b”,
每次输出sl...sr中,权值∈[a,b]的权值的种类数。
【题解】
用莫队维护,对于修改操作用树状数组,
发现复杂度为O(msqrtnlogn),难以接受,
考虑用分块处理整数序列,修改操作O(1),查询操作O(sqrt(n))
总复杂度O(msqrt(n)+mlog(m))
【代码】
#include <cstdio>
#include <algorithm>
#include <cmath>
const int N=100001,M=1000001;
using namespace std;
typedef long long LL;
int size[N],ans[M],pos[N],color[N],c[400],n,m,limit,bl[400],br[400];
struct Q{
int l,r,id,a,b;
friend bool operator < (const Q &a,const Q &b){
return pos[a.l]<pos[b.l]||(pos[a.l]==pos[b.l]&&a.r<b.r);
}
}ask[M];
bool cmp(const Q &a,const Q &b){return a.id<b.id;}
void read(int&a){
char ch;while(!((ch=getchar())>='0')&&(ch<='9'));
a=ch-'0';while(((ch=getchar())>='0')&&(ch<='9'))a*=10,a+=ch-'0';
}
int query(int x,int y){
int res=0;
int L=pos[x],R=pos[y];
for(int i=L+1;i<R;i++)res+=c[i];
if(L==R){for(int i=x;i<=y;i++)if(size[i])res++;}
else{
for(int i=x;i<=br[L];i++)if(size[i])res++;
for(int i=bl[R];i<=y;i++)if(size[i])res++;
}return res;
}
void modify(int u,int x){
if(size[color[u]]==1&&x==-1)c[pos[color[u]]]--;
if(size[color[u]]==0&&x==1)c[pos[color[u]]]++;
size[color[u]]+=x;
}
int main(){
read(n);read(m);
limit=(int)sqrt(n+0.5);
for(int i=1;i<=n;i++){read(color[i]);pos[i]=(i-1)/limit+1;}
for(int i=1;i<=n;i++){br[pos[i]]=i;if(!bl[pos[i]])bl[pos[i]]=i;}
for(int i=1;i<=m;i++){read(ask[i].l);read(ask[i].r);read(ask[i].a);read(ask[i].b);ask[i].id=i;}
sort(ask+1,ask+m+1);
for(int i=1,l=1,r=0;i<=m;i++){
while(r<ask[i].r)modify(++r,1);
while(r>ask[i].r)modify(r--,-1);
while(l<ask[i].l)modify(l++,-1);
while(l>ask[i].l)modify(--l,1);
ans[ask[i].id]=query(ask[i].a,ask[i].b);
}for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
return 0;
}
BZOJ 3809 Gty的二逼妹子序列(莫队+分块)的更多相关文章
- Bzoj 3809: Gty的二逼妹子序列 莫队,分块
3809: Gty的二逼妹子序列 Time Limit: 35 Sec Memory Limit: 28 MBSubmit: 868 Solved: 234[Submit][Status][Dis ...
- bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...
- bzoj 3809 Gty的二逼妹子序列——莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- [BZOJ3809]Gty的二逼妹子序列[莫队+分块]
题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...
- 【BZOJ3809】Gty的二逼妹子序列 莫队 分块
题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出区间\([l,r]\)有多少范围在\([a,b]\)的权值. \(n\leq 100000, ...
- BZOJ 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1387 Solved: 400[Submit][Status][Di ...
- [AHOI2013]作业 & Gty的二逼妹子序列 莫队
---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...
- [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...
随机推荐
- Frogs' Neighborhood(POJ1659+Havel-Hakimi定理)
题目链接:http://poj.org/problem?id=1659 题目: 题意:根据他给你的每个点的度数构造一张无向图. 思路:自己WA了几发(好菜啊……)后看到discuss才知道这个要用Ha ...
- Less & Sass
CSS不是一种编程语言.它开发网页样式,但是没法用它编程.也就是说,CSS基本上是设计师的工具,它没有变量,也没有条件语句,只是一行行单纯的描述.有人就开始为CSS加入编程元素,这被叫做"C ...
- 移动端测试===Android内存泄露和GC机制(转)
本文转自:https://www.testwo.com/article/1153 1.前言 Hello,小伙伴们,相信大家在项目测试中都遇到过内存泄露问题,小编也着实爬过很多坑.比如小编所测项目,更换 ...
- monkey测试===关于monkey测试的介绍
https://www.cnblogs.com/lauren1003/p/6193277.html
- interrupted()和isInterrupted()比较+终止线程的正确方法+暂停线程
interrupted():测试当前线程[运行此方法的当前线程]是否已经是中断状态,执行后具有将状态标志清除为false的功能. isInterrupted():测试线程对象是否已经是中断状态,但不清 ...
- 【bzoj1649】Cow Roller Coaster
傻逼dp题. dp[i][j]表示用了i长度已花费成本j所能得到的价值. 然后枚举一下铁轨随便做了. 不行就sort一下. #include<bits/stdc++.h> #define ...
- caffe Python API 之 数据输入层(Data,ImageData,HDF5Data)
import sys sys.path.append('/projects/caffe-ssd/python') import caffe4 net = caffe.NetSpec() 一.Image ...
- JS实现全选与取消 Jquery判断checkbox是否被选中
1.JS实现checkbox全选与取消 <body> <input type="checkbox" name="select_all"/> ...
- LeetCode解题报告—— Number of Islands & Bitwise AND of Numbers Range
1. Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of island ...
- 《逐梦旅程 WINDOWS游戏编程之从零开始》笔记3——输入消息处理,物理建模与粒子系统初步
第7章 Windows游戏输入消息处理 1. 键盘消息处理 之前提到的窗口过程函数有两参数与消息输出有关——wParam和llParam LRESULT CALLBACK WindowProc( _I ...