【BZOJ】3809: Gty的二逼妹子序列
http://www.lydsy.com/JudgeOnline/problem.php?id=3809
题意:n个元素(1<=n<=100000)每个元素有一权值<=n。q个询问,1<=q<=1000000,每次询问区间[l, r]的权值在区间[a, b]的种类数。时限35s...
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <sstream>
using namespace std;
typedef long long ll;
#define pb push_back
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline int getint() { static int r, k; r=0,k=1; static char c; c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=100005; int n, pos[N], cnt[N], sum[N], m, ans[N*10], w[N], sq;
struct dat { int l, r, a, b, id; }a[N*10];
inline bool cmp(const dat &a, const dat &b) { return pos[a.l]==pos[b.l]?a.r<b.r:a.l<b.l; }
void init() { sq=sqrt(n+0.5); for1(i, 1, n) pos[i]=(i-1)/sq+1; }
int ask(int l, int r) {
int bl=pos[l], br=pos[r], ret=0;
if(bl==br) { for1(i, l, r) ret+=cnt[i]>0; return ret; }
for1(i, bl+1, br-1) ret+=sum[i];
bl=sq*bl;
br=sq*(br-1)+1;
for1(i, l, bl) ret+=cnt[i]>0;
for1(i, br, r) ret+=cnt[i]>0;
return ret;
}
void update(int x, int k) {
cnt[x]+=k;
if(k==1 && cnt[x]==1) ++sum[pos[x]];
if(k==-1 && cnt[x]==0) --sum[pos[x]];
} int main() {
read(n); read(m);
for1(i, 1, n) read(w[i]);
init();
for1(i, 1, m) read(a[i].l), read(a[i].r), read(a[i].a), read(a[i].b), a[i].id=i;
sort(a+1, a+1+m, cmp);
int l=1, r=0;
for1(i, 1, m) {
int nl=a[i].l, nr=a[i].r;
while(l<nl) update(w[l++], -1);
while(l>nl) update(w[--l], 1);
while(r<nr) update(w[++r], 1);
while(r>nr) update(w[r--], -1);
ans[a[i].id]=ask(a[i].a, a[i].b);
}
for1(i, 1, m) printf("%d\n", ans[i]);
return 0;
}
好神的一题orz
一开始写裸的线段树+莫队t了....没分析复杂度果然会跪...如果是这样写,单次查询$O(logn)$,修改$O(logn)$,而有$q$个询问,所以总的复杂度为$O(n^{1.5}logn+qlogn)$...tle成翔....单组极限数据大概就要5s以上的样子?
于是看题解,orz rausen orz hzwer
因为权值的范围在n内,我们可以将权值也分块!即统计对应块内的种类数即可...
这样的话,单次修改可以到$O(1)$,单次查询$O(n^{0.5})$,总的复杂度为$O(n^{1.5}+qn^{0.5})$,单组数据大概3s左右....
于是就水过了...
听说xyz大爷的集训队论文有更优越的做法,先留个坑...
【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 ...
随机推荐
- poj1860 bellman—ford队列优化 Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22123 Accepted: 799 ...
- linux 搭建hexo博客
搭建环境: CentOS 6.5 1.安装git的编译包 yum -y install gcc zlib-devel openssl-devel perl cpio expat-devel gette ...
- 坚持不懈之linux haproxy的配置文件关键字查询手册
1.关键词balance balance用于定义负载均衡的算法,可用于defaults.listen和backend中. balance使用方法如下: balance <algorithm> ...
- CentOS6.5以runlevel 3开机时自动连接某无线设置示例
[参考]http://blog.csdn.net/simeone18/article/details/8580592 [方法一] 假设无线网卡代号为wlan0,无线AP的essid为:TheWiFi, ...
- Ubuntu12.04安装ia32-libs
sudo apt-get install libc6:i386 sudo -i cd /etc/apt/sources.list.d// care for old-releases.ubuntu.co ...
- July 18th, Week 30th Monday, 2016
Truth needs no color; beauty, no pencil. 真理不需要色彩,美丽无需涂饰. Most of the time, giving some color to trut ...
- win7下IIS配置MVC项目
第一步:添加MVC程序映射 1.双击打开,如下图: 2. 点击界面右边操作中的:添加脚本映射 请求路径:* 可执行文件路径:C:\Windows\Microsoft.NET\Framework\v4. ...
- 支持向量机(SVM)简介
主要内容 一:SVM简介 二:线性分类 三:分类间隔 四:核函数 五:松弛变量 SVM简介 支持向量机(support vector Machine)是由Cortes和Vapnik于1995年首先提出 ...
- cVim—Chrome上更强大的vim插件
cVim——Chrome上更强大的vim插件 介绍 也许很多人在chrome上都用过类似Vimium, ViChrome的插件,这些插件的目的都差不多,就是在浏览器中提供一些类似vim的操作来提高效率 ...
- mysql:表注释和字段注释
mysql:表注释和字段注释 1 创建表的时候写注释 create table test1 ( field_name int comment '字段的注释' )comment='表的注释'; 2 修改 ...