This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
int n, q, a[N], b[N];
int ql[N], qr[N], bit[N];
pair<int,int> Q[N];
vector<int> md[N];
void update(int p, int val){
for(; p <= n; p += p&-p) bit[p] += val;
return;
}
int get(int p){
int res = 0;
for(; p; p -= p&-p) res += bit[p];
return res;
}
int get_range(int l, int r){
return get(r) - get(l-1);
}
void solve()
{
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
b[i] = i;
}
for(int i = 1; i <= q; i++){
cin >> Q[i].first >> Q[i].second;
ql[i] = 1, qr[i] = 2e5+1;
}
sort(b+1, b+1+n, [&](int x, int y){
return a[x] > a[y];
});
bool changed = true;
while(changed){
changed = false;
for(int i = 1; i <= q; i++){
if(ql[i]+1 < qr[i]){
changed = true;
md[ql[i]+qr[i]>>1].push_back(i);
}
}
for(int i = 2e5, j = 1; i >= 1; i--){
while(a[b[j]] >= i) update(b[j], 1), j++;
while(md[i].size()){
int id = md[i].back();
int l = Q[id].first, r = Q[id].second;
if(get_range(l, r) >= i) ql[id] = i;
else{
qr[id] = i;
}
md[i].pop_back();
}
}
for(int i = 1; i <= n; i++) bit[i] = 0;
}
for(int i = 1; i <= q; i++) cout << ql[i] <<"\n";
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message (stderr)
index.cpp: In function 'void solve()':
index.cpp:75:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | md[ql[i]+qr[i]>>1].push_back(i);
| ~~~~~^~~~~~
index.cpp: In function 'int main()':
index.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
index.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(name".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... |