BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块
BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块
Description
Input
Output
对每个询问,单独输出一行,表示sl...sr中权值∈[a,b]的权值的种类数。
Sample Input
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
0
0
2
1
1
1
0
1
2
HINT
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
char nc() {
static char buf[100000],*p1,*p2;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd() {
register int x=0;
register char s=nc();
while(s<'0'||s>'9')s=nc();
while(s>='0'&&s<='9')x=(x<<3)+(x<<1)+s-'0',s=nc();
return x;
}
#define N 100001
int n,m,c[N],pos[N],L[1050],R[1050],size,block,ansblo[1050],h[N],ans[N*10];
struct A {
int l,r,a,b,id;
}q[N*10];
bool cmp(const A &x,const A &y) {
if(pos[x.l]!=pos[y.l]) return x.l<y.l;
return x.r<y.r;
}
int query(int l,int r) {
int p=pos[l],q=pos[r],ans=0,i;
if(p==q) {
for(i=l;i<=r;i++) {
if(h[i]) ans++;
}
return ans;
}
for(i=p+1;i<q;i++) {
ans+=ansblo[i];
}
for(i=l;i<=R[p];i++) {
if(h[i]) ans++;
}
for(i=L[q];i<=r;i++) {
if(h[i]) ans++;
}
return ans;
}
void del(int x) {
h[x]--;
if(h[x]==0) ansblo[pos[x]]--;
}
void add(int x) {
h[x]++;
if(h[x]==1) ansblo[pos[x]]++;
}
void solve() {
int l=1,r=0,i;
for(i=1;i<=m;i++) {
while(l<q[i].l) del(c[l]),l++;
while(r>q[i].r) del(c[r]),r--;
while(l>q[i].l) l--,add(c[l]);
while(r<q[i].r) r++,add(c[r]);
ans[q[i].id]=query(q[i].a,q[i].b);
}
}
int main() {
n=rd(); m=rd();
int i,j,size=sqrt(n);
block=n/size;
for(i=1;i<=block;i++) {
L[i]=R[i-1]+1; R[i]=size*i;
for(j=L[i];j<=R[i];j++) {
c[j]=rd(); pos[j]=i;
}
}
if(R[block]!=n) {
block++; L[block]=R[block-1]+1; R[block]=n;
for(i=L[block];i<=n;i++) {
c[i]=rd(); pos[i]=block;
}
}
for(i=1;i<=m;i++) {
q[i].l=rd(); q[i].r=rd(); q[i].a=rd(); q[i].b=rd();
q[i].id=i;
}
sort(q+1,q+m+1,cmp);
solve();
for(i=1;i<=m;i++) {
printf("%d\n",ans[i]);
}
}
代码(3263):
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
char nc() {
static char buf[100000],*p1,*p2;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd() {
register int x=0;
register char s=nc();
while(s<'0'||s>'9')s=nc();
while(s>='0'&&s<='9')x=(x<<3)+(x<<1)+s-'0',s=nc();
return x;
}
#define N 100001
int n,m,c[N],pos[N],L[1050],R[1050],size,block,ansblo[1050],h[N],ans[N*10],ans1[N*10],ansblo1[1050];
struct A {
int l,r,a,b,id;
}q[N*10];
bool cmp(const A &x,const A &y) {
if(pos[x.l]!=pos[y.l]) return x.l<y.l;
return x.r<y.r;
}
int query(int l,int r) {
int p=pos[l],q=pos[r],ans=0,i;
if(p==q) {
for(i=l;i<=r;i++) {
if(h[i]) ans++;
}
return ans;
}
for(i=p+1;i<q;i++) {
ans+=ansblo[i];
}
for(i=l;i<=R[p];i++) {
if(h[i]) ans++;
}
for(i=L[q];i<=r;i++) {
if(h[i]) ans++;
}
return ans;
}
int query1(int l,int r) {
int p=pos[l],q=pos[r],i,ans=0;
if(p==q) {
for(i=l;i<=r;i++) {
if(h[i]) ans+=h[i];
}
return ans;
}
for(i=p+1;i<q;i++) ans+=ansblo1[i];
for(i=l;i<=R[p];i++) if(h[i]) ans+=h[i];
for(i=L[q];i<=r;i++) if(h[i]) ans+=h[i];
return ans;
}
void del(int x) {
h[x]--;
if(h[x]==0) ansblo[pos[x]]--;
ansblo1[pos[x]]--;
}
void add(int x) {
h[x]++;
if(h[x]==1) ansblo[pos[x]]++;
ansblo1[pos[x]]++;
}
void solve() {
int l=1,r=0,i;
for(i=1;i<=m;i++) {
while(l<q[i].l) del(c[l]),l++;
while(r>q[i].r) del(c[r]),r--;
while(l>q[i].l) l--,add(c[l]);
while(r<q[i].r) r++,add(c[r]);
ans[q[i].id]=query(q[i].a,q[i].b);
ans1[q[i].id]=query1(q[i].a,q[i].b);
}
}
int main() {
n=rd(); m=rd();
int i,j,size=sqrt(n);
block=n/size;
for(i=1;i<=block;i++) {
L[i]=R[i-1]+1; R[i]=size*i;
for(j=L[i];j<=R[i];j++) {
c[j]=rd(); pos[j]=i;
}
}
if(R[block]!=n) {
block++; L[block]=R[block-1]+1; R[block]=n;
for(i=L[block];i<=n;i++) {
c[i]=rd(); pos[i]=block;
}
}
for(i=1;i<=m;i++) {
q[i].l=rd(); q[i].r=rd(); q[i].a=rd(); q[i].b=rd();
q[i].id=i;
}
sort(q+1,q+m+1,cmp);
solve();
for(i=1;i<=m;i++) {
printf("%d %d\n",ans1[i],ans[i]);
}
}
BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块的更多相关文章
- [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业
[bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...
- BZOJ 3809: Gty的二逼妹子序列 & 3236: [Ahoi2013]作业 [莫队]
题意: 询问区间权值在$[a,b]$范围内种类数和个数 莫队 权值分块维护种类数和个数$O(1)-O(\sqrt{N})$ #include <iostream> #include < ...
- bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...
- 【洛谷4396/BZOJ3236】[AHOI2013]作业(莫队+分块/树状数组/线段树)
题目: 洛谷4396 BZOJ3236(权限) 这题似乎BZOJ上数据强一些? 分析: 这题真的是--一言难尽 发现题面里没说权值的范围,怕出锅就写了离散化.后来经过面向数据编程(以及膜神犇代码)知道 ...
- [BZOJ3236]:[Ahoi2013]作业(莫队+分块)
题目传送门 题目描述 此时已是凌晨两点,刚刚做了$Codeforces$的小$A$掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是一个小时可与做完的数学作业,接下来是分别都是一个小时可 ...
- 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
[BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...
- [AHOI2013]作业 & Gty的二逼妹子序列 莫队
---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...
- 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 链接 分析: 和这道AHOI2013 作业差不多.权值是1~n的,所以对权值进行分块.$O(1)$修改,$O(\sqrt n)$查询. 代码: #include< ...
- BZOJ 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1387 Solved: 400[Submit][Status][Di ...
随机推荐
- Jquery getJSON方法分析
准备工作 ·Customer类 public class Customer { public int Unid { get; set; } public string Customer ...
- tomcat6 高并发配置 与优化
server.xml配置 1. <Connectorport="8080"protocol="HTTP/1.1" 2. maxThreads=&quo ...
- 使用swagger管理接口
swagger 配置 1.pom 增加jar包依赖 <dependency> <groupId>io.springfox</groupId> <artifac ...
- Using Sass with the Angular CLI
https://www.tuicool.com/articles/mauiMzY One of the first things you'll usually do in a project is t ...
- Codeforces Round #479 (Div. 3) C. Less or Equal
题目地址:http://codeforces.com/contest/977/problem/C 题解:给一串数组,是否找到一个数x,找到k个数字<=x,找到输出x,不能输出-1.例如第二组,要 ...
- [Luogu 4135] 作诗
Description 神犇SJY虐完HEOI之后给傻×LYD出了一题: SHY是T国的公主,平时的一大爱好是作诗. 由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M ...
- 前后台分离部署时,Niginx上的部署
upstream bowenpay_backend { server 127.0.0.1:9002; } server { listen 80; server_name wx.bowenpay.com ...
- php获取汉字首字母实例
在我们实际开发工作中,有时候需要获取输入汉字的首字母,然后存到库中,方便用户搜索相关信息,下面给出php代码,留做备用: //新添加获取汉子首字,首字字母 function pinyin($zh){ ...
- 用Python+qrcode库创建一个包含信息的二维码
安装qrcode库和PIL库 在命令行中分别输入pip install qrcode 和pip install pillow 导入库格式如下: import PIL import qrcode 下面以 ...
- 团队项目第二阶段个人进展——Day3
一.昨天工作总结 冲刺第三天,基本完成发布页面的布局 二.遇到的问题 添加照片的样式会随照片增加而改变 三.今日工作规划 分析要封装的数据有哪些,数据如何传到后端服务器中