This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
#define ff first
#define ss second
#define pb push_back
/// #define int ll
const int MOD = 1e9 + 7;
int st[400001], par[100001], mx[100001];
void update(int v, int l, int r, int pos, int val) {
if(l == r) {
st[v] = val;
return;
}
int md = (l + r) / 2;
if(pos <= md) update(v * 2, l, md, pos, val);
else update(v * 2 + 1, md + 1, r, pos, val);
st[v] = max(st[v * 2], st[v * 2 + 1]);
}
int query0(int v, int l, int r, int lo, int hi) {
if(l > hi || r < lo) return 0;
if(l >= lo && r <= hi) return st[v];
return max(query0(v * 2, l, (l + r) / 2, lo, hi), query0(v * 2 + 1, (l + r) / 2 + 1, r, lo, hi));
}
int findSet(int v) {
if(par[v] == v) return v;
return par[v] = findSet(par[v]);
}
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) {
int res = 0, curr = 0;
vector<ii> query; vector<int> s;
for(int l = 0; l < N; l++) s.push_back(l);
for(int l = 0; l < C; l++) {
int lf = S[l], rf = E[l]; query.pb({s[lf], s[rf]});
s.erase(s.begin() + lf, s.begin() + rf);
}
for(int l = 0; l < N; l++) {
for(int i = 0; i < l; i++) {
par[i] = i, mx[i] = K[i];
update(1, 0, N - 1, i, mx[i]);
}
par[l] = l; mx[l] = R; update(1, 0, N - 1, l, mx[l]);
for(int i = l; i < N - 1; i++) {
par[i + 1] = i + 1, mx[i + 1] = K[i];
update(1, 0, N - 1, i + 1, K[i]);
}
int calc = 0;
for(auto u : query) {
int lf = u.ff, rf = u.ss;
/// lf = findSet(lf), rf = findSet(rf);
/// if(lf > rf) swap(lf, rf);
/// par[rf] = lf; mx[lf] = max({mx[lf], mx[rf]});
int val = query0(1, 0, N - 1, lf, rf);
if(val == R) calc++;
}
if(calc > curr) {
curr = calc, res = l;
}
}
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |