1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include <iostream>
#include <vector>
using namespace std;
void merge(vector<int> &a, int, int, int);
void merge_sort(vector<int> &A, int left, int right)
{
int mid;
if (left >= right) {
return;
}
else {
mid = (left + right) / 2;
merge_sort(A, left, mid);
merge_sort(A, mid + 1, right);
merge(A, left, mid, right);
}
}
void merge(vector<int> &a, int left, int mid, int right)
{
int b[100];
int i;
int j;
int k = left;
for (i = left; i <= right; i++) {
b[i] = a[i];
}
i = left;
j = mid + 1;
while (i <= mid && j <= right) {
if (b[i] < b[j]) {
a[k] = b[i];
i = i + 1;
}
else {
a[k] = b[j];
j = j + 1;
}
k = k + 1;
}
while (i <= mid) {
a[k] = b[i];
i = i + 1;
k = k + 1;
}
while (j <= right) {
a[k] = b[j];
j = j + 1;
k = k + 1;
}
}
void answer(vector<int> a, int left, int right, int k, vector<int> &v)
{
int b[100];
int ctr = 0; // ctr is counter for b[100];
merge_sort(a, left, right);
for (int i = left; i <= right; i++) {
b[ctr] = a[i];
ctr = ctr + 1;
}
v.push_back(b[k]);
}
int main()
{
int size, temp;
int num_ques;
int left, right, k;
vector<int> v;
cin >> size;
cin >> num_ques;
vector<int> arr;
for (int i = 0; i < size; i++) {
cin >> temp;
arr.push_back(temp);
}
for (int i = 0; i < num_ques; i++) {
cin >> left;
cin >> right;
cin >> k;
answer(arr, left - 1, right - 1, k - 1, v);
}
for (int i = 0; i < v.size(); i++) {
cout << v[i] << endl;
}
return 0;
}
|