Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <algorithm>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <climits>
- #include <string>
- #include <set>
- #include <cmath>
- #include <map>
- #include <unordered_map>
- #include <numeric>
- #include <random>
- #include <memory>
- #include <chrono>
- #include <iterator>
- #include <functional>
- #include <unordered_set>
- #include <cassert>
- #ifdef LOCAL
- #include "debug.h"
- #else
- #define debug(x...)
- #endif
- #pragma GCC optimize("Ofast")
- //#define int ll
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- typedef pair < int, int > pii;
- typedef pair < ll, ll > pll;
- #define sz(x) int((x).size())
- #ifndef LOCAL
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- #else
- mt19937 rng(228);
- #endif
- const int N = 5e3 + 7;
- const int inf = INT_MAX / 2;
- const ll INF = LLONG_MAX / 3;
- const int MOD = 1e9 + 7;
- const double eps = 1e-6;
- const string cars[] = {"🚗", "🚕", "🚙"};
- short id[N], dp[N][N];
- inline bool f(short l, short r, short x, short k) {
- return dp[r][x] - (l ? dp[l - 1][x] : 0) >= k;
- }
- signed main() {
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- cout << fixed << setprecision(4);
- ios::sync_with_stdio(false);
- cin.tie();
- cout.tie();
- int n;
- cin >> n;
- vector < int > aa(n);
- for (int& x : aa) {
- cin >> x;
- }
- auto a = aa;
- sort(aa.begin(), aa.end());
- for (int& x : a) {
- x = lower_bound(aa.begin(), aa.end(), x) - aa.begin();
- }
- for (int i = 0; i < n; i++) {
- id[a[i]] = i + 1;
- }
- for (int i = a[0]; i < n; i++) {
- dp[0][i] = 1;
- }
- for (int i = 1; i < n; i++) {
- for (int j = 0; j < n; j++) {
- dp[i][j] = dp[i - 1][j] + (a[i] <= j);
- }
- }
- int q;
- cin >> q;
- while (q--) {
- short l, r, k;
- cin >> l >> r >> k;
- l--, r--;
- short L = 0, R = n - 1;
- while (R - L > 1) {
- short M = (L + R) / 2;
- if (f(l, r, M, k)) {
- R = M;
- }
- else {
- L = M;
- }
- }
- short ans = (f(l, r, L, k) ? L : R);
- cout << id[ans] - l << "\n";
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment