Question Link
The Kth numbertime limit: 12000/6000 ms (Java/others) memory limit: 128000/64000 KB (Java/others) submitstatisticnext problemproblem 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:
| 123456 |
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.......
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.
Outputfor every query, output a integer in a line. sample input
16 313 14 15 13 14 131 6 31 6 13 5 2
Sample output
131
Sourcezhangmingmingmanagerwuyiqi
This is the first time that you are familiar with the Mosuo algorithm. Tu once... Haha, don't care about these details and the method of square division is similar. The idea of the Mo team algorithm is probably to divide linear sequences as evenly as possible. It is generally used for questions that do not require team data modification and must be offline. The sorting here is first sorted in ascending order of the bucket, if the bucket order is the same, then sorted by the end.
Accepted code:
1 /* 2 * this code is made by Stomach_ache 3 * Problem: 1108 4 * Verdict: Accepted 5 * Submission Date: 2014-09-04 21:32:52 6 * Time: 1320MS 7 * Memory: 4248KB 8 */ 9 #include <stdio.h>10 #include <string.h>11 #include <algorithm>12 using namespace std;13 /*Let‘s fight!!!*/14 15 const int Sqrt = 333;16 const int MAX_N = 101000;17 int a[MAX_N], ans[MAX_N], freq[MAX_N], cnt[MAX_N];18 int ll[MAX_N], rr[MAX_N], kk[MAX_N], idx[MAX_N], n, m;19 20 bool cmp (int a, int b) {21 if (ll[a]/Sqrt == ll[b]/Sqrt) return rr[a] < rr[b];22 return ll[a] < ll[b];23 }24 25 int query(int k) {26 int lb = 1, ub = 100001;27 while (ub - lb > 1) {28 int mid = (lb + ub) / 2;29 if (freq[mid] >= k) lb = mid;30 else ub = mid;31 }32 return lb;33 }34 35 int main() {36 int T;37 scanf("%d", &T);38 while (T--) {39 scanf("%d%d", &n, &m);40 for (int i = 0; i < n; i++) scanf("%d", a+i);41 for (int i = 0; i < m; i++) {42 scanf("%d%d%d", ll+i, rr+i, kk+i);43 idx[i] = i; ll[i]--; rr[i]--;44 }45 46 sort(idx, idx + m, cmp);47 memset(freq, 0, sizeof(freq));48 memset(cnt, 0, sizeof(cnt));49 50 int cl = 0, cr = -1;51 for (int i = 0; i < m; i++) {52 int l = ll[idx[i]], r = rr[idx[i]], k = kk[idx[i]];53 while (cr < r) { freq[++cnt[a[++cr]]] ++; }54 while (l < cl) { freq[++cnt[a[--cl]]] ++; }55 while (r < cr) { freq[cnt[a[cr--]]--] --; }56 while (cl < l) { freq[cnt[a[cl++]]--] --; }57 ans[idx[i]] = query(k);58 }59 60 for (int i = 0; i < m; i++) printf("%d\n", ans[i]);61 }62 63 return 0;64 }
Acdream 1108 (Mo Team)