/* Vu Quang Sang from Le Khiet High School for Gifted */
#include <bits/stdc++.h>
using namespace std;
#define lg(x) __lg(x)
#define alla(a,n) a+1,a+n+1
#define el "\n"
#define all(x) x.begin(),x.end()
#define pb push_back
#define ll long long
#define fi first
#define se second
#define Mask(i) (1ll<<(i))
#define c_bit(i) __builtin_popcountll(i)
#define fr(i,a,b) for(int i = (a), _b = (b); i <= _b; i++)
#define frd(i,a,b) for(int i = (a), _b = (b); i >= _b; i--)
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; i++)
const ll sm = 1e9+7;
const long long inf = 1e18+7;
const ll base = 311;
const ll BLOCK = 320;
const int dx[5] = {0, -1, 1, 0 , 0};
const int dy[5] = {0, 0, 0, -1, 1};
struct node{ll x,y;};
struct pll{ll fi,se;};
template <class T> bool maxi(T &x, T y) { if(x < y) { x = y ; return true ;} return false;}
template <class T> bool mini(T &x, T y) { if(x > y) { x = y ; return true ;} return false;}
ll gcd(ll a, ll b) {return __gcd(a, b);}
ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}
const int N = 2e5 + 2;
int n, q, a[N];
node Q[N];
void input()
{
cin >> n >> q; fr(i, 1, n) cin >> a[i];
fr(i, 1, q) cin >> Q[i].x >> Q[i].y;
}
namespace sub1
{
int L[N], R[N], ans[N];
vector<int> qry[N];
vector<int> pos[N];
// vector<int> st[4 * N];
//
// vector<int> MER(vector<int> a, vector<int> b)
// {
// vector<int> res(a.size() + b.size());
// merge(all(a), all(b), res.begin());
// return res;
// }
// void build(int id, int l, int r)
// {
// if(l == r) {
// st[id].pb(a[l]);
// return;
// }
// int mid = (r + l) >> 1;
// build(id << 1, l, mid);
// build(id << 1 | 1, mid + 1, r);
// st[id] = MER(st[id << 1], st[id << 1 | 1]);
// }
// int get(int id, int l, int r, int u, int v, int val)
// {
// if(r < u || v < l) return 0;
// if(u <= l && r <= v) {
// return st[id].end() - lower_bound(all(st[id]), val);
// }
// int mid = (r + l) >> 1;
// return get(id << 1, l, mid, u, v, val) + get(id << 1 | 1, mid + 1, r, u, v, val);
// }
int bit[N + 2];
void upd(int x, int v)
{
for(;x <= 2e5; x += x & -x) bit[x] += v;
}
int get(int x)
{
int ans = 0 ;
for(; x >= 1; x -= x & -x) ans += bit[x];
return ans;
}
int get(int l, int r)
{
return get(r) - get(l - 1);
}
void slv()
{
fr(i, 1, q) L[i] = 1, R[i] = Q[i].y - Q[i].x + 1;
fr(i, 1, n) pos[a[i]].pb(i);
// build(1, 1, n);
while(1){
bool ok = 0;
fr(i, 1, q) if(L[i] <= R[i]) {
ok = 1;
qry[(L[i] + R[i]) / 2].pb(i);
}
if(!ok) break;
// voi 1 doan l -> r dem xem co bao nhieu so >= val, upd vi tri bang 1, dem so vi tri
frd(val, n, 1){
for(int x : pos[val]) upd(x, 1);
for(int id : qry[val]) {
int mid = (L[id] + R[id]) / 2;
int l = Q[id].x, r = Q[id].y;
if(get(l, r) >= val) {
ans[id] = val;
L[id] = mid + 1;
}
else R[id] = mid - 1;
}
}
fr(i, 1, 2e5) bit[i] = 0;
fr(i, 1, n) qry[i].clear();
}
fr(i, 1, q) cout << ans[i] << el;
}
}
namespace sub2
{
void slv()
{
}
}
main()
{
#define TASK "task"
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);
}
bool qs = 0;
ll mtt = 1;
if(qs) cin >> mtt;
fr(i, 1, mtt){
input();
sub1::slv();
// sub2::slv();
}
}
Compilation message (stderr)
index.cpp:131:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
131 | main()
| ^~~~
index.cpp: In function 'int main()':
index.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen( TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
index.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | 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... |