#include<bits/stdc++.h>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define task "test"
#define pii pair<pll, pll>
using namespace std;
using ll = int;
const int N = 2e5+5;
const ll mod = 1e8+7;
const ll base = 113;
const ll inf = 1e9;
ll m, n, t, k, a[N], tong, d[N], b[N], h[N], ans, x[N], y[N], l[N], r[N];
vector<ll> adj[N];
vector<ll> kq, ask[N];
vector<pll> val;
ll pw(ll k, ll n, ll md)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n&1)total = total * k % md;
k = k * k % md;
}
return total;
}
struct BIT
{
ll n;
vector<ll> fe;
BIT(ll _n)
{
n = _n;
fe.resize(n+1, 0);
}
void reset()
{
fill(fe.begin(), fe.end(), 0);
}
void add(ll id, ll x)
{
for(; id <= n; id += id & -id)fe[id] += x;
}
ll get(ll id)
{
ll total = 0;
for(; id ; id -= id & -id)total += fe[id];
return total;
}
};
string s;
void sol()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
val.pb({a[i], i});
}
sort(val.begin(), val.end());
for(int i = 1; i <= m; i ++)
{
cin >> x[i] >> y[i];
l[i] = 1;
r[i] = n;
}
bool ok = true;
BIT bit(n);
while(ok)
{
ok = false;
for(int i = 1; i <= m; i ++)
{
if(l[i] <= r[i])
{
ok = true;
adj[(l[i]+r[i])/2].pb(i);
}
}
for(int i = n, j = n-1; i > 0; i --)
{
while(j >= 0 && val[j].fi >= i)
{
bit.add(val[j].se, 1);
--j;
}
for(ll p: adj[i])
{
if(bit.get(y[p])-bit.get(x[p]-1) >= i)l[p] = i+1;
else r[p] = i-1;
}
adj[i].clear();
}
bit.reset();
}
for(int i = 1; i <= m; i ++)cout << r[i] << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
Compilation message
index.cpp: In function 'int main()':
index.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
index.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9804 KB |
Output is correct |
2 |
Correct |
6 ms |
9804 KB |
Output is correct |
3 |
Correct |
6 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9724 KB |
Output is correct |
5 |
Correct |
7 ms |
9732 KB |
Output is correct |
6 |
Correct |
6 ms |
9804 KB |
Output is correct |
7 |
Correct |
6 ms |
9804 KB |
Output is correct |
8 |
Correct |
6 ms |
9804 KB |
Output is correct |
9 |
Correct |
6 ms |
9804 KB |
Output is correct |
10 |
Correct |
6 ms |
9804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9804 KB |
Output is correct |
2 |
Correct |
6 ms |
9804 KB |
Output is correct |
3 |
Correct |
6 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9724 KB |
Output is correct |
5 |
Correct |
7 ms |
9732 KB |
Output is correct |
6 |
Correct |
6 ms |
9804 KB |
Output is correct |
7 |
Correct |
6 ms |
9804 KB |
Output is correct |
8 |
Correct |
6 ms |
9804 KB |
Output is correct |
9 |
Correct |
6 ms |
9804 KB |
Output is correct |
10 |
Correct |
6 ms |
9804 KB |
Output is correct |
11 |
Correct |
89 ms |
16296 KB |
Output is correct |
12 |
Correct |
83 ms |
16196 KB |
Output is correct |
13 |
Correct |
89 ms |
16444 KB |
Output is correct |
14 |
Correct |
90 ms |
16332 KB |
Output is correct |
15 |
Correct |
85 ms |
16188 KB |
Output is correct |
16 |
Correct |
85 ms |
16244 KB |
Output is correct |
17 |
Correct |
85 ms |
16196 KB |
Output is correct |
18 |
Correct |
92 ms |
16360 KB |
Output is correct |
19 |
Correct |
84 ms |
16220 KB |
Output is correct |
20 |
Correct |
88 ms |
16312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9804 KB |
Output is correct |
2 |
Correct |
6 ms |
9804 KB |
Output is correct |
3 |
Correct |
6 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9724 KB |
Output is correct |
5 |
Correct |
7 ms |
9732 KB |
Output is correct |
6 |
Correct |
6 ms |
9804 KB |
Output is correct |
7 |
Correct |
6 ms |
9804 KB |
Output is correct |
8 |
Correct |
6 ms |
9804 KB |
Output is correct |
9 |
Correct |
6 ms |
9804 KB |
Output is correct |
10 |
Correct |
6 ms |
9804 KB |
Output is correct |
11 |
Correct |
89 ms |
16296 KB |
Output is correct |
12 |
Correct |
83 ms |
16196 KB |
Output is correct |
13 |
Correct |
89 ms |
16444 KB |
Output is correct |
14 |
Correct |
90 ms |
16332 KB |
Output is correct |
15 |
Correct |
85 ms |
16188 KB |
Output is correct |
16 |
Correct |
85 ms |
16244 KB |
Output is correct |
17 |
Correct |
85 ms |
16196 KB |
Output is correct |
18 |
Correct |
92 ms |
16360 KB |
Output is correct |
19 |
Correct |
84 ms |
16220 KB |
Output is correct |
20 |
Correct |
88 ms |
16312 KB |
Output is correct |
21 |
Correct |
405 ms |
38256 KB |
Output is correct |
22 |
Correct |
420 ms |
38308 KB |
Output is correct |
23 |
Correct |
406 ms |
38340 KB |
Output is correct |
24 |
Correct |
397 ms |
38340 KB |
Output is correct |
25 |
Correct |
414 ms |
38200 KB |
Output is correct |
26 |
Correct |
397 ms |
38296 KB |
Output is correct |
27 |
Correct |
395 ms |
38160 KB |
Output is correct |
28 |
Correct |
412 ms |
38432 KB |
Output is correct |
29 |
Correct |
396 ms |
38200 KB |
Output is correct |
30 |
Correct |
403 ms |
37952 KB |
Output is correct |