|
| 1 | +#include <cstring> |
| 2 | +#include <map> |
| 3 | +#include <deque> |
| 4 | +#include <queue> |
| 5 | +#include <stack> |
| 6 | +#include <sstream> |
| 7 | +#include <numeric> |
| 8 | +#include <iostream> |
| 9 | +#include <iomanip> |
| 10 | +#include <cstdio> |
| 11 | +#include <cmath> |
| 12 | +#include <fstream> |
| 13 | +#include <string> |
| 14 | +#include <cstdlib> |
| 15 | +#include <ctime> |
| 16 | +#include <functional> |
| 17 | +#include <algorithm> |
| 18 | +#include <vector> |
| 19 | +#include <set> |
| 20 | +#include <complex> |
| 21 | +#include <list> |
| 22 | +#include <climits> |
| 23 | +#include <cctype> |
| 24 | +#include <bitset> |
| 25 | +using namespace std; |
| 26 | + |
| 27 | +#define PI 3.14159265359 |
| 28 | +#define all(v) v.begin(),v.end() |
| 29 | +#define sortva(v) sort(all(v)) |
| 30 | +#define sortvd(v) sort(v.rbegin(),v.rend()) |
| 31 | +#define sortaa(a,n) sort(a,a+n) |
| 32 | +#define sortad(a,n) sort(a,a+n),reverse(a,a+n) |
| 33 | +#define sfi1(v) scanf("%d",&v) |
| 34 | +#define sfi2(v1,v2) scanf("%d %d",&v1,&v2) |
| 35 | +#define sfi3(v1,v2,v3) scanf("%d %d %d",&v1,&v2,&v3) |
| 36 | +#define sfll1(v) scanf("%I64d",&v); |
| 37 | +#define sfll2(v1,v2) scanf("%I64d %I64d",&v1,&v2) |
| 38 | +#define sfll3(v1,v2,v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3) |
| 39 | +#define sfstr(v) scanf("%s", v); |
| 40 | +#define sz(v) (int)v.size() |
| 41 | +#define ndl puts("") |
| 42 | +#define flush fflush(stdout) |
| 43 | +#define SS stringstream |
| 44 | +typedef long long ll; |
| 45 | +typedef unsigned long long ull; |
| 46 | +typedef long double ld; |
| 47 | + |
| 48 | +int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 }; |
| 49 | +int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 }; |
| 50 | + |
| 51 | +ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); } |
| 52 | +ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } |
| 53 | + |
| 54 | +void PLAY() { |
| 55 | +#ifndef ONLINE_JUDGE |
| 56 | +freopen("input.txt", "r", stdin); |
| 57 | +freopen("output.txt", "w", stdout); |
| 58 | +#endif |
| 59 | + |
| 60 | +cout << fixed << setprecision(10); |
| 61 | +ios::sync_with_stdio(0); |
| 62 | +cin.tie(0); |
| 63 | +cout.tie(0); |
| 64 | +} |
| 65 | + |
| 66 | +const int N = (1 << 17); |
| 67 | + |
| 68 | +struct node; |
| 69 | +node *empty; |
| 70 | + |
| 71 | +struct node { |
| 72 | +int sum; |
| 73 | +node *lft, *rit; |
| 74 | +node() { |
| 75 | +lft = rit = this; |
| 76 | +sum = 0; |
| 77 | +} |
| 78 | +node(int s, node *l = empty, node *r = empty) { |
| 79 | +lft = l; rit = r; sum = s; |
| 80 | +} |
| 81 | +}; |
| 82 | + |
| 83 | +node *insert(node *cur, int l, int r, int val) { |
| 84 | +if (val < l || val > r) return cur; |
| 85 | +if (l == r) return new node(cur->sum + 1); |
| 86 | +int mid = l + (r - l) / 2; |
| 87 | +node *lft = insert(cur->lft, l, mid, val); |
| 88 | +node *rit = insert(cur->rit, mid + 1, r, val); |
| 89 | +return new node(lft->sum + rit->sum, lft, rit); |
| 90 | +} |
| 91 | + |
| 92 | +int query(node *rit, node *lft, int k, int l, int r) { |
| 93 | +if (l == r) return l; |
| 94 | +int left_size = rit->lft->sum - lft->lft->sum; |
| 95 | +int mid = l + (r - l) / 2; |
| 96 | +if (k <= left_size) |
| 97 | +return query(rit->lft, lft->lft, k, l, mid); |
| 98 | +return query(rit->rit, lft->rit, k - left_size, mid + 1, r); |
| 99 | +} |
| 100 | + |
| 101 | +node *roots[N]; |
| 102 | + |
| 103 | +int main() { |
| 104 | +PLAY(); |
| 105 | + |
| 106 | +empty = new node(); |
| 107 | +roots[0] = empty; |
| 108 | + |
| 109 | +int n, q; |
| 110 | +cin >> n >> q; |
| 111 | +for (int i = 1; i <= n; i++) { |
| 112 | +int x; cin >> x; |
| 113 | +roots[i] = insert(roots[i - 1], -1e9, 1e9, x); |
| 114 | +} |
| 115 | +while (q--) { |
| 116 | +int l, r, k; |
| 117 | +cin >> l >> r >> k; |
| 118 | +cout << query(roots[r], roots[l - 1], k, -1e9, 1e9) << "\n"; |
| 119 | +} |
| 120 | + |
| 121 | +return 0; |
| 122 | +} |
0 commit comments