Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

Input

The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

Output

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

Sample Input

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output

5
6
3

HINT

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

题目大意

求不修改的区间$k$小值。

题解

这道题做法有很多,据说某种奇妙的暴力可以过,还有《挑战程序设计竞赛》上说的平方分割、分桶法等等可以做。这些就先不讨论。这里介绍$5$种常见的做法。

解法一 莫队(+离散化+树状数组+二分)

一看到题,区间求值,就想到万能的莫队。

我们将数值哈希一下,将值域范围缩小。

做莫队的同时维护一个树状数组,记录出现次数。 询问的时候可以考虑二分,在树状数组中查找第$k$小的数。

 1 #include<map>
 2 #include<set>
 3 #include<ctime>
 4 #include<cmath>
 5 #include<queue>
 6 #include<stack>
 7 #include<cstdio>
 8 #include<string>
 9 #include<vector>
10 #include<cstdlib>
11 #include<cstring>
12 #include<iostream>
13 #include<algorithm>
14 #define LL long long
15 #define RE register
16 #define IL inline
17 #define lowbit(x) (x&-x)
18 using namespace std;
19 const int N=100000;
20 const int M=5000;
21 const int INF=2e9;
22 
23 struct tt
24 {
25     int num,pos,id;
26 }a[N+5];
27 int rem[N+5];
28 int n,m,lim;
29 IL bool comp(tt a,tt b) {return a.num<b.num;}
30 IL bool accomp(tt a,tt b) {return a.pos<b.pos;}
31 
32 struct ss
33 {
34     int l,r,k,id;
35 }q[M+5];
36 IL bool qcomp(ss a,ss b) {return a.l/lim==b.l/lim ? a.r<b.r : a.l<b.l;}
37 
38 int c[N+5];
39 void Add(int a,int k) {for (;a<=n;a+=lowbit(a)) c[a]+=k;}
40 int Count(int a)
41 {
42     int sum=0;
43     for (;a;a-=lowbit(a)) sum+=c[a];
44     return sum;
45 }
46 int Dev(int k)
47 {
48     int l=1,r=n,mid,ans;
49     while (l<=r)
50     {
51         mid=(l+r)>>1;
52         if (Count(mid)>=k) r=mid-1,ans=mid;
53         else l=mid+1;
54     }
55     return rem[ans];
56 }
57 
58 int ans[M+5];
59 
60 int main()
61 {
62     scanf("%d%d",&n,&m);
63     lim=sqrt(double(n));
64     for (RE int i=1;i<=n;i++) scanf("%d",&a[i].num),a[i].pos=i;
65     sort(a+1,a+n+1,comp);
66     a[0].num=-INF;
67     for (RE int i=1;i<=n;i++) a[i].id=a[i-1].id+(a[i].num!=a[i-1].num),rem[a[i].id]=a[i].num;
68     sort(a+1,a+n+1,accomp);
69     for (RE int i=1;i<=m;i++) scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].k),q[i].id=i;
70     sort(q+1,q+m+1,qcomp);
71     int curl=1,curr=0,l,r;
72     for (RE int i=1;i<=m;i++)
73     {
74         l=q[i].l,r=q[i].r;
75         while (curl<l) Add(a[curl++].id,-1);
76         while (curl>l) Add(a[--curl].id,1);
77         while (curr<r) Add(a[++curr].id,1);
78         while (curr>r) Add(a[curr--].id,-1);
79         ans[q[i].id]=Dev(q[i].k);
80     }
81     for (RE int i=1;i<=m;i++) printf("%d\n",ans[i]);
82     return 0;
83 }
莫队

相关文章:

  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-03
  • 2021-12-15
猜你喜欢
  • 2022-02-14
  • 2021-06-03
  • 2022-01-17
  • 2021-09-08
  • 2021-07-20
  • 2021-10-16
相关资源
相似解决方案