#include<bits/stdc++.h>
using namespace std;
const bool multiTest = false;
#define task "C"
#define fi first
#define se second
#define MASK(i) (1LL << (i))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define BIT(mask, i) ((mask) >> (i) & 1)
template<typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) a = b; else return 0; return 1;
}
template<typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) a = b; else return 0; return 1;
}
const int MAX = 200020;
int n, q, nAsk;
int pp[MAX];
int ll[MAX];
int rr[MAX];
int ps[MAX];
int l[MAX];
int r[MAX];
pair<int, int> queries[MAX];
struct FenwickTree {
vector<int> bit;
int n;
FenwickTree(int _n = 0) {
n = _n;
bit.assign(_n + 5, 0);
}
void update(int id, int delta) {
for (; id <= n; id += id & -id)
bit[id] += delta;
}
int get(int id) {
int res = 0;
for (; id > 0; id -= id & -id)
res += bit[id];
return res;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
};
void solve(void) {
sort(queries + 1, queries + 1 + nAsk);
FenwickTree bit(n);
for (int i = nAsk, j = 1; i >= 1; --i) {
int idQuery = queries[i].se;
while (j <= n && pp[ps[j]] >= queries[i].fi) bit.update(ps[j++], +1);
if (bit.get(l[idQuery], r[idQuery]) >= queries[i].fi) ll[idQuery] = queries[i].fi; else rr[idQuery] = queries[i].fi;
}
}
/*
7 6
3 2 3 1 1 4 7
3 4
1 7
1 6
4 5
1 2
5 7
*/
void process(void) {
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> pp[i];
ps[i] = i;
}
sort(ps + 1, ps + 1 + n, [](int u, int v) {
return pp[u] > pp[v];
});
for (int i = 1; i <= q; ++i) {
cin >> l[i] >> r[i];
ll[i] = 1;
rr[i] = n + 1;
}
for (int t = 0; t < 20; ++t) {
nAsk = 0;
for (int i = 1; i <= q; ++i) if (rr[i] - ll[i] > 1) {
int mid = ll[i] + rr[i] >> 1;
queries[++nAsk] = make_pair(mid, i);
}
solve();
}
for (int i = 1; i <= q; ++i) {
cout << ll[i] << '\n';
}
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int nTest = 1; if (multiTest) cin >> nTest;
while (nTest--) {
process();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
index.cpp: In function 'int main()':
index.cpp:110:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
index.cpp:111:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |