Submission #793927

#TimeUsernameProblemLanguageResultExecution timeMemory
793927ind1vIndex (COCI21_index)C++11
110 / 110
488 ms41780 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 200005;

struct fenwick_tree {
  int fenw[N];
  
  fenwick_tree() {
    memset(fenw, 0, sizeof(fenw));
  }
  
  void reset() {
    memset(fenw, 0, sizeof(fenw));
  }
  
  void upd(int idx, int val) {
    for (; idx < N; idx |= (idx + 1)) {
      fenw[idx] += val;
    }
  }
  
  int get(int idx) {
    int res = 0;
    for (; idx >= 0; idx &= (idx + 1), --idx) {
      res += fenw[idx];
    }
    return res;
  }
  
  int get(int l, int r) {
    return get(r) - get(l - 1);
  }
};

int n, q;
int p[N];
int l[N], r[N];
int x[N], y[N], ans[N];
vector<int> cand[N], pos[N];
fenwick_tree ft;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
    pos[p[i]].emplace_back(i);
  }
  for (int i = 1; i <= q; i++) {
    cin >> l[i] >> r[i];
    x[i] = 1;
    y[i] = 200000;
    ans[i] = -1;
  }
  for (int ite = 1; ite <= 20; ite++) {
    ft.reset();
    for (int i = 1; i <= 200000; i++) {
      cand[i].clear();
    }
    for (int i = 1; i <= q; i++) {
      if (x[i] > y[i]) {
        continue;
      }
      cand[x[i] + y[i] >> 1].emplace_back(i);
    }
    for (int i = 200000; i >= 1; i--) {
      for (int &idx : pos[i]) {
        ft.upd(idx, +1);
      }
      for (int &que : cand[i]) {
        if (ft.get(l[que], r[que]) >= i) {
          ans[que] = i;
          x[que] = i + 1;
        } else {
          y[que] = i - 1;
        }
      }
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}

Compilation message (stderr)

index.cpp: In function 'int main()':
index.cpp:67:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   67 |       cand[x[i] + y[i] >> 1].emplace_back(i);
      |            ~~~~~^~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...