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/extc++.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;
using namespace __gnu_pbds;
template <typename T> using rbt = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 100025;
vector<pair<int,int>> TransferSE(int S[], int E[], int c, int n) {
vector<int> mx(n);
iota(all(mx), 0);
vector<pair<int,int>> res;
rbt<int> QQ;
for(int i = 0; i < n; i++) QQ.insert(i);
for(int i = 0; i < c; i++) {
int cnt = 0;
auto itl = QQ.find_by_order(S[i]);
auto itr = QQ.find_by_order(E[i]);
int L = *itl;
int R = mx[*itr];
debug(L, R, i);
++itl, ++itr;
for(auto it = itl; it != itr; QQ.erase(it++));
safe;
// QQ.erase(next(itl), next(itr));
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;
struct SegmentTree {
int mx[N<<1], n;
void init(int v[], int _n) {
n = _n;
for(int i = 0; i < n; i++) mx[i+n] = v[i];
for(int i = n-1; i > 0; i--) mx[i] = max(mx[i<<1], mx[i<<1|1]);
}
int getmx(int l, int r) {
int res = 0;
for(l+=n,r+=n; l<r; l>>=1,r>>=1) {
if(l&1) res = max(res, mx[l++]);
if(r&1) res = max(res, mx[--r]);
}
return res;
}
} sgt;
int GetBestPosition(int n, int C, int X, int *K, int *S, int *E) {
auto segs = TransferSE(S, E, C, n);
safe;
sgt.init(K, n-1);
vector<tuple<int,bool,int,int>> evt;
for(int i = 0; i < C; i++) {
auto [l, r] = segs[i];
int mx = sgt.getmx(l, r);
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 = fwt.query(firstLose-1);
if(win > mx) {
mx = win;
mxpos = pos;
}
}
return mxpos;
}
Compilation message (stderr)
tournament.cpp: In function 'std::vector<std::pair<int, int> > TransferSE(int*, int*, int, int)':
tournament.cpp:29:13: warning: unused variable 'cnt' [-Wunused-variable]
29 | int cnt = 0;
| ^~~
tournament.cpp: In function 'int GetBestPosition(int, int, int, int*, int*, int*)':
tournament.cpp:92:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, bool, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int i = 0, j; i < evt.size(); i = j) {
| ~~^~~~~~~~~~~~
tournament.cpp:93:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, bool, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(j = i; j < evt.size(); j++) {
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |