题目链接

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. JavaScript特效源码(8、其他特效)

    1.中文日期 中文日期[无须修改][共1步]] ====1.将以下代码加入HEML的<body></body>之间 <script LANGUAGE="Java ...

  2. var、fucntion关键字优先级问题

    情况1:使用var关键字定义的变量优先被声明 console.log(a);    // undefined var a = 20; /* var a =20; (1)声明变量a --  优先被执行, ...

  3. python3 使用aria2下载的一个脚本

    import requests import time ariaurl="http://localhost:6800/jsonrpc" dlurl="http://xxx ...

  4. 类欧几里德算法(洛谷 P5170

    #include <iostream> #include <cstdio> #include <queue> #include <algorithm> ...

  5. 廖雪峰Java12maven基础-2maven进阶-1使用插件

    1.maven的Lifecycle,Phase和Goal: 使用maven构建项目就是执行Lifecycle 执行Lifecycle就是按顺序执行一系列Phase 每执行一个Phase,都会执行该Ph ...

  6. Python3基础笔记_迭代器

    # Python3 迭代器与生成器 import sys ''' 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开 ...

  7. css正則匹配、模糊匹配

    //所有class包含font-red的p标签 p[class~="font-red"] {color: red;} [abc^="def"] 选择 abc 属 ...

  8. springboot thymeleaf ----服务端渲染html

    一. 引用命名空间 <html xmlns:th="http://www.thymeleaf.org"> 不这么写 html标签没闭合会报错 二.实际内容在../sta ...

  9. 元素显示v-show

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  10. 2018-8-10-docfx-做一个和微软一样的文档平台

    title author date CreateTime categories docfx 做一个和微软一样的文档平台 lindexi 2018-08-10 19:16:51 +0800 2018-2 ...