题目链接

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. tensorflow识别验证码(1)-tensorflow安装,验证码生成

    什么是TensorFlow?  TensorFlow是Google开发的一款神经网络的Python外部的结构包, 也是一个采用数据流图来进行数值计算的开源软件库.TensorFlow 让我们可以先绘制 ...

  2. sort的cmp函数

    sort的cmp函数只能写return a>b;或者return a<b;

  3. 转载:shell中#*,##*,#*,##*,% *,%% *的含义及用法

    介绍下Shell中的${}.##和%%使用范例,本文给出了不同情况下得到的结果.假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得 ...

  4. Python3基础笔记_字典

    # Python3 字典 dict = {'} # 1.访问字典里的值 ,字典中没有key会报错 # 2.修改字典 print("修改之前:", dict['Beth']) dic ...

  5. python 版本配置问题

    环境变量里有anaconda 但是命令行输入python却并不是anaconda里的python 这个现象的产生是由于anaconda在环境变量里的顺序靠后,python2.7已经在其他环境变量里被找 ...

  6. 修改数组中对象的key值

    遇见场景:echart图表中后台返回我的数据,后台无法修改key值,但是echart渲染图表的时候,需要用 var m2R2Data= [ {value:335,name:"种类01 335 ...

  7. 杂项-公司:Google

    ylbtech-杂项-公司:Google 谷歌公司(Google Inc.)成立于1998年9月4日,由拉里·佩奇和谢尔盖·布林共同创建,被公认为全球最大的搜索引擎公司.谷歌是一家位于美国的跨国科技企 ...

  8. iOS开发CGImage.h简介

    1.前因 由于剪切图片用到下面方法,此方法属于CGImage.h中,通过创建CGImageRef像素位图,可以通过操作存储的像素位来编辑图片. /* Create an image using the ...

  9. wifi共享大师,去除弹窗广告。

    1.安装WiFi共享大师 2.不要打开 3.右键打开文件所在位置 4.在任意目录新建三个空的txt,重命名为MiniNews.exe.ProLive.exe.WifiUpdate.exe 5.将上面的 ...

  10. js 实现横向滚动轮播并中间暂停下

    效果: html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...