Submission #1260932

#TimeUsernameProblemLanguageResultExecution timeMemory
1260932tminhIndex (COCI21_index)C++20
60 / 110
2504 ms49964 KiB
#include "bits/stdc++.h"
using namespace std;

#define task ""
#define ll long long
#define endl '\n'
#define fi first
#define se second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (int)a.size()
#define pii pair<int, int>
#define pll pair<ll, ll>


#define ep emplace_back
#define pb push_back
#define pf push_front


const ll mod = 1e9 + 7;
const int N = 2e5 + 5;
const ll oo = 1e18;



bool START;

int n, q; 
pii a[N];
struct ST {
	vector<int> st[N << 2];
	void merge(int id) {
		int i = 0, j = 0;
		while(i < sze(st[id << 1]) && j < sze(st[id << 1 | 1])) {
			if (st[id << 1][i] < st[id << 1 | 1][j]) {
				st[id].pb(st[id << 1][i++]);
			} else {
				st[id].pb(st[id << 1 | 1][j++]);
			}
		}
		while(i < sze(st[id << 1])) st[id].pb(st[id << 1][i++]);
		while(j < sze(st[id << 1 | 1])) st[id].pb(st[id << 1 | 1][j++]);
	}
	void build(int id, int l, int r) {
		if (l == r) {
			st[id].pb(a[l].se);
			return;
		}
		int mid = (l + r) >> 1;
		build(id << 1, l, mid);
		build(id << 1 | 1, mid + 1, r);
		merge(id);
	}
	int get(int id, int l, int r, int u, int v, int k) {
		if (l == r) return st[id].back();
		int p = upper_bound(vall(st[id << 1]), v) - lower_bound(vall(st[id << 1]), u);
		
		int mid = (l + r) >> 1;
		if (p >= k) return get(id << 1, l, mid, u, v, k);
		return get(id << 1 | 1, mid + 1, r, u, v, k - p);
	}
} st;

int b[N];
bool check(int x, int l, int r) {
	return b[st.get(1, 1, n, l, r, (r - l + 1) - x + 1)] >= x;
}

inline void solve() {
	sort(a + 1, a + 1 + n);
	st.build(1, 1, n);
	
	
	while(q--) {
		int L, R;
		cin >> L >> R;
		
		int l = 0, r = R - L + 1, ans = 0;
		while(l <= r) {
			int mid = (l + r) >> 1;
			if (check(mid, L, R)) {
				ans = mid;
				l = mid + 1;
			} else {
				r = mid - 1;
			}
		}
		cout << ans << endl;
	}
}


inline void input() {
    cin >> n >> q;
    for (int i = 1; i <= n; ++i) {
    	cin >> a[i].fi;
    	b[i] = a[i].fi;
    	a[i].se = i;
    }
    return solve();
}
bool END;

int main() {
    if(fopen(task ".inp", "r")) {
        freopen(task ".inp", "r", stdin);
        freopen(task ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    input();
    
    
    cerr << endl << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl;
    cerr << "Memory: " << fabs ((&END - &START)) / 1048576.0 << "MB\n";
    return 0;
}

Compilation message (stderr)

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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...