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>
#define ll long long
#define F first
#define S second
#define MAXN 200005
#define MAXTREE 3600005
#define ii pair<int,int>
#define bit(i,j) ((i>>j)&1)
#define sz(i) (int)i.size()
#define endl '\n'
using namespace std;
int n, ntest;
int a[MAXN];
namespace sub1
{
int node, MX;
int st[MAXTREE], root[MAXN];
ii child[MAXTREE];
void update(int id, int pre, int l, int r, int u)
{
if(l == r)
return st[id]=st[pre]+1, void();
int mid = (l + r) / 2;
node++;
if(u <= mid)
{
child[id].F = node;
child[id].S = child[pre].S;
update(node, child[pre].F, l, mid, u);
}
else
{
child[id].F = child[pre].F;
child[id].S = node;
update(node, child[pre].S, mid + 1, r, u);
}
st[id] = st[child[id].F] + st[child[id].S];
}
int get(int id, int l, int r, int u, int v)
{
if(l > v || r < u)
return 0;
if(l >= u && r <= v)
return st[id];
int mid = (l + r) / 2;
return get(child[id].F, l, mid, u, v) + get(child[id].S, mid + 1, r, u, v);
}
int bina(int L, int R)
{
int l = 1, r = R - L + 1;
while(l < r)
{
int mid = (l + r+1) / 2;
if(get(root[R], 1, MX, mid, MX) - get(root[L - 1], 1, MX, mid, MX) >= mid)
l = mid;
else
r = mid - 1;
}
return l;
}
void solve()
{
MX = *max_element(a + 1, a + 1 + n);
for(int i = 1; i <= n; i++)
{
node++;
root[i] = node;
update(node, root[i - 1], 1, MX, a[i]);
}
// cout << get(root[n], 1, MX, 1, MX) << endl;
for(int i = 1; i <= ntest; i++)
{
int l, r;
cin >> l >> r;
cout << bina(l, r) << endl;
}
}
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> ntest;
for(int i = 1; i <= n; i++)
cin >> a[i];
sub1::solve();
}
Compilation message (stderr)
index.cpp:79:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
79 | main()
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |