【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 ...
随机推荐
- Python配合BeautifulSoup读取网络图片并保存在本地
本例为Python配合BeautifulSoup读取网络图片,并保存在本地. BeautifulSoup可代替正则表达式,更好地解析Html文本,获取其中的指定内容,如Tag.Property等 # ...
- 夏令时 DST (Daylight Saving Time) java中的夏令时【转】
1916年,德国首先实行夏令时,英国因为怕德国会从中得到更大的效益,因此紧跟着也采取了夏令时 1986年至1991年,中华人民共和国在全国范围实行了六年夏令时 サマータイム 夏時間(日本现在没有实行夏 ...
- 【leetcode】Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- Java for LeetCode 139 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- CUDA学习笔记(一)——CUDA编程模型
转自:http://blog.sina.com.cn/s/blog_48b9e1f90100fm56.html CUDA的代码分成两部分,一部分在host(CPU)上运行,是普通的C代码:另一部分在d ...
- Linux中编译、安装nginx
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器. Nginx 是由Igor Sysoev为俄罗斯访问 ...
- C#控制管理VisualSVN Server
VisualSVN Server可以用WMI接口管理(Windows Management Instrumentation). VisualSVN Server安装的计算机中,位于%VISUALSVN ...
- Java Hour 56 Spring 和 Hibernate 的集成
上一章节我们完成了一个简单的Spring 的试验品,这章要让Spring 上战场了,不要慌,步骤都是一样的. Spring 对 Hibernate 的支持是很多方面的,第一个战场是SessionFac ...
- Wcf for wp8 连接数据库,读写数据库,显示数据库数据(二)
下载: Microsoft® SQL Server® 2012 Express http://www.microsoft.com/zh-cn/download/details.aspx?id=2906 ...
- C语言输出格式总结
转自:http://www.cnblogs.com/scbzljstudy/archive/2011/02/28/1966887.html 1 一般格式 printf(格式控制,输出表列) ...