题目链接

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. python全栈开发:字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. 1.百分号方式 %[(name ...

  2. 跳过爱奇艺优酷vip

      1.google chrome浏览器  2.下载插件安装 Tampermonkey  https://pan.baidu.com/s/1qvRQD2UO6gPHogjtSUBwUw       将 ...

  3. soj98 卡牌

    题意:一共有n张牌,每张牌有三个属性ai,bi,ci.问在属性上限为A,B,C的所有牌中有多少张牌满足至少有两个属性可以完全压制(严格大于)那n张牌? n<=50W. 标程: #include& ...

  4. CF596D Wilbur and Trees

    题意:有一些高度为h的树在数轴上.每次选择剩下的树中最左边或是最右边的树推倒(各50%概率),往左倒有p的概率,往右倒1-p. 一棵树倒了,如果挨到的另一棵树与该数的距离严格小于h,那么它也会往同方向 ...

  5. python类的静态方法和类方法区别

    先看语法,python 类语法中有三种方法,实例方法,静态方法,类方法. # coding:utf-8 class Foo(object): """类三种方法语法形式&q ...

  6. csps模拟测试7273简单的操作小P的2048小P的单调数列小P的生成树

    题面:https://www.cnblogs.com/Juve/articles/11678564.html 简单的操作: 考场上sb了,没看出来 如果有奇环一定不能缩成一条链,判掉奇环后就是bfs最 ...

  7. 第十章 Odoo 12开发之后台视图 - 设计用户界面

    本文将学习如何为用户创建图形化界面来与图书应用交互.我们将了解不同视图类型和小组件(widgets)之间的差别,以及如何使用它们来提供更优的用户体验. 本文主要内容有: 菜单项 窗口操作(Window ...

  8. PAT甲级——A1087 All Roads Lead to Rome【30】

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

  9. hibernate使用truncate清空表 截断表

    public void truncateTable(Session session, String tableNameInDb) { String sql = " truncate tabl ...

  10. 05_Spring AOP原理

    理解AOP相关概念 Target(目标对象):代理的目标对象 Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点. ...