Submission #843074

#TimeUsernameProblemLanguageResultExecution timeMemory
843074fanwenAbracadabra (CEOI22_abracadabra)C++17
100 / 100
484 ms47376 KiB
#include <bits/stdc++.h> using namespace std; #define MASK(x) (1LL << (x)) #define BIT(x, i) (((x) >> (i)) & 1) #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for (int i = 0, _n = n; i < _n; ++i) #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i) #define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; } template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; } template <class T> struct Fenwick_Tree { vector<T> bit; int n, LOG; Fenwick_Tree(int _n = 0) : n(_n), bit(_n + 5) { this->LOG = 32 - __builtin_clz(n); } void clear() { fill(bit.begin(), bit.end(), T(0)); } void update(int u, T val) { // cout << u << " " << val << '\n'; for (; u <= n; u += u & -u) bit[u] += val; } T get(int u) { T ans = 0; assert(u <= n); for (; u; u -= u & -u) ans += bit[u]; return ans; } T get(int l, int r) { return get(r) - get(l - 1); } int lower_bound(T x) { int pos = 0; for (int k = LOG - 1; k >= 0; --k) { if(pos + (1 << k) <= n && bit[pos + (1 << k)] < x) { pos ^= 1 << k; x -= bit[pos]; } } return pos + 1; } }; const int MAXN = 2e5 + 5; int N, Q, a[MAXN], p[MAXN], ans[MAXN * 5], nxt[MAXN]; vector <pair <int, int>> queries[MAXN]; void you_make_it(void) { cin >> N >> Q; FOR(i, 1, N) cin >> a[i], p[a[i]] = i; FOR(i, 1, Q) { int t, x; cin >> t >> x; queries[min(t, N)].emplace_back(x, i); } stack <int> st; st.push(N + 1), a[N + 1] = N + 1; FORD(i, N, 1) { while(!st.empty() and a[st.top()] <= a[i]) st.pop(); nxt[i] = st.top(); st.push(i); } for (auto [x, i] : queries[0]) ans[i] = a[x]; int lt = 1; Fenwick_Tree <int> bit(N); while(lt <= N / 2) { bit.update(a[lt], min(N / 2 + 1, nxt[lt]) - lt); lt = nxt[lt]; } lt = N / 2 + 1; while(lt <= N) { bit.update(a[lt], min(N + 1, nxt[lt]) - lt); lt = nxt[lt]; } // exit(0); FOR(q, 1, N) { for (auto [x, i] : queries[q]) { int id = bit.lower_bound(x); ans[i] = a[x - bit.get(id - 1) + p[id] - 1]; } int id = bit.lower_bound(N / 2); if(bit.get(id) <= N / 2) continue; int lt = N / 2 - bit.get(id - 1) + p[id]; int rt = bit.get(id, id) + p[id] - 1; bit.update(id, N / 2 - bit.get(id)); while(lt <= rt) { bit.update(a[lt], min(rt + 1, nxt[lt]) - lt); lt = nxt[lt]; } } FOR(i, 1, Q) cout << ans[i] << '\n'; } signed main() { #ifdef LOCAL freopen("TASK.inp", "r", stdin); freopen("TASK.out", "w", stdout); #endif auto start_time = chrono::steady_clock::now(); cin.tie(0), cout.tie(0) -> sync_with_stdio(0); you_make_it(); auto end_time = chrono::steady_clock::now(); cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl; return (0 ^ 0); } // Dream it. Wish it. Do it.

Compilation message (stderr)

Main.cpp: In instantiation of 'Fenwick_Tree<T>::Fenwick_Tree(int) [with T = int]':
Main.cpp:76:29:   required from here
Main.cpp:20:9: warning: 'Fenwick_Tree<int>::n' will be initialized after [-Wreorder]
   20 |     int n, LOG;
      |         ^
Main.cpp:19:15: warning:   'std::vector<int> Fenwick_Tree<int>::bit' [-Wreorder]
   19 |     vector<T> bit;
      |               ^~~
Main.cpp:21:5: warning:   when initialized here [-Wreorder]
   21 |     Fenwick_Tree(int _n = 0) : n(_n), bit(_n + 5) {
      |     ^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...