【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 ...
随机推荐
- Django的Context和RequestContext
参考:http://www.dannysite.com/blog/38/ Django的模板渲染中,Context可以用来传递数据,一个Context是一系列变量和值的集合,它和Python的字典有点 ...
- 转mysql复制主从集群搭建
最近搭了个主从复制,中间出了点小问题,排查搞定,记录下来 1环境:虚拟机:OS:centos6.5Linux host2 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 ...
- iOS PickerView动态加载数据
将新的数据放入临时数组 NSMutableArray *tmp=[[NSMutableArray alloc] init]; [tmp addObject:[[NSString alloc] init ...
- php 用封装类的方法操作数据库和批量删除
封装类 <?php class DBDA { public $host="localhost"; //服务器地址 public $uid="root"; ...
- DbHelper-SQL数据库访问助手
using System; using System.Data; using System.Data.SqlClient; namespace Whir.Software.Framework.Ulti ...
- Java Hour 42 fastjson
fastjson 神一样的存在,然后由于缺乏文档,很多功能完全不知道该怎么用. 42.1 字段的大小写问题 刚开始没想到会因为字段的大小写问题而导致反序列化json 失败. @Override pub ...
- web页面自适应手机屏幕宽度
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scal ...
- 理解ASP.NET 5的中间件
今天推荐的这篇文章,讲述了如何实现和使用ASP.NET 5的中间件. 虽然在ASP.NET 5中,微软没有再强调OWIN(Open Web Interface for .NET)及其微软官方的OWIN ...
- spring 源代码地址
spring的源代码地址发生了更改,以前的地址是 https://src.springframework.org/svn/spring-framework/.但现在spring的代码开始使用Git进行 ...
- 【现代程序设计】homework-10
作业地址:http://www.cnblogs.com/xinz/p/3441537.html 进行中...