aayyk

Untitled

Nov 30th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <stack>
  8. #include <climits>
  9. #include <string>
  10. #include <set>
  11. #include <cmath>
  12. #include <map>
  13. #include <unordered_map>
  14. #include <numeric>
  15. #include <random>
  16. #include <memory>
  17. #include <chrono>
  18. #include <iterator>
  19. #include <functional>
  20. #include <unordered_set>
  21. #include <cassert>
  22. #ifdef LOCAL
  23. #include "debug.h"
  24. #else
  25. #define debug(x...)
  26. #endif
  27. #pragma GCC optimize("Ofast")
  28. //#define int ll
  29.  
  30.  
  31.  
  32. using namespace std;
  33. typedef long long ll;
  34. typedef long double ld;
  35. typedef pair < int, int > pii;
  36. typedef pair < ll, ll > pll;
  37. #define sz(x) int((x).size())
  38.  
  39. #ifndef LOCAL
  40. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  41. #else
  42. mt19937 rng(228);
  43. #endif
  44.  
  45. const int N = 5e3 + 7;
  46. const int inf = INT_MAX / 2;
  47. const ll INF = LLONG_MAX / 3;
  48. const int MOD = 1e9 + 7;
  49. const double eps = 1e-6;
  50. const string cars[] = {"🚗", "🚕", "🚙"};
  51.  
  52. short id[N], dp[N][N];
  53.  
  54. inline bool f(short l, short r, short x, short k) {
  55.     return dp[r][x] - (l ? dp[l - 1][x] : 0) >= k;
  56. }
  57.  
  58. signed main() {
  59. #ifdef LOCAL
  60.     freopen("input.txt", "r", stdin);
  61.     freopen("output.txt", "w", stdout);
  62. #endif
  63.     cout << fixed << setprecision(4);
  64.     ios::sync_with_stdio(false);
  65.     cin.tie();
  66.     cout.tie();
  67.  
  68.     int n;
  69.     cin >> n;
  70.     vector < int > aa(n);
  71.  
  72.     for (int& x : aa) {
  73.         cin >> x;
  74.     }
  75.  
  76.     auto a = aa;
  77.     sort(aa.begin(), aa.end());
  78.    
  79.     for (int& x : a) {
  80.         x = lower_bound(aa.begin(), aa.end(), x) - aa.begin();
  81.     }
  82.  
  83.     for (int i = 0; i < n; i++) {
  84.         id[a[i]] = i + 1;
  85.     }
  86.  
  87.     for (int i = a[0]; i < n; i++) {
  88.         dp[0][i] = 1;
  89.     }
  90.  
  91.     for (int i = 1; i < n; i++) {
  92.         for (int j = 0; j < n; j++) {
  93.             dp[i][j] = dp[i - 1][j] + (a[i] <= j);
  94.         }
  95.     }
  96.  
  97.     int q;
  98.     cin >> q;
  99.  
  100.     while (q--) {
  101.         short l, r, k;
  102.         cin >> l >> r >> k;
  103.        
  104.         l--, r--;
  105.  
  106.         short L = 0, R = n - 1;
  107.         while (R - L > 1) {
  108.             short M = (L + R) / 2;
  109.  
  110.             if (f(l, r, M, k)) {
  111.                 R = M;
  112.             }
  113.             else {
  114.                 L = M;
  115.             }
  116.         }
  117.        
  118.         short ans = (f(l, r, L, k) ? L : R);
  119.         cout << id[ans] - l << "\n";
  120.        
  121.     }
  122.  
  123.     return 0;
  124. }
Add Comment
Please, Sign In to add comment