# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
288029 | 2qbingxuan | Jousting tournament (IOI12_tournament) | C++17 | 0 ms | 0 KiB |
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>
#ifdef local
#define debug(...) qqbx(#__VA_ARGS__, __VA_ARGS__)
template <typename H, typename ...T> void qqbx(const char *s, const H& h, T &&...args) {
for(; *s && *s != ','; ++s) if(*s != ' ') std::cerr << *s;
std::cerr << " = " << h << (sizeof...(T) ? ", " : "\n");
if constexpr(sizeof...(T)) qqbx(++s, args...);
}
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
#define pb emplace_back
#define all(v) begin(v),end(v)
using namespace std;
vector<pair<int,int>> TransferSE(int S[], int E[], int c, int n) {
vector<int> qq(n, 1);
vector<int> mx(n);
iota(all(mx), 0);
vector<pair<int,int>> res;
for(int i = 0; i < c; i++) {
int cnt = 0;
int L = -1, R = -1;
for(int j = 0; j < n; j++) if(qq[j]) {
if(cnt == S[i]) L = j;
else if(cnt == E[i]) R = mx[j];
++cnt;
}
for(int j = L+1; j <= R; j++) qq[j] = 0;
mx[L] = mx[R];
debug(L, R);
assert(L!=-1 && R!=-1);
res.pb(L, R);
}
return res;
}
struct FenwickTree {
int sum[N];
void add(int p, int d) {
for(++p; p < N; p+=p&-p) sum[p] += d;
}
int query(int p) {
int r = 0;
for(++p; p > 0; p -= p&-p) r += sum[p];
return r;
}
} fwt;
int GetBestPosition(int n, int C, int X, int *K, int *S, int *E) {
auto segs = TransferSE(S, E, C, n);
safe;
vector<tuple<int,bool,int,int>> evt;
for(int i = 0; i < C; i++) {
auto [l, r] = segs[i];
int mx = -1;
for(int j = l; j < r; j++) mx = max(mx, K[j]);
debug(l, r, mx);
evt.pb(l, 1, i, mx);
evt.pb(r, 0, i, mx);
}
sort(all(evt));
int mx = -1, mxpos = -1;
set<int> s;
for(int i = 0, j; i < evt.size(); i = j) {
for(j = i; j < evt.size(); j++) {
if(get<0>(evt[i]) != get<0>(evt[j])) break;
auto [_, t, pos, val] = evt[j];
if(t) {
if(val > X) s.insert(pos);
fwt.add(pos, 1);
} else {
if(val > X) s.erase(pos);
fwt.add(pos, -1);
}
}
int firstLose = s.size() ? *s.begin() : C;
int pos = get<0>(evt[i]);
int win = query(firstLose-1);
if(win > mx) {
mx = win;
mxpos = pos;
}
}
return mxpos;
}