#include <bits/stdc++.h>
#define ll int
#define loop(i, a, b) for(ll i=a;i<b;++i)
#define pool(i, a, b) for(ll i=a-1;i>=b;--i)
#define fore(i, a) for(auto&& i:a)
#define fi first
#define se second
#define ps(a) push_back(a)
#define pb(a) pop_back(a)
#define eb(...) emplace_back(__VA_ARGS__)
#define sc scanf
#define vc vector
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define llmax LLONG_MAX/2
#define llmin -LLONG_MAX/2
using namespace std;
#define mn 500100
#define par pair<ll, ll>
#define ld long double
#define mod 1000000007
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, q;cin >> n >> q;
vc<vc<ll>> tr(2 * n);
loop(i, 0, n) {ll t;cin >> t;tr[i+n].eb(t);}
pool(i, n, 1) tr[i].resize(tr[i * 2].size() + tr[i * 2 + 1].size());
pool(no, n, 1) merge(all(tr[no * 2]), all(tr[no * 2 +1]), tr[no].begin());
vc<vc<ll>> prno(q);
loop(i, 0, q){
ll ql, qr;cin >> ql >> qr;--ql;
for(ql+=n, qr+=n;ql<qr;ql>>=1, qr>>=1){
if(ql&1) prno[i].ps(ql++);
if(qr&1) prno[i].ps(--qr);
}
}
vc<ll> l(q, 1), r(q, n), mid(q);
while(1){
vc<ll> act;
loop(i, 0, q) if(l[i]<r[i]) act.ps(i), mid[i]=(l[i]+r[i]+1)/2;
if(act.size()==0) break;
sort(all(act), [&](ll a, ll b){return mid[a]>mid[b];});
vc<vc<ll>> ques(2 * n);
vc<ll> sum(q, 0);
fore(i, act){
fore(v, prno[i]) ques[v].eb(i);
}
loop(i, 1, 2 * n) if(ques[i].size()){
ll k=tr[i].size();
loop(j, 0, k) while(ques[i].size() and tr[i][j]>=mid[ques[i].back()]){
sum[ques[i].back()]+=k-j;
ques[i].pop_back();
}
}
fore(i, act){
if(mid[i]<=sum[i]) l[i]=mid[i];
else r[i]=mid[i]-1;
}
}
loop(i, 0, q) cout << l[i] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
588 KB |
Output is correct |
2 |
Correct |
5 ms |
588 KB |
Output is correct |
3 |
Correct |
5 ms |
588 KB |
Output is correct |
4 |
Correct |
6 ms |
588 KB |
Output is correct |
5 |
Correct |
5 ms |
588 KB |
Output is correct |
6 |
Correct |
6 ms |
588 KB |
Output is correct |
7 |
Correct |
5 ms |
588 KB |
Output is correct |
8 |
Correct |
5 ms |
588 KB |
Output is correct |
9 |
Correct |
6 ms |
588 KB |
Output is correct |
10 |
Correct |
6 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
588 KB |
Output is correct |
2 |
Correct |
5 ms |
588 KB |
Output is correct |
3 |
Correct |
5 ms |
588 KB |
Output is correct |
4 |
Correct |
6 ms |
588 KB |
Output is correct |
5 |
Correct |
5 ms |
588 KB |
Output is correct |
6 |
Correct |
6 ms |
588 KB |
Output is correct |
7 |
Correct |
5 ms |
588 KB |
Output is correct |
8 |
Correct |
5 ms |
588 KB |
Output is correct |
9 |
Correct |
6 ms |
588 KB |
Output is correct |
10 |
Correct |
6 ms |
588 KB |
Output is correct |
11 |
Correct |
555 ms |
23704 KB |
Output is correct |
12 |
Correct |
550 ms |
23884 KB |
Output is correct |
13 |
Correct |
576 ms |
23604 KB |
Output is correct |
14 |
Correct |
580 ms |
23676 KB |
Output is correct |
15 |
Correct |
535 ms |
23504 KB |
Output is correct |
16 |
Correct |
538 ms |
23628 KB |
Output is correct |
17 |
Correct |
549 ms |
23684 KB |
Output is correct |
18 |
Correct |
548 ms |
23692 KB |
Output is correct |
19 |
Correct |
544 ms |
23728 KB |
Output is correct |
20 |
Correct |
549 ms |
23496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
588 KB |
Output is correct |
2 |
Correct |
5 ms |
588 KB |
Output is correct |
3 |
Correct |
5 ms |
588 KB |
Output is correct |
4 |
Correct |
6 ms |
588 KB |
Output is correct |
5 |
Correct |
5 ms |
588 KB |
Output is correct |
6 |
Correct |
6 ms |
588 KB |
Output is correct |
7 |
Correct |
5 ms |
588 KB |
Output is correct |
8 |
Correct |
5 ms |
588 KB |
Output is correct |
9 |
Correct |
6 ms |
588 KB |
Output is correct |
10 |
Correct |
6 ms |
588 KB |
Output is correct |
11 |
Correct |
555 ms |
23704 KB |
Output is correct |
12 |
Correct |
550 ms |
23884 KB |
Output is correct |
13 |
Correct |
576 ms |
23604 KB |
Output is correct |
14 |
Correct |
580 ms |
23676 KB |
Output is correct |
15 |
Correct |
535 ms |
23504 KB |
Output is correct |
16 |
Correct |
538 ms |
23628 KB |
Output is correct |
17 |
Correct |
549 ms |
23684 KB |
Output is correct |
18 |
Correct |
548 ms |
23692 KB |
Output is correct |
19 |
Correct |
544 ms |
23728 KB |
Output is correct |
20 |
Correct |
549 ms |
23496 KB |
Output is correct |
21 |
Execution timed out |
2577 ms |
99676 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |