Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.004 s | 1 KBi |
#2 |
Correcto
|
0.004 s | 1 KBi |
#3 |
Correcto
|
0.004 s | 1 KBi |
#4 |
Correcto
|
0.006 s | 1 KBi |
#5 |
Correcto
|
0.005 s | 1 KBi |
#6 |
Correcto
|
0.006 s | 2 KBi |
#7 |
Correcto
|
0.004 s | 1 KBi |
#8 |
Correcto
|
0.005 s | 0 KBi |
#9 |
Correcto
|
0.005 s | 1 KBi |
#10 |
Correcto
|
0.006 s | 1 KBi |
#11 |
Correcto
|
0.584 s | 6 KBi |
#12 |
Correcto
|
0.903 s | 6 KBi |
#13 |
Correcto
|
0.863 s | 6 KBi |
#14 |
Correcto
|
0.93 s | 6 KBi |
#15 |
Correcto
|
0.872 s | 6 KBi |
// Made by Daniel Diaz (@Danidiaztech) #include <bits/stdc++.h> using namespace std; #define endl '\n' #define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); #define forn(i, n) for (int i = 0; i < n; i++) // for in range in python #define fore(i, a, b) for (int i = a; i < b; i++) // for in range in python #define int long long int #define double long double #define pb push_back #define ff first #define ss second #define mk make_pair #define all(x) x.begin(),x.end() #define sz(x) (int)x.siz() typedef pair<int, int> pii; typedef vector<int> vii; const int MAX = 1e6; const int MIN = -MAX; const int oo = LLONG_MAX / 2; const int ooo = LLONG_MIN / 2; const int mod = 1e9 + 7; int siz = 2; vii t; void init(int n){ siz = 2; while (siz < n) siz *= 2; t.assign(2 * siz, 0); } void build(vii &a, int x, int lx, int rx){ if (rx - lx == 1){ if (lx < (int)a.size()) t[x] = a[lx]; return; } int m = (lx + rx) /2 ; build(a, 2 * x + 1, lx, m); build(a, 2 * x + 2, m, rx); t[x] = max(t[2 * x + 1], t[2 * x + 2]); } vii query(int l, int r, int x, int lx, int rx){ if (lx >= r || rx <= l) return {0,-1, -1,-1}; if (lx >= l && rx <= r) return {t[x], x, lx, rx}; int m = (lx + rx) /2 ; vii q1 = query(l,r,2 * x + 1, lx, m); vii q2 = query(l,r, 2* x + 2, m, rx); if (q1[0] >= q2[0]){ return q1; } else return q2; } vii query(int l, int r){ return query(l,r,0,0,siz); } // Search for the number k in a range int search(int k, int x, int l, int r){ if (r - l == 1){ // leaf return l; } int m= (l + r) /2; if (t[2 * x + 1] == k){ return search(k, 2 * x + 1, l, m); } else{ return search(k, 2 * x + 2, m, r); } } void solve(){ int n; cin >> n; vii a(n); vector<pii> v; forn(i,n){ cin >> a[i]; v.pb({a[i], i}); } sort(v.begin(), v.end()); init(n); build(a, 0,0,siz); int q; cin >> q; forn(i,q){ int l, r; cin >> l >> r; vii p = query(l, r + 1); cout << search(p[0], p[1], p[2], p[3]) << endl; } } int32_t main() { fastInp; #if LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int tc = 1; // cin >> tc; for (int t = 1; t <= tc; t++){ // cout << "Case #" << t << ": "; solve(); } return 0; }