Submission #1121002

#TimeUsernameProblemLanguageResultExecution timeMemory
1121002steveonalexIndex (COCI21_index)C++17
110 / 110
507 ms138008 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll gcd(ll a, ll b){return __gcd(a, b);} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ll mask){return __builtin_ctzll(mask);} int logOf(ll mask){return __lg(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T& container, string separator = " ", string finish = "\n"){ for(auto item: container) cout << item << separator; cout << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); }struct Node { int cnt; Node *left, *right; Node() : cnt(0), left(nullptr), right(nullptr) {} }; const int MAXN = 2e5 + 5; // Adjust size as needed const int MAXV = 2e5 + 5; // Maximum value in the array Node* roots[MAXN]; // Store version roots vector<int> sortedArr; // Sorted version of the array for coordinate compression // Build the initial segment tree Node* build(int l, int r) { Node* node = new Node(); if (l == r) return node; // Leaf node int mid = (l + r + 1) / 2; node->left = build(l, mid-1); node->right = build(mid, r); return node; } // Update the segment tree for a specific index Node* update(Node* prev, int l, int r, int idx) { Node* node = new Node(); if (l == r) { node->cnt = prev->cnt + 1; // Increment count return node; } int mid = (l + r + 1) / 2; if (idx < mid) { node->left = update(prev->left, l, mid - 1, idx); node->right = prev->right; } else { node->left = prev->left; node->right = update(prev->right, mid, r, idx); } node->cnt = node->left->cnt + node->right->cnt; return node; } // Query for the largest k where the count of numbers >= k is >= k int findLargestK(Node* lRoot, Node* rRoot, int l, int r, int k) { if (l == r) { int count = rRoot->cnt - lRoot->cnt + k; if (count < l) assert(false); return l; } int mid = (l + r + 1) / 2; int countRight = rRoot->right->cnt - lRoot->right->cnt; // cout << mid << " " << r << " " << countRight << "\n"; // Check if the solution lies in the right subtree if (countRight + k >= mid) { return findLargestK(lRoot->right, rRoot->right, mid, r, k); } else { return findLargestK(lRoot->left, rRoot->left, l, mid - 1, countRight + k); } } void solve() { int n, q; cin >> n >> q; vector<int> arr(n); for (int i = 0; i < n; ++i) { cin >> arr[i]; sortedArr.push_back(i + 1); } // Coordinate compression sort(sortedArr.begin(), sortedArr.end()); sortedArr.erase(unique(sortedArr.begin(), sortedArr.end()), sortedArr.end()); for (int i = 0; i < n; ++i) { arr[i] = lower_bound(sortedArr.begin(), sortedArr.end(), arr[i]) - sortedArr.begin() + 1; } // Build the persistent segment tree roots[0] = build(1, sortedArr.size()); for (int i = 1; i <= n; ++i) { roots[i] = update(roots[i - 1], 1, sortedArr.size(), arr[i - 1]); } // Answer queries while (q--) { int l, r; cin >> l >> r; cout << findLargestK(roots[l - 1], roots[r], 1, sortedArr.size(), 0) << "\n"; } } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); clock_t start = clock(); solve(); cerr << "Time elapsed: " << clock() - start << "ms!\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...