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之间,所以我们可以建一个基 ...
随机推荐
- python 中的pipe
from multiprocessing import Process,Queue,Pipe import os def f(q): # q.send([42,None,'hello']) print ...
- 【JavaScript】explode动画
这是一个js实现的粒子聚合文字或图片的动画特效 部分程序如下 n.container = n.container[0] || n.container; /*有且仅有一个container*/ var ...
- Css3 实现丝带效果
代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- CS 20_Overview of Tensorflow
tf.assign(A, new_number): 这个函数的功能主要是把new_number的值向前传递给了A run( fetches, feed_dict=None, options=None, ...
- cocos2d-x 音效中断问题
做跑酷重吃金币播音效时,播放其它音效会使得音效所有中断,最后发现时音效上限的问题,2.2.3默认的似乎是5个音效,改动成50后问题解决. 在java中的org.cocos2dx.lib包下有一个Coc ...
- java点滴之MulticastSocket的使用
一基本概念 该类恰是上文介绍的DatagramSocket的子类. DatagramSocket仅仅同意数据报发送给指定的目标地址,而MulticastSocket能够将数据报以广播的方式发送到多个c ...
- Leetcode名企之路
微信扫码关注,每天推送一道面试题! 公众号:Leetcode名企之路 作者简介 知乎ID: 码蹄疾 码蹄疾,毕业于哈尔滨工业大学. 小米广告第三代广告引擎的设计者.开发者: 负责小米应用商店.日历.开 ...
- 【js】javaScript 执行机制
javascript 是一门单线程语言(按照语句一行一行的执行) let a = '1'; console.log(a); let b = '2'; console.log(b); 这样子正常执行是没 ...
- sublime Text3安装 markdownediting 报错 解决记录
看了一下官方文档,也是醉了,都怪自己的无知. 在安装时候不要打开.md的文件,因为你里面有可能有一些语法错误,所以会导致报错. 解决方法关闭其他文件,在重新安装! 官方解释: 参考文档:Trouble ...
- 为什么浏览器控制台返回不是undefined,而是一串数字
setTimeout( (function(){console.log("ok")} )(), 16) 在浏览器控制台输入这段代码的时候返回的不是"undefined&q ...