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之间,所以我们可以建一个基 ...
随机推荐
- luogu P3381【模板】最小费用最大流
嘟嘟嘟 没错,我开始学费用流了! 做法也是比较朴素的\(spfa\). 就是每一次以费用为权值跑一遍\(spfa\)找到一条最短路,然后把这条道全流满,并把这一次的流量和费用累加到答案上.因此我们需要 ...
- Level/levelup-1-简介
https://github.com/Level/levelup A node.js wrapper for abstract-leveldown compliant stores 一个为实现抽象le ...
- Apollo GPS调试笔记
前言:在Apollo美研团队的支持下,最近完成了Apollo推荐的Novatel PP7与我们的基站调试,在这里对Apollo的笔记做一个补充,希望以后的开发者不用在踩我们踩过的坑. 在自己调导航(h ...
- 通讯协议(一)HTTP协议
协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器.目前我们使 ...
- iOS OC与JS的交互(JavaScriptCore实现)
本文包括JS调用OC方法并传值,OC调用JS方法并传值 本来想把html放进服务器里面,然后访问,但是觉得如果html在本地加载更有助于理解,特把html放进项目里 HTML代码 <!DOCTY ...
- -L -Wl,-rpath-link -Wl,-rpath区别精讲
目录 前言 源码准备 源码内容 尝试编译,保证源码没有问题 编译 首先编译world.c 编译并链接hello.c 调试编译test.c 结论 转载请注明出处,谢谢 https://www.cnblo ...
- $.ajax(),$.get(),$.post()的区别,以及一些参数注意规则
$.ajax()方法和$.get(),$.post()方法的对比 $.ajax()方法是最完整的写法,可以完成所有的ajax请求(包含get类型和post类型) $.get()和$.post()都是简 ...
- Bootstrap源码解读之栅格化篇
本文纯属自己研究所写笔记,如果有错误还请多多指教提出 版心(container) 版心:class名为.container的容器,其版心的宽度在各个屏幕设备下是不一样的值,版心两边就是留白. 各尺寸下 ...
- Elasticsearch入门和查询语法分析(ik中文分词)
全文搜索现在已经是很常见的功能了,当然你也可以用mysql加Sphinx实现.但开源的Elasticsearch(简称ES)目前是全文搜索引擎的首选.目前像GitHub.维基百科都使用的是ES,它可以 ...
- android4.4 重启的开机不播放开机铃声,按power键的开机播放开机铃声
平台:A33Android4.4Linux3.4 功能描述:实现重启的开机不播放开机铃声,按power键的开机播放开机铃声 一,无论关机还是重启都会经过rebootOrShutdown这个方法,在方法 ...