Submission #397612

#TimeUsernameProblemLanguageResultExecution timeMemory
397612MrRobot_28Index (COCI21_index)C++17
60 / 110
2593 ms166428 KiB
#include<bits/stdc++.h> using namespace std; #define X first #define Y second #define int long long #define sz(a) (int)a.size() const int mod = 1e9 + 7; const int N = 2e5; vector <int> uk_left[N * 4]; vector <int> uk_right[N * 4]; vector <int> Tree[4 * N]; int h[N]; int n, q; void build(int v, int l, int r) { if(l == r) { Tree[v].push_back(h[l]); return; } build(v * 2, l, (r + l) / 2); build(v * 2 + 1, (r + l) / 2 + 1, r); int j = 0; for(int i = 0; i < sz(Tree[v * 2]); i++) { while(j < sz(Tree[v * 2 + 1]) && Tree[v * 2 + 1][j] < Tree[v * 2][i]) { Tree[v].push_back(Tree[v * 2 + 1][j]); j++; } Tree[v].push_back(Tree[v * 2][i]); } while(j < sz(Tree[v * 2 + 1])) { Tree[v].push_back(Tree[v * 2 + 1][j]); j++; } int it1 = sz(Tree[v * 2]), it2 = sz(Tree[v * 2 + 1]); uk_left[v].resize(r - l + 1); uk_right[v].resize(r - l + 1); for(int i = sz(Tree[v]) - 1; i >= 0; i--) { while(it1 > 0 && Tree[v * 2][it1 - 1] >= Tree[v][i]) { it1--; } uk_left[v][i] = it1; while(it2 > 0 && Tree[v * 2 + 1][it2 - 1] >= Tree[v][i]) { it2--; } uk_right[v][i] = it2; } } int go_to(int v, int l, int r, int al, int ar, int uk) { if(r < al || l > ar) { return 0; } if(uk == sz(Tree[v])) { return 0; } if(l >= al && r <= ar) { return r - l + 1 - uk; } return go_to(v * 2, l, (r + l) / 2, al, ar, uk_left[v][uk]) + go_to(v * 2 + 1, (r + l) / 2 + 1, r, al, ar, uk_right[v][uk]); } int q_ans(int l, int r, int x) { int l1 = -1, r1 = sz(Tree[1]); while(r1 - l1 > 1) { int midd = (r1 + l1) / 2; if(Tree[1][midd] < x) { l1 = midd; } else { r1 = midd; } } return go_to(1, 0, n - 1, l, r, r1); } signed main() { // ifstream cin("input1.txt.4c"); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; for(int i = 0; i < n; i++) { cin >> h[i]; } build(1, 0, n - 1); while(q--) { int l, r; cin >> l >> r; l--; r--; int l1 = 1, r1 = r - l + 2; while(r1 - l1 > 1) { int midd = (r1 + l1) / 2; if(q_ans(l, r, midd) >= midd) { l1 = midd; } else { r1 = midd; } } cout << l1 << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...