#include<iostream>
#include<fstream>
#include<stdio.h>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<deque>
#include<math.h>
#include<ctime>
using namespace std;
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define long long long
#define mp(a,b) make_pair(a,b)
#define FI first
#define SE second
#define N 200002
const long mod = 998244353;
long bit[N];
long a[N];
long n,q;
long res[N];
vector<long> pos[N];
void init()
{
for(long i = 1;i <= n;++i)
bit[i] = 0;
}
struct Query{
long u,v,l,r;
} query[N];
void update(long p,long val)
{
for(p;p <= n;p += p&-p)
bit[p] += val;
}
long get(long p)
{
long res = 0;
for(p;p > 0;p -= p&-p)
res += bit[p];
return res;
}
vector<long> cur[N];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> q;
for(long i = 1;i <= n;i++)
{
cin >> a[i];
pos[min(a[i],n)].push_back(i);
}
for(long i = 1;i <= q;i++)
{
res[i] = 1;
cin >>query[i].u >> query[i].v;
query[i].l = 1;
query[i].r = n;
}
for(long num = 1;num <= 30;num++)
{
for(long i = 1;i <= n;i++)
cur[i].clear();
for(long i = 1;i <= q;i++)
if(query[i].l <= query[i].r)
cur[(query[i].l+query[i].r)/2].push_back(i);
init();
for(long i = n;i >= 1;i--)
{
for(long j:pos[i])
{
update(j,1);
// cout << j << " ";
}
for(long j:cur[i])
{
long l = query[j].u;
long r = query[j].v;
long mid = (query[j].l+query[j].r)/2;
if(get(r) - get(l-1) >= i)
{
res[j] = i;
// cout << get(r) - get(l-1) << " " << l << " " <<r << " " <<endl;
query[j].l = mid + 1;
//cout << j <<" "<<i<<endl;
}
else
query[j].r = mid - 1;
}
}
// cout << endl;
}
for(long i = 1;i <= q;i++)
cout << res[i] <<endl;
cerr<<"Times: "<<TIME<<endl;
}
Compilation message
index.cpp: In function 'void update(long long int, long long int)':
index.cpp:45:9: warning: statement has no effect [-Wunused-value]
45 | for(p;p <= n;p += p&-p)
| ^
index.cpp: In function 'long long int get(long long int)':
index.cpp:52:9: warning: statement has no effect [-Wunused-value]
52 | for(p;p > 0;p -= p&-p)
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
5 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
15156 KB |
Output is correct |
4 |
Correct |
5 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
5 ms |
14940 KB |
Output is correct |
7 |
Correct |
5 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14940 KB |
Output is correct |
9 |
Correct |
5 ms |
14940 KB |
Output is correct |
10 |
Correct |
5 ms |
14936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
5 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
15156 KB |
Output is correct |
4 |
Correct |
5 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
5 ms |
14940 KB |
Output is correct |
7 |
Correct |
5 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14940 KB |
Output is correct |
9 |
Correct |
5 ms |
14940 KB |
Output is correct |
10 |
Correct |
5 ms |
14936 KB |
Output is correct |
11 |
Correct |
156 ms |
25700 KB |
Output is correct |
12 |
Correct |
146 ms |
25044 KB |
Output is correct |
13 |
Correct |
144 ms |
25148 KB |
Output is correct |
14 |
Correct |
144 ms |
25060 KB |
Output is correct |
15 |
Correct |
152 ms |
25220 KB |
Output is correct |
16 |
Correct |
146 ms |
25068 KB |
Output is correct |
17 |
Correct |
147 ms |
25040 KB |
Output is correct |
18 |
Correct |
148 ms |
25532 KB |
Output is correct |
19 |
Correct |
147 ms |
25012 KB |
Output is correct |
20 |
Correct |
145 ms |
25052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14936 KB |
Output is correct |
2 |
Correct |
5 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
15156 KB |
Output is correct |
4 |
Correct |
5 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
5 ms |
14940 KB |
Output is correct |
7 |
Correct |
5 ms |
14940 KB |
Output is correct |
8 |
Correct |
5 ms |
14940 KB |
Output is correct |
9 |
Correct |
5 ms |
14940 KB |
Output is correct |
10 |
Correct |
5 ms |
14936 KB |
Output is correct |
11 |
Correct |
156 ms |
25700 KB |
Output is correct |
12 |
Correct |
146 ms |
25044 KB |
Output is correct |
13 |
Correct |
144 ms |
25148 KB |
Output is correct |
14 |
Correct |
144 ms |
25060 KB |
Output is correct |
15 |
Correct |
152 ms |
25220 KB |
Output is correct |
16 |
Correct |
146 ms |
25068 KB |
Output is correct |
17 |
Correct |
147 ms |
25040 KB |
Output is correct |
18 |
Correct |
148 ms |
25532 KB |
Output is correct |
19 |
Correct |
147 ms |
25012 KB |
Output is correct |
20 |
Correct |
145 ms |
25052 KB |
Output is correct |
21 |
Correct |
913 ms |
63564 KB |
Output is correct |
22 |
Correct |
955 ms |
63748 KB |
Output is correct |
23 |
Correct |
989 ms |
64340 KB |
Output is correct |
24 |
Correct |
927 ms |
63796 KB |
Output is correct |
25 |
Correct |
904 ms |
64360 KB |
Output is correct |
26 |
Correct |
942 ms |
63996 KB |
Output is correct |
27 |
Correct |
974 ms |
63960 KB |
Output is correct |
28 |
Correct |
900 ms |
64480 KB |
Output is correct |
29 |
Correct |
870 ms |
63340 KB |
Output is correct |
30 |
Correct |
826 ms |
64200 KB |
Output is correct |