3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列
分析:
和这道AHOI2013 作业差不多。权值是1~n的,所以对权值进行分块。$O(1)$修改,$O(\sqrt n)$查询。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int bel[N], sum[N], a[N], Ans[N * ], cnt[N], B;
struct Que{
int l, r, a, b, id;
bool operator < (const Que &A) const {
return bel[l] == bel[A.l] ? r < A.r : bel[l] < bel[A.l];
}
}Q[N * ]; inline void add(int x) {
cnt[x] ++;
if (cnt[x] == ) sum[bel[x]] ++;
}
inline void del(int x) {
cnt[x] --;
if (cnt[x] == ) sum[bel[x]] --;
}
inline int query(int l,int r) {
int res = ;
for (int i = l, lim = min(r, bel[l] * B); i <= lim; ++i) res += (cnt[i] > );
if (bel[l] != bel[r])
for (int i = (bel[r] - ) * B + ; i <= r; ++i) res += (cnt[i] > );
for (int i = bel[l] + ; i <= bel[r] - ; ++i) res += sum[i];
return res;
}
int main() {
int n = read(), m = read(); B = sqrt(n);
for (int i = ; i <= n; ++i) a[i] = read();
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;
for (int i = ; i <= n; ++i) bel[i] = (i - ) / B + ;
sort(Q + , Q + m + );
int L = , R = ;
for (int i = ; i <= m; ++i) {
while (L > Q[i].l) add(a[--L]);
while (R < Q[i].r) add(a[++R]);
while (L < Q[i].l) del(a[L++]);
while (R > Q[i].r) del(a[R--]);
Ans[Q[i].id] = query(Q[i].a, Q[i].b);
}
for (int i = ; i <= m; ++i) printf("%d\n",Ans[i]);
return ;
}
3809: Gty的二逼妹子序列的更多相关文章
- BZOJ 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1387 Solved: 400[Submit][Status][Di ...
- Bzoj 3809: Gty的二逼妹子序列 莫队,分块
3809: Gty的二逼妹子序列 Time Limit: 35 Sec Memory Limit: 28 MBSubmit: 868 Solved: 234[Submit][Status][Dis ...
- 【BZOJ 3809】 3809: Gty的二逼妹子序列 (莫队+分块)
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1728 Solved: 513 Description Autumn ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- BZOJ 3809 Gty的二逼妹子序列(莫队+分块)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...
- 【BZOJ】3809: Gty的二逼妹子序列
http://www.lydsy.com/JudgeOnline/problem.php?id=3809 题意:n个元素(1<=n<=100000)每个元素有一权值<=n.q个询问, ...
- bzoj 3809 Gty的二逼妹子序列——莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...
- bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...
- bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)
[题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...
随机推荐
- 协议森林09 爱的传声筒 (TCP连接)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在TCP协议与"流"通信中,我们概念性的讲解了TCP通信的方式 ...
- 发送邮件 html格式
下面黄色代码为实现发送邮件 FileStream fs = new FileStream(Server.MapPath("../ImportUserIOExcel/productConsul ...
- robotframwork接口测试(三)—HttpLibrary.HTTP安装
1. 安装: 1)可以用命令:pip install robotframework-httplibrary(或pip install --upgrade robotframework-httplib ...
- Java8 Stream()关于在所有用户的所有上传记录中,找出每个用户最新上传记录
原创文章:转载请标明出处 https://www.cnblogs.com/yunqing/p/9504196.html 首先分析相当于如下,在所有的猫中,每个名字的猫都保留年龄最小的一个 import ...
- jenkins持续集成工作原理
转载https://www.cnblogs.com/liyuanhong/p/6548925.html 片段 这里是选择Gitlab作为git server.Gitlab的功能和Github差不多,但 ...
- 修改微软RDP远程桌面端口
远程桌面服务所使用的通信协议是Microsoft定义RDP(Remote Desktop Protocol)协议,RDP协议的TCP通信端口号是3389. 有时候为了安全起见,或者其他的需要,我们常需 ...
- Linux 嵌入式 开发环境 交叉编译安装
1.安装 Ubuntu 系统 安装完毕,系统 提示 重启,这个时候 请拔掉U盘,进行重启 OK. 2.安装 NFS 服务 3.安装 openssh服务 4.开启openSSH服务 5.就可以使用 Wi ...
- Java Activiti 流程审批 后台框架源码 springmvc SSM 工作流引擎
即时通讯:支持好友,群组,发图片.文件,消息声音提醒,离线消息,保留聊天记录 工作流模块-------------------------------------------------------- ...
- centos7字体中英文转化
[root@localhost oracle]#vi /etc/locate.conf,把里面的内容改为: 转化为英文: LANG="en_US.UTF-8"LANGUAGE=&q ...
- BZOJ 2648: SJY摆棋子(K-D Tree)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 6051 Solved: 2113[Submit][Status][Discuss] Descript ...