莫队..用两个树状数组计算.时间复杂度应该是O(N1.5logN). 估计我是写残了...跑得很慢...

-------------------------------------------------------------------------

#include<bits/stdc++.h>
 
using namespace std;
 
#define lowbit(x) ((x) & -(x))
 
const int maxn = 100009;
const int maxm = 1000009;
 
int N, M, block[maxn], seq[maxn], ans[maxm][2], L, R;
 
inline int read() {
int ans = 0;
char c = getchar();
for(; !isdigit(c); c = getchar());
for(; isdigit(c); c = getchar())
   ans = ans * 10 + c - '0';
return ans;
}
 
int buf[12];
inline void write(int x) {
if(!x) {
putchar('0');
return;
}
int n = 0;
for(; x; x /= 10) buf[n++] = x % 10;
while(n--) putchar(buf[n] + '0');
}
 
struct BIT {
int b[maxn];
BIT() {
memset(b, 0, sizeof b);
}
inline void update(int x, int v) {
for(; x <= N; x += lowbit(x)) b[x] += v;
}
inline int sum(int x) {
int ret = 0;
for(; x; x -= lowbit(x)) ret += b[x];
return ret;
}
inline int query(int l, int r) {
return sum(r) - sum(l - 1);
}
} cnt, exist;
 
struct Q {
int l, r, a, b, p;
inline void Read(int t) {
l = read() - 1; r = read() - 1;
a = read(); b = read();
p = t;
}
bool operator < (const Q &o) const {
return block[l] < block[o.l] || (block[l] == block[o.l] && r < o.r);
}
} A[maxm];
 
void init() {
N = read(); M = read();
for(int i = 0; i < N; i++) seq[i] = read();
int blocks = (int) sqrt(N);
for(int i = 0; i < N; i++) block[i] = i / blocks;
for(int i = 0; i < M; i++) A[i].Read(i);
}
 
void work(int x) {
Q* c = A + x;
for(; L < c->l; L++) {
cnt.update(seq[L], -1);
if(!cnt.query(seq[L], seq[L])) exist.update(seq[L], -1);
}
while(L > c->l) {
L--;
if(!cnt.query(seq[L], seq[L])) exist.update(seq[L], 1);
cnt.update(seq[L], 1);
}
for(; R > c->r; R--) {
cnt.update(seq[R], -1);
if(!cnt.query(seq[R], seq[R])) exist.update(seq[R], -1);
}
while(R < c->r) {
R++;
if(!cnt.query(seq[R], seq[R])) exist.update(seq[R], 1);
cnt.update(seq[R], 1);
}
ans[c->p][0] = cnt.query(c->a, c->b);
ans[c->p][1] = exist.query(c->a, c->b);
}
 
int main() {
init();
sort(A, A + M);
cnt.update(seq[L = R = 0], 1);
exist.update(seq[0], 1);
for(int i = 0; i < M; i++) work(i);
for(int i = 0; i < M; i++) {
write(ans[i][0]);
putchar(' ');
write(ans[i][1]);
putchar('\n');
}
return 0;
}

-------------------------------------------------------------------------

3236: [Ahoi2013]作业

Time Limit: 100 Sec  Memory Limit: 512 MB
Submit: 899  Solved: 345
[Submit][Status][Discuss]

Description

Input

Output

Sample Input

3 4
1 2 2
1 2 1 3
1 2 1 1
1 3 1 3
2 3 2 3

Sample Output

2 2
1 1
3 2
2 1

HINT

N=100000,M=1000000

Source

BZOJ 3236: [Ahoi2013]作业( 莫队 + BIT )的更多相关文章

  1. Bzoj 3236: [Ahoi2013]作业 莫队,分块

    3236: [Ahoi2013]作业 Time Limit: 100 Sec  Memory Limit: 512 MBSubmit: 1113  Solved: 428[Submit][Status ...

  2. BZOJ 3236: [Ahoi2013]作业(莫队+树状数组)

    传送门 解题思路 莫队+树状数组.把求\([a,b]\)搞成前缀和形式,剩下的比较裸吧,用\(cnt\)记一下数字出现次数.时间复杂度\(O(msqrt(n)log(n)\),莫名其妙过了. 代码 # ...

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

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

  4. [AHOI2013]作业 (莫队+分块)

    [AHOI2013]作业 (莫队+分块) 题面 给定了一个长度为n的数列和若干个询问,每个询问是关于数列的区间[l,r],首先你要统计该区间内大于等于a,小于等于b的数的个数,其次是所有大于等于a,小 ...

  5. BZOJ 3236: [Ahoi2013]作业

    3236: [Ahoi2013]作业 Time Limit: 100 Sec  Memory Limit: 512 MBSubmit: 1393  Solved: 562[Submit][Status ...

  6. bzoj 3236: [Ahoi2013]作业(缺线段树)

    3236: [Ahoi2013]作业 Time Limit: 100 Sec  Memory Limit: 512 MBSubmit: 1744  Solved: 702[Submit][Status ...

  7. [BZOJ 3236] [Ahoi2013] 作业 && [BZOJ 3809] 【莫队(+分块)】

    题目链接: BZOJ - 3236   BZOJ - 3809 算法一:莫队 首先,单纯的莫队算法是很好想的,就是用普通的第一关键字为 l 所在块,第二关键字为 r 的莫队. 这样每次端点移动添加或删 ...

  8. bzoj 3236: 洛谷 P4396: [AHOI2013]作业 (莫队, 分块)

    题目传送门:洛谷P4396. 题意简述: 给定一个长度为\(n\)的数列.有\(m\)次询问,每次询问区间\([l,r]\)中数值在\([a,b]\)之间的数的个数,和数值在\([a,b]\)之间的不 ...

  9. 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块

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

随机推荐

  1. [C#编程参考]把图像转换为数组的两种实现

    当一个程序和一个图片放在一起,无非有两种操作: 第一种,就是传输这个图片,在传输图片之前要首先把这个图片变成byte类型的数组.所以这时候我们用到的是图片的存储的数据,也就是图片属性中的大小.我们并不 ...

  2. 编写带参数decorator

    无参的@log装饰器: def log(f): def fn(x): print 'call ' + f.__name__ + '()...' return f(x) return fn 发现对于被装 ...

  3. idea中使用sbt构建scala项目及依赖

    1.安装scala插件 http://www.cnblogs.com/yrqiang/p/5310700.html 2. 详细了解sbt: http://www.scala-sbt.org/0.13/ ...

  4. Python collections.defaultdict 笔记

    其实defaultdict 就是一个字典,只不过python自动的为它的键赋了一个初始值.这也就是说,你不显示的为字典的键赋初值python不会报错,看下实际例子. 比如你想计算频率 frequenc ...

  5. ie的selectNodes函数和firefox的document.evaluate

    selectNodes() 方法用一个 XPath 查询选择节点. nodeObject.selectNodes(query)query  XPath 查询串. 包含了匹配查询的节点的一个 NodeL ...

  6. Qt实战之开发CSDN下载助手 (2)

    现在,我们正式开工啦.这一篇主要学习下基本的抓包分析.学会协议登录CSDN并制作登陆界面. 准备工具: 一款http抓包工具. 可以是FireBug或者fiddler.这里我们用httpWatch. ...

  7. poj1061

    构造方程 (x + m * s) - (y + n * s) = k * l(k = 0, 1, 2,...) 变形为 (n-m) * s + k * l = x - y.即转化为模板题,a * x ...

  8. 3027 - Corporative Network(并差集)

    3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...

  9. iOS开展-CocoaPods安装和使用教程

    原文链接: iOS开展-CocoaPods安装和使用教程 修正已经增加了自己的理解. CocoaPods安装和使用教程 Code4App 原创文章.转载请注明出处:http://code4app.co ...

  10. Servlet、SPringMVC、Struts等防止表单反复提交的多种处理方法

    第一种处理方法(非拦截器): 眼下这样的方法不建议,由于JSP规范不建议写JAVA代码.这样的能够方便另外一种处理方法的理解,另外一种方法引入拦截器的思想,原理基本一样,模仿Struts的Token机 ...