#include<bits/stdc++.h>
#define pli pair<long long,long long>
using namespace std;
using ll=long long;
const ll maxN=3e5+5;
ll n, a[maxN],id[maxN],st[4*maxN];
void build(ll id, ll l, ll r)
{
if(l==r)
{
st[id]=0;
return;
}
ll mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
st[id]=st[id*2]+st[id*2+1];
}
void update(ll id, ll l, ll r, ll pos, ll val)
{
if(pos<l||pos>r) return;
if(l==r)
{
st[id]=val;
return;
}
ll mid=(l+r)/2;
update(id*2,l,mid,pos,val);
update(id*2+1,mid+1,r,pos,val);
st[id]=st[id*2]+st[id*2+1];
}
ll get(ll id, ll l, ll r, ll i, ll j)
{
if(r<i||j<l) return 0;
if(i<=l&&r<=j)
{
return st[id];
}
ll mid=(l+r)/2;
return get(id*2,l,mid,i,j)+get(id*2+1,mid+1,r,i,j);
}
bool lf(ll x, ll y)
{
return a[x]>a[y];
}
struct TQuery
{
ll l, r, mid, low, high;
}q[maxN];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int que;
cin >> n>> que;
for(int i=1;i<=n;i++) cin >> a[i],id[i]=i;
sort(id+1,id+n+1,lf);
//for(int i=1;i<=n;i++) cout << id[i]<<' ';
for(int i=1;i<=que;i++)
{
cin >> q[i].l >> q[i].r;
q[i].low=1;
q[i].high=q[i].r-q[i].l+1;
}
vector<pli> v;
while(true)
{
v.clear();
build(1,1,n);
for(int i=1;i<=que;i++)
{
if(q[i].low>q[i].high)
{
continue;
}
else
{
q[i].mid=(q[i].low+q[i].high)/2;
v.push_back({q[i].mid,i});
}
}
if(v.size()==0) break;
sort(v.begin(),v.end(),greater<pli>());
int j=1;
for(int i=0;i<v.size();i++)
{
while(a[id[j]]>=v[i].first)
{
update(1,1,n,id[j],1);
j++;
}
ll idx=v[i].second;
ll x=get(1,1,n,q[idx].l,q[idx].r);
if(x>=q[idx].mid) q[idx].low=q[idx].mid+1;
else q[idx].high=q[idx].mid-1;
}
}
for(int i=1;i<=que;i++)
{
cout << q[i].high<<'\n';
}
}
Compilation message
index.cpp: In function 'int main()':
index.cpp:85:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
4 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
464 KB |
Output is correct |
7 |
Correct |
4 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
468 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
4 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
4 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
464 KB |
Output is correct |
7 |
Correct |
4 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
468 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
4 ms |
468 KB |
Output is correct |
11 |
Correct |
412 ms |
6108 KB |
Output is correct |
12 |
Correct |
390 ms |
6104 KB |
Output is correct |
13 |
Correct |
383 ms |
6104 KB |
Output is correct |
14 |
Correct |
385 ms |
6216 KB |
Output is correct |
15 |
Correct |
400 ms |
6100 KB |
Output is correct |
16 |
Correct |
384 ms |
6108 KB |
Output is correct |
17 |
Correct |
416 ms |
6104 KB |
Output is correct |
18 |
Correct |
392 ms |
6216 KB |
Output is correct |
19 |
Correct |
378 ms |
6100 KB |
Output is correct |
20 |
Correct |
379 ms |
6108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
4 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
464 KB |
Output is correct |
7 |
Correct |
4 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
468 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
4 ms |
468 KB |
Output is correct |
11 |
Correct |
412 ms |
6108 KB |
Output is correct |
12 |
Correct |
390 ms |
6104 KB |
Output is correct |
13 |
Correct |
383 ms |
6104 KB |
Output is correct |
14 |
Correct |
385 ms |
6216 KB |
Output is correct |
15 |
Correct |
400 ms |
6100 KB |
Output is correct |
16 |
Correct |
384 ms |
6108 KB |
Output is correct |
17 |
Correct |
416 ms |
6104 KB |
Output is correct |
18 |
Correct |
392 ms |
6216 KB |
Output is correct |
19 |
Correct |
378 ms |
6100 KB |
Output is correct |
20 |
Correct |
379 ms |
6108 KB |
Output is correct |
21 |
Correct |
2245 ms |
23536 KB |
Output is correct |
22 |
Correct |
2165 ms |
23556 KB |
Output is correct |
23 |
Correct |
2169 ms |
23532 KB |
Output is correct |
24 |
Correct |
2199 ms |
23536 KB |
Output is correct |
25 |
Correct |
2134 ms |
23528 KB |
Output is correct |
26 |
Correct |
2272 ms |
23532 KB |
Output is correct |
27 |
Correct |
2242 ms |
23596 KB |
Output is correct |
28 |
Correct |
2255 ms |
23528 KB |
Output is correct |
29 |
Correct |
2243 ms |
23532 KB |
Output is correct |
30 |
Correct |
2206 ms |
23608 KB |
Output is correct |