题目链接

The kth number

Time Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)

Problem Description

Do you still remember the Daming Lake's  k'th number? Let me take you back and recall that wonderful memory.

Given a sequence A with length of n,and m querys.Every query is defined by three integer(l,r,k).For each query,please find the kth biggest frequency in interval [l,r].
Frequency of a number x in [l,r] can be defined by this code:

1
2
3
4
5
6
intFrequencyOfX = 0;
for(inti = l; i <= r; i ++) {
     if(a[i]==X) {
         FrequencyOfX ++;
     }
}

Input

First line is a integer T,the test cases.
For each case:
First line contains two integers n and m.
Second line contains n integers a1,a2,a3....an.
Then next m lines,each line contain three integers l,r,k.

T<=12
1<=n,m,ai<=100000
1<=l<=r<=n
1<=k
data promise that for each query(l,r,k),the kind of number in interval [l,r] is at least k.

Output

for every query,output a integer in a line.

Sample Input

1
6 3
13 14 15 13 14 13
1 6 3
1 6 1
3 5 2

Sample Output

1
3
1

Source

zhangmingming

Manager

初次接触莫队算法。屠了一次版。。。哈哈,不要在意这些细节
和平方分割的方法类似,莫队算法的思想大概也是把线性的序列尽量平均的进行分割。
一般用于不需要队数据进行修改的题目,而且必须离线。
这里的排序,都是先按照桶的顺序升序排序,如果桶的顺序相同再按终点排序。
Accepted Code:
 /*
* this code is made by Stomach_ache
* Problem: 1108
* Verdict: Accepted
* Submission Date: 2014-09-04 21:32:52
* Time: 1320MS
* Memory: 4248KB
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ const int Sqrt = ;
const int MAX_N = ;
int a[MAX_N], ans[MAX_N], freq[MAX_N], cnt[MAX_N];
int ll[MAX_N], rr[MAX_N], kk[MAX_N], idx[MAX_N], n, m; bool cmp (int a, int b) {
if (ll[a]/Sqrt == ll[b]/Sqrt) return rr[a] < rr[b];
return ll[a] < ll[b];
} int query(int k) {
int lb = , ub = ;
while (ub - lb > ) {
int mid = (lb + ub) / ;
if (freq[mid] >= k) lb = mid;
else ub = mid;
}
return lb;
} int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &m);
for (int i = ; i < n; i++) scanf("%d", a+i);
for (int i = ; i < m; i++) {
scanf("%d%d%d", ll+i, rr+i, kk+i);
idx[i] = i; ll[i]--; rr[i]--;
} sort(idx, idx + m, cmp);
memset(freq, , sizeof(freq));
memset(cnt, , sizeof(cnt)); int cl = , cr = -;
for (int i = ; i < m; i++) {
int l = ll[idx[i]], r = rr[idx[i]], k = kk[idx[i]];
while (cr < r) { freq[++cnt[a[++cr]]] ++; }
while (l < cl) { freq[++cnt[a[--cl]]] ++; }
while (r < cr) { freq[cnt[a[cr--]]--] --; }
while (cl < l) { freq[cnt[a[cl++]]--] --; }
ans[idx[i]] = query(k);
} for (int i = ; i < m; i++) printf("%d\n", ans[i]);
} return ;
}

ACdream 1108(莫队)的更多相关文章

  1. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  2. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  3. 【填坑向】bzoj2038小Z的袜子 莫队

    学莫队必做题,,,但是懒得写.今天来填个坑 莫队水题 莫队实际上就是按一个玄学顺序来离线计算询问,保证复杂度只会多一个n1/2,感觉是玄学(离线算法都很玄学) 易错点:要开long long(卡我半天 ...

  4. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  5. NPY and girls-HDU5145莫队算法

    Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  6. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  7. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

  8. 【BZOJ 3735】苹果树 树上莫队(树分块+离线莫队+鬼畜的压行)

    2016-05-09 UPD:学习了新的DFS序列分块,然后发现这个东西是战术核导弹?反正比下面的树分块不知道要快到哪里去了 #include<cmath> #include<cst ...

  9. 【BZOJ 2038】【2009 国家集训队】小Z的袜子(hose) 分块+莫队

    $SDOI2016Day-1$临时抱佛脚学习一下莫队算法$233$ 我预感到自己省选要爆0hhh #include<cmath> #include<cstdio> #inclu ...

随机推荐

  1. 网站时间显示——基于Date

    网站时间显示 代码实现如下: =============css样式=================== <style> #show{ width: 460px; height: 100p ...

  2. C++: inheritance

    公有继承(public).私有继承(private).保护继承(protected)是常用的三种继承方式. 1. 公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时, ...

  3. Android开发 使用SparseArray代替HashMap[转载]

    源作者:Android小Y链接:https://www.jianshu.com/p/1828f14d7955来源:简书 前言 Android开发中,一个好的应用,除了要有吸引人的功能和交互之外,在性能 ...

  4. 【JZOJ3347】树的难题

    description analysis 比较麻烦树形\(DP\) 不过这个我还是不算很懂-- 下次要注意思考,不要怕麻烦 code #pragma GCC optimize("O3&quo ...

  5. Nginx 和 Gunicorn 部署 Django项目

    目录 Nginx 和 Gunicorn 部署 Django项目 配置Nginx 安装配置Gunicorn 通过命令行直接启动 Gunicorn 与 uwsgi 的区别,用哪个好呢 Gunicorn u ...

  6. QQ交流群

  7. PAT甲级——A1098 Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  8. Ajax之json返回结果是集合的处理

    Jquery实现ajax: $.ajax({       type    //数据的提交方式:get和post        url   //数据的提交路径        async   //是否支持 ...

  9. Python学习day10-文件处理

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  10. Python基础——使用with结构打开多个文件

    考虑如下的案例: 同时打开三个文件,文件行数一样,要求实现每个文件依次读取一行,然后输出,我们先来看比较容易想到的写法: with open(filename1, 'rb') as fp1: with ...