【BZOJ 3809】Gty的二逼妹子序列
这个莫队如果用线段树来维护的话,复杂度是$O(n\sqrt{n}logn+qlogn)$
很明显,可以看出来莫队每次$O(1)$的移动因为套上了线段树变成了$O(logn)$,但莫队移动的总数是非常大的,所以乘起来复杂度就上天了。
那么有没有一种方法在修改上能够比线段树更快,同时又能相比暴力较快地回答询问呢?
我们可以用分块,把序列分成$\sqrt{n}$块,修改的复杂度是$O(1)$,回答询问的复杂度是$O(\sqrt{n})$
这样用分块代替线段树总复杂度就变成了$O(n\sqrt{n}+q\sqrt{n})$,然后就AC了~
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100003;
const int M = 1000003;
void read(int &k) {
k = 0; int fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 1) + (k << 3) + c - '0';
k = k * fh;
} struct node {int l, r, a, b, id;} Q[M];
int w[N], bel[N], n, m, sum[N], A[M], c[N]; bool cmp(node A, node B) {return bel[A.l] == bel[B.l] ? A.r < B.r : A.l < B.l;}
void update(int a, int flag) {
c[a] += flag;
if (flag == -1 && c[a] == 0) --sum[bel[a]];
if (flag == 1 && c[a] == 1) ++sum[bel[a]];
}
int QQ(int l, int r) {
int bl = bel[l], br = bel[r], ret = 0;
if (bl == br) {
for(int i = l; i <= r; ++i)
if (c[i] > 0) ++ret;
return ret;
}
for(int i = l; bel[i] == bel[l]; ++i)
if (c[i] > 0) ++ret;
for(int i = r; bel[i] == bel[r]; --i)
if (c[i] > 0) ++ret;
for(int i = bel[l] + 1; i < bel[r]; ++i)
ret += sum[i];
return ret;
} int main() {
read(n); read(m);
for(int i = 1; i <= n; ++i) read(w[i]);
for(int i = 1; i <= m; ++i) {read(Q[i].l); read(Q[i].r); read(Q[i].a); read(Q[i].b); Q[i].id = i;} int t = sqrt(n + 0.5), cnt = 1, tmp = 1;
for(int i = 1; i <= n; ++i) {
bel[i] = tmp;
++cnt; if (cnt > t) {cnt = 1; ++tmp;}
} sort(Q + 1, Q + m + 1, cmp); int l = 1, r = 0, tol, tor;
for(int i = 1; i <= m; ++i) {
tol = Q[i].l; tor = Q[i].r;
while (l < tol) update(w[l++], -1);
while (l > tol) update(w[--l], 1);
while (r < tor) update(w[++r], 1);
while (r > tor) update(w[r--], -1);
A[Q[i].id] = QQ(Q[i].a, Q[i].b);
} for(int i = 1; i <= m; ++i) printf("%d\n", A[i]); return 0;
}
分块大法好
【BZOJ 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 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的二逼妹子序列——莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...
- [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...
- 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之间,所以我们可以建一个基 ...
- BZOJ 3809: Gty的二逼妹子序列 & 3236: [Ahoi2013]作业 [莫队]
题意: 询问区间权值在$[a,b]$范围内种类数和个数 莫队 权值分块维护种类数和个数$O(1)-O(\sqrt{N})$ #include <iostream> #include < ...
- BZOJ.3809.Gty的二逼妹子序列(分块 莫队)
题目链接 /* 25832 kb 26964 ms 莫队+树状数组:增加/删除/查询 都是O(logn)的,总时间复杂度O(m*sqrt(n)*logn),卡不过 莫队+分块:这样查询虽然变成了sqr ...
随机推荐
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- [No000015]坏习惯一大堆?别怕,还有救-坏习惯一堆,怎么好好学习嘛!
- java 27 - 4 反射之 通过反射获取成员变量并使用
类Field: 提供有关类或接口的单个字段的信息,以及对它的动态访问权限. A:获得类的成员变量 数组: 1.getFields(公共类的) 2.getDeclaredFields(所有类型的) B: ...
- Eclipse自动打开实现类原型的工具
http://eclipse-tools.sourceforge.net/implementors/ I always use this implementors plugin to find all ...
- Android三种播放视频的方式
在Android中,我们有三种方式来实现视频的播放: 1.使用其自带的播放器.指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型. 2.使用VideoView来播放. ...
- There was an internal API error.的解决办法
1.当安装应用到4s时,偶尔有些程序会报这个错误,There was an internal API error 解决办法如下: 真机运行下,项目在iOS8.0下运行正常,但是一旦换到iOS7.0和7 ...
- jquery工具方法parseJSON
error : 自定义错误 parseJSON : 字符串转json trim : 去除字符串头尾空字符 parseJSON方法先判断参数是否为字符串,否则返回空对象,再去除字符串头尾空字符,判断是否 ...
- 阿里云消息队列MQ_HTTP接入 for .NetCore 简单例子
, , )).TotalMilliseconds; , , )).TotalMilliseconds; ) ...
- Java命令行的执行参数
Java 程序命令行参数说明 启动Java程序的方式有两种: # starts a Java virtual machine, loads the specified class, and invok ...
- VS XML注释
1.<c> <c>text</c> 其中: text 希望将其指示为代码的文本. 备注 <c> 标记为您提供了一种将说明中的文本标记为代码的方法.使用 ...