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的二逼妹子序列的更多相关文章

  1. BZOJ 3809: Gty的二逼妹子序列

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1387  Solved: 400[Submit][Status][Di ...

  2. Bzoj 3809: Gty的二逼妹子序列 莫队,分块

    3809: Gty的二逼妹子序列 Time Limit: 35 Sec  Memory Limit: 28 MBSubmit: 868  Solved: 234[Submit][Status][Dis ...

  3. BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块

    Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...

  4. BZOJ 3809 Gty的二逼妹子序列(莫队+分块)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...

  5. bzoj 3809 Gty的二逼妹子序列——莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...

  6. [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列

    \(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...

  7. bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...

  8. bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)

    [题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...

  9. BZOJ 3809: Gty的二逼妹子序列 & 3236: [Ahoi2013]作业 [莫队]

    题意: 询问区间权值在$[a,b]$范围内种类数和个数 莫队 权值分块维护种类数和个数$O(1)-O(\sqrt{N})$ #include <iostream> #include < ...

  10. BZOJ.3809.Gty的二逼妹子序列(分块 莫队)

    题目链接 /* 25832 kb 26964 ms 莫队+树状数组:增加/删除/查询 都是O(logn)的,总时间复杂度O(m*sqrt(n)*logn),卡不过 莫队+分块:这样查询虽然变成了sqr ...

随机推荐

  1. HDU 4435 charge-station () bfs图论问题

    E - charge-station Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  2. [ruby on rails] 跟我学之(5)显示所有数据

    之前的index页,显示的是hello world,现在将其修改为显示我们在rails console里面录入的数据. 1. 修改action 如之前的章节<[ruby on rails] 跟我 ...

  3. How to: Set up Openswan L2TP VPN Server on CentOS 6

    Have you ever wanted to set up your own VPN server? By following the steps below, you can set up you ...

  4. 学习jquery mobile

    学习jquery mobile的时间不是很长,在学习的过程当中也遇到了很多令人抓狂的问题,在网上搜索问题答案的时候发现,现在关于jquery mobile的文章还不是很多,所以,我也是一边学习,一边摸 ...

  5. php 二分查找

    <?php /**二分查找:查找一个值在数组中的位置 * @$arr:操作的数组,前提是按顺序排列 * @$val:查找的值 * @$start:查找的起始位置,默认从数组的第一个数找起 * @ ...

  6. Combination Sum | & || & ||| & IV

    Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...

  7. jlink安装

    https://www.segger.com/jlink-software.html?step=1&file=JLink_510p

  8. Django之admin界面恢复及添加数据模型

    引自:http://fl0wjacky.github.io/jekyll_demo/2014/07/14/Django-admin.html Django之admin界面恢复及添加数据模型 Djang ...

  9. Java面向对象的多态

    Java中多态的概念是面向对象中除封装和继承外非常重要的知识点,也是Java面向对象三大特性最后一个特性 多态其实就是指对象存在的多种形态,多态分为引用多态和方法多态 引用多态的含义就是:父类的引用可 ...

  10. Java性能优化权威指南-读书笔记(五)-JVM性能调优-吞吐量

    吞吐量是指,应用程序的TPS: 每秒多少次事务,QPS: 每秒多少次查询等性能指标. 吞吐量调优就是减少垃圾收集器消耗的CPU周期数,从而将更多的CPU周期用于执行应用程序. CMS吞吐调优 CMS包 ...