Submission #731313

#TimeUsernameProblemLanguageResultExecution timeMemory
731313errayAbracadabra (CEOI22_abracadabra)C++11
100 / 100
682 ms48064 KiB
//author: erray
#include <bits/stdc++.h>
 
using namespace std;
 
template<typename A, typename B> string to_string(pair<A, B>);
template<typename A, typename B, typename C> string to_string(tuple<A, B, C>);
string to_string(string s) {
  return '"' + s + '"';
}
string to_string(const char* c) {
  return to_string(string(c));
}
string to_string(bool b) {
  return (b ? "true" : "false");
}
template<size_t T> 
string to_string(bitset<T> bs) {
  return bs.to_string();
}
template<typename T>
string to_string(priority_queue<T> pq) {
  vector<T> a;
  while (!pq.empty()) {
    a.push_back(pq.top());
    pq.pop();
  }
  return to_string(a);
}
string to_string(vector<bool> v) {
  string res = "{";
  for (int i = 0; i < int(v.size()); ++i) {
    if (int(res.size()) > 1) {
      res += ", ";
    }
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template<typename T> 
string to_string(T v) {
  string res = "{";
  for (auto x : v) {
    if (int(res.size()) > 1) {
      res += ", ";
    }
    res += to_string(x);
  }
  res += "}";
  return res;
}
template<typename A, typename B> string to_string(pair<A, B> p) {
  return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template<typename A, typename B, typename C> string to_string(tuple<A, B, C> t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ')';
}
void debug_out() {
  cerr << endl;
}
template<typename H, typename... T> 
void debug_out(H head, T... tail) {
  cerr << "  " << to_string(head);
  debug_out(tail...);
}
 
#ifdef DEBUG 
  #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else 
  #define debug(...) void(37)
#endif
 
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
 
struct Fenwick {
  int n;
  vector<int> tree;
  Fenwick(int _n) : n(_n) {
    tree.resize(n + 1);
  }
  int get(int x) {
    x += 1;
    int res = 0;
    while (x > 0) {
      res += tree[x];
      x -= x & -x;
    }
    return res;
  }
  void modify(int x, int d) {
    x += 1;
    while (x <= n) {
      tree[x] += d;
      x += x & -x;
    }
  }
  int lower_bound(int& s) {
    int res = 0;
    for (int i = 1 << __lg(n); i > 0; i >>= 1) {
      if (res + i <= n && s > tree[res + i]) {
        res += i;
        s -= tree[res];
      }
    }
    return res;
  }
};
  
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N, Q;
  cin >> N >> Q;
  vector<int> P(N);
  for (int i = 0; i < N; ++i) {
    cin >> P[i];
    --P[i];
  }
  vector<vector<array<int, 2>>> qs(N + 1);
  vector<int> ans(Q);
  for (int i = 0; i < Q; ++i) {
    int T, I;
    cin >> T >> I;
    --I;
    qs[min(N, T)].push_back({I, i});
  }
  Fenwick fenw(N);
  int mx = -1;
  vector<int> sizes(N);  
  for (int i = 0; i < N; ++i) {
    if (mx == -1 || P[mx] < P[i]) {
      mx = i;
    }
    fenw.modify(P[mx], +1);
    sizes[P[mx]] += 1;
  }
  vector<int> next_big(N);
  vector<int> st;
  for (int i = N - 1; i >= 0; --i) {
    while (!st.empty() && P[st.back()] < P[i]) {
      st.pop_back();
    }
    next_big[i] = (st.empty() ? N : st.back());
    st.push_back(i);
  }  
  vector<int> inv(N);
  for (int i = 0; i < N; ++i) {
    inv[P[i]] = i;
  }
  debug(inv);
  for (int i = 0; i <= N; ++i) {
    debug(i);
    if (i > 0) {
      int left = N / 2;
      int f = fenw.lower_bound(left);
      int s = sizes[f];
      debug(f, s, left);
      if (left != s) {
        int next = inv[f] + left;
        int cd = s - left;
        fenw.modify(f, -cd);
        sizes[f] -= cd;        
        while (next < inv[f] + s) {
          int del = min(inv[f] + s, next_big[next]) - next;
          fenw.modify(P[next], del);
          sizes[P[next]] += del;
          next = next_big[next];
        }    
      }
    }
    for (auto[q, id] : qs[i]) {
      q += 1;
      int block = fenw.lower_bound(q);
      ans[id] = P[inv[block] + q - 1];
    }
  }
  for (int i = 0; i < Q; ++i) {
    cout << ans[i] + 1 << " \n"[i == Q - 1];
  }
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:172:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  172 |     for (auto[q, id] : qs[i]) {
      |              ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...