#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define sz(a) (int)a.size()
const int mod = 1e9 + 7;
const int N = 2e5+ 100;
int cnt[N];
const int T = 500;
bool cmp(pair <pair <int, int>, int> a, pair <pair <int, int>, int> b)
{
if((a.X.X / T) == (b.X.X / T))
{
return a.X.Y < b.X.Y;
}
return (a.X.X / T) < (b.X.X / T);
}
int n, q;
int h[N];
signed main()
{
// ifstream cin("input1.txt.4c");
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for(int i = 0; i < n; i++)
{
cin >> h[i];
}
vector <pair <pair <int, int>, int> > queries;
for(int i = 0; i < q; i++)
{
int l, r;
cin >> l >> r;
l--;
r--;
queries.push_back({{l, r}, i});
}
sort(queries.begin(), queries.end(), cmp);
int suma, it;
vector <int> qans(q);
for(int i = 0; i < sz(queries); i++)
{
int l = queries[i].X.X;
int r = queries[i].X.Y;
if(i == 0 || (queries[i].X.X / T) != (queries[i - 1].X.X / T))
{
fill(cnt, cnt + N, 0);
suma = 0;
it = 1;
for(int j = l; j <= r; j++)
{
suma++;
cnt[h[j]]++;
}
}
else
{
for(int j = queries[i - 1].X.Y + 1; j <= r; j++)
{
if(h[j] >= it)
{
suma++;
}
cnt[h[j]]++;
}
for(int j = queries[i].X.X; j < queries[i - 1].X.X; j++)
{
if(h[j] >= it)
{
suma++;
}
cnt[h[j]]++;
}
for(int j = queries[i - 1].X.X; j < queries[i].X.X; j++)
{
if(h[j] >= it)
{
suma--;
}
cnt[h[j]]--;
}
}
while(suma < it)
{
it--;
suma += cnt[it];
}
while(suma - cnt[it] >= it + 1)
{
suma -= cnt[it];
it++;
}/*
for(int e = 0; e <= n; e++)
{
cout << cnt[e] << " ";
}
cout << "\n";
cout << l << " " << r << " " << suma << " " << it << "\n";*/
qans[queries[i].Y] = it;
}
for(int j =0; j < q; j++)
{
cout << qans[j] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1100 KB |
Output is correct |
2 |
Correct |
2 ms |
1100 KB |
Output is correct |
3 |
Correct |
2 ms |
1100 KB |
Output is correct |
4 |
Correct |
2 ms |
1100 KB |
Output is correct |
5 |
Correct |
2 ms |
1100 KB |
Output is correct |
6 |
Correct |
3 ms |
1100 KB |
Output is correct |
7 |
Correct |
2 ms |
1104 KB |
Output is correct |
8 |
Correct |
2 ms |
1100 KB |
Output is correct |
9 |
Correct |
2 ms |
1100 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1100 KB |
Output is correct |
2 |
Correct |
2 ms |
1100 KB |
Output is correct |
3 |
Correct |
2 ms |
1100 KB |
Output is correct |
4 |
Correct |
2 ms |
1100 KB |
Output is correct |
5 |
Correct |
2 ms |
1100 KB |
Output is correct |
6 |
Correct |
3 ms |
1100 KB |
Output is correct |
7 |
Correct |
2 ms |
1104 KB |
Output is correct |
8 |
Correct |
2 ms |
1100 KB |
Output is correct |
9 |
Correct |
2 ms |
1100 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
69 ms |
2332 KB |
Output is correct |
12 |
Correct |
64 ms |
2264 KB |
Output is correct |
13 |
Correct |
64 ms |
2364 KB |
Output is correct |
14 |
Correct |
67 ms |
2388 KB |
Output is correct |
15 |
Correct |
64 ms |
2332 KB |
Output is correct |
16 |
Correct |
65 ms |
2264 KB |
Output is correct |
17 |
Correct |
66 ms |
2292 KB |
Output is correct |
18 |
Correct |
63 ms |
2376 KB |
Output is correct |
19 |
Correct |
64 ms |
2360 KB |
Output is correct |
20 |
Correct |
66 ms |
2360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1100 KB |
Output is correct |
2 |
Correct |
2 ms |
1100 KB |
Output is correct |
3 |
Correct |
2 ms |
1100 KB |
Output is correct |
4 |
Correct |
2 ms |
1100 KB |
Output is correct |
5 |
Correct |
2 ms |
1100 KB |
Output is correct |
6 |
Correct |
3 ms |
1100 KB |
Output is correct |
7 |
Correct |
2 ms |
1104 KB |
Output is correct |
8 |
Correct |
2 ms |
1100 KB |
Output is correct |
9 |
Correct |
2 ms |
1100 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
69 ms |
2332 KB |
Output is correct |
12 |
Correct |
64 ms |
2264 KB |
Output is correct |
13 |
Correct |
64 ms |
2364 KB |
Output is correct |
14 |
Correct |
67 ms |
2388 KB |
Output is correct |
15 |
Correct |
64 ms |
2332 KB |
Output is correct |
16 |
Correct |
65 ms |
2264 KB |
Output is correct |
17 |
Correct |
66 ms |
2292 KB |
Output is correct |
18 |
Correct |
63 ms |
2376 KB |
Output is correct |
19 |
Correct |
64 ms |
2360 KB |
Output is correct |
20 |
Correct |
66 ms |
2360 KB |
Output is correct |
21 |
Correct |
290 ms |
6348 KB |
Output is correct |
22 |
Correct |
300 ms |
9864 KB |
Output is correct |
23 |
Correct |
292 ms |
9868 KB |
Output is correct |
24 |
Correct |
292 ms |
9924 KB |
Output is correct |
25 |
Correct |
293 ms |
9872 KB |
Output is correct |
26 |
Correct |
290 ms |
9900 KB |
Output is correct |
27 |
Correct |
289 ms |
9892 KB |
Output is correct |
28 |
Correct |
295 ms |
9856 KB |
Output is correct |
29 |
Correct |
293 ms |
9800 KB |
Output is correct |
30 |
Correct |
290 ms |
9868 KB |
Output is correct |