#include <bits/stdc++.h>
using namespace std;
#define speed ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define rall(x) x.rbegin(), x.rend()
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define nl '\n'
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
const int N = 2e5 + 5;
const int M = 1e3 + 5;
const int inf = 1e9 + 123;
const ll infl = 1e15 + 10;
const int mod = 1e9 + 7;
const int P = 31;
int n, q, a[N], t[N << 2], ans[N];
struct query{
int l, r, id;
};
vector <query> qs;
void upd(int v, int tl, int tr, int pos, int x){
if(tl == tr){
t[v] += x;
return;
}
int tm = (tl + tr) >> 1;
if(pos <= tm)
upd(v + v, tl, tm, pos, x);
else
upd(v + v + 1, tm + 1, tr, pos, x);
t[v] = t[v + v] + t[v + v + 1];
}
int get(int v, int tl, int tr, int l, int r){
if(tl > r || l > tr)
return 0;
if(l <= tl && tr <= r)
return t[v];
int tm = (tl + tr) >> 1;
return get(v + v, tl, tm, l, r) + get(v + v + 1, tm + 1, tr, l, r);
}
void add(int x){
upd(1, 1, N, x, 1);
}
void del(int x){
upd(1, 1, N, x, -1);
}
void solve() {
cin >> n >> q;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i <= q; i++){
int l, r;
cin >> l >> r;
qs.pb({l, r, i});
}
sort(all(qs), [](query a, query b){
return a.r < b.r;
});
int l = 1, r = 0;
for(auto Q : qs){
while (r < Q.r)
add(a[++r]);
while (l < Q.l)
del(a[l++]);
while (l > Q.l)
add(a[--l]);
int l = 1, r = n + 1, mid;
while(r - l > 1){
mid = (l + r) >> 1;
if(get(1, 1, N, mid, N) >= mid)
l = mid;
else
r = mid;
}
ans[Q.id] = l;
}
for(int i = 1; i <= q; i++)
cout << ans[i] << nl;
}
signed main() {
speed;
int T = 1;
// cin >> T;
while (T--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6492 KB |
Output is correct |
2 |
Correct |
5 ms |
6492 KB |
Output is correct |
3 |
Correct |
4 ms |
6620 KB |
Output is correct |
4 |
Correct |
5 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6492 KB |
Output is correct |
7 |
Correct |
4 ms |
6692 KB |
Output is correct |
8 |
Correct |
4 ms |
6492 KB |
Output is correct |
9 |
Correct |
4 ms |
6492 KB |
Output is correct |
10 |
Correct |
5 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6492 KB |
Output is correct |
2 |
Correct |
5 ms |
6492 KB |
Output is correct |
3 |
Correct |
4 ms |
6620 KB |
Output is correct |
4 |
Correct |
5 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6492 KB |
Output is correct |
7 |
Correct |
4 ms |
6692 KB |
Output is correct |
8 |
Correct |
4 ms |
6492 KB |
Output is correct |
9 |
Correct |
4 ms |
6492 KB |
Output is correct |
10 |
Correct |
5 ms |
6492 KB |
Output is correct |
11 |
Execution timed out |
2537 ms |
8816 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6492 KB |
Output is correct |
2 |
Correct |
5 ms |
6492 KB |
Output is correct |
3 |
Correct |
4 ms |
6620 KB |
Output is correct |
4 |
Correct |
5 ms |
6492 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6492 KB |
Output is correct |
7 |
Correct |
4 ms |
6692 KB |
Output is correct |
8 |
Correct |
4 ms |
6492 KB |
Output is correct |
9 |
Correct |
4 ms |
6492 KB |
Output is correct |
10 |
Correct |
5 ms |
6492 KB |
Output is correct |
11 |
Execution timed out |
2537 ms |
8816 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |