#include <bits/stdc++.h>
#define ub(x) (x&(-x))
using namespace std;
constexpr int NMAX = 2e5 + 5;
int Bucket;
struct intrebare {
int L, R;
int ind;
bool operator < (const intrebare &other) const {
return (L / Bucket < other.L / Bucket) || (L / Bucket == other.L / Bucket && R < other.R);
}
};
intrebare quer[NMAX];
int N, Q;
int aib[NMAX];
int A[NMAX];
void Update (int pos, int val) {
for (int i = pos; i <= N; i += ub(i))
aib[i] += val;
}
int Query (int pos) {
int S = 0;
for (int i = pos; i; i -= ub(i))
S += aib[i];
return S;
}
void Read () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> Q;
Bucket = Q / sqrt(N);
for (int i = 1; i <= N; ++ i )
cin >> A[i];
for (int i = 1; i <= Q; ++ i ) {
cin >> quer[i].L >> quer[i].R;
quer[i].ind = i;
}
sort(quer+1, quer+Q+1);
}
int sol[NMAX];
void Add (int pos) {
Update(A[pos], +1);
}
void Elimin (int pos) {
Update(A[pos], -1);
}
int Raspuns (int Total) {
int st = 1, dr = N;
int ans = 1;
while (st <= dr) {
int mij = (st + dr) / 2;
int cnt = Query(mij-1);
if (Total - cnt >= mij) {
st = mij + 1;
ans = mij;
}
else dr = mij - 1;
}
return ans;
}
/*
dp[i][j] - i cifre cu ultimele 4 cifre j
dp[i][j] = max(dp[i-1][jprim]) + 1 (j este lucky)
i cifre : a b c m
i-1 cifre: n a b c
dp[i][j][k][l][m] = max(dp[i-1][aux][j][k][l]) + 1
*/
void Solve () {
int st = 1, dr = 0;
for (int i = 1; i <= Q; ++ i ) {
while (dr < quer[i].R) {
++ dr;
Add(dr);
}
while (st > quer[i].L) {
-- st;
Add(st);
}
while (st < quer[i].L) {
Elimin(st);
++ st;
}
while (dr > quer[i].R) {
Elimin(dr);
-- dr;
}
sol[quer[i].ind] = Raspuns(quer[i].R - quer[i].L + 1);
}
for (int i = 1; i <= Q; ++ i )
cout << sol[i] << '\n';
}
int main () {
Read();
Solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
110 ms |
2516 KB |
Output is correct |
12 |
Correct |
104 ms |
2560 KB |
Output is correct |
13 |
Correct |
103 ms |
2496 KB |
Output is correct |
14 |
Correct |
102 ms |
2492 KB |
Output is correct |
15 |
Correct |
102 ms |
2564 KB |
Output is correct |
16 |
Correct |
103 ms |
2556 KB |
Output is correct |
17 |
Correct |
100 ms |
2500 KB |
Output is correct |
18 |
Correct |
105 ms |
2500 KB |
Output is correct |
19 |
Correct |
103 ms |
2560 KB |
Output is correct |
20 |
Correct |
104 ms |
2560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
110 ms |
2516 KB |
Output is correct |
12 |
Correct |
104 ms |
2560 KB |
Output is correct |
13 |
Correct |
103 ms |
2496 KB |
Output is correct |
14 |
Correct |
102 ms |
2492 KB |
Output is correct |
15 |
Correct |
102 ms |
2564 KB |
Output is correct |
16 |
Correct |
103 ms |
2556 KB |
Output is correct |
17 |
Correct |
100 ms |
2500 KB |
Output is correct |
18 |
Correct |
105 ms |
2500 KB |
Output is correct |
19 |
Correct |
103 ms |
2560 KB |
Output is correct |
20 |
Correct |
104 ms |
2560 KB |
Output is correct |
21 |
Correct |
820 ms |
9924 KB |
Output is correct |
22 |
Correct |
805 ms |
9876 KB |
Output is correct |
23 |
Correct |
811 ms |
9844 KB |
Output is correct |
24 |
Correct |
807 ms |
9852 KB |
Output is correct |
25 |
Correct |
827 ms |
9848 KB |
Output is correct |
26 |
Correct |
851 ms |
9844 KB |
Output is correct |
27 |
Correct |
816 ms |
9844 KB |
Output is correct |
28 |
Correct |
830 ms |
9904 KB |
Output is correct |
29 |
Correct |
814 ms |
9844 KB |
Output is correct |
30 |
Correct |
828 ms |
9856 KB |
Output is correct |