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 <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
#define mp make_pair
#define f first
#define s second
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 1e5+5;
const ll INF = 1e18;
const ld PI = 4*atan((ld)1);
const int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
namespace io {
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
// cin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int
if (sz(s)) {
setIn(s+".in");
setOut(s+".out");
} // for USACO
}
}
using namespace io;
namespace input {
template<class T> void re(complex<T>& x);
template<class T1, class T2> void re(pair<T1,T2>& p);
template<class T> void re(vector<T>& a);
template<class T, size_t SZ> void re(array<T,SZ>& a);
template<class T> void re(T& x) { cin >> x; }
void re(double& x) { string t; re(t); x = stod(t); }
void re(ld& x) { string t; re(t); x = stold(t); }
template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
re(first); re(rest...);
}
template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}
namespace output {
template<class T1, class T2> void pr(const pair<T1,T2>& x);
template<class T, size_t SZ> void pr(const array<T,SZ>& x);
template<class T> void pr(const vector<T>& x);
template<class T> void pr(const set<T>& x);
template<class T1, class T2> void pr(const map<T1,T2>& x);
template<class T> void pr(const T& x) { cout << x; }
template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
pr(first); pr(rest...);
}
template<class T1, class T2> void pr(const pair<T1,T2>& x) {
pr("{",x.f,", ",x.s,"}");
}
template<class T> void prContain(const T& x) {
pr("{");
bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
pr("}");
}
template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
template<class T> void pr(const vector<T>& x) { prContain(x); }
template<class T> void pr(const set<T>& x) { prContain(x); }
template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
void ps() { pr("\n"); }
template<class Arg> void ps(const Arg& first) {
pr(first); ps(); // no space at end of line
}
template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
pr(first," "); ps(rest...); // print w/ spaces
}
}
using namespace output;
using namespace input;
ll add(ll a, ll b) {
a += b;
if(a >= MOD) {
a -= MOD;
}
return a;
}
ll sub(ll a, ll b) {
a -= b;
if(a < 0) {
a += MOD;
}
return a;
}
ll mul(ll a, ll b) {
return (a * b)%MOD;
}
void add_self(ll& a, ll b) {
a = add(a, b);
}
void sub_self(ll& a, ll b) {
a = sub(a, b);
}
void mul_self(ll& a, ll b) {
a = mul(a, b);
}
pl rounds[MX];
ll t[4*MX], lazy[4*MX], lazymax[4*MX];
pl tmax[4*MX];
pl intervals[MX];
ll rwin[MX];
ll maxdepth[MX];
bool cmp(pl a, pl b) {
if (a.f != b.f) {
return a.f < b.f;
} else {
ll c = intervals[a.s].s, d = intervals[b.s].s;
if ((c == a.f) != (d == b.f)) {
return c == a.f;
} else {
return c > d;
}
}
}
void build(ll curr, ll tl, ll tr) {
if (tl == tr) {
t[curr] = 1;
} else {
ll tm = (tl+tr)/2;
build(curr*2, tl, tm);
build(curr*2+1, tm+1, tr);
t[curr] = t[curr*2]+t[curr*2+1];
}
}
void push(ll curr) {
if (lazy[curr]) {
t[curr*2] = 0;
lazy[curr*2] = 1;
t[curr*2+1] = 0;
lazy[curr*2+1] = 1;
lazy[curr] = 0;
}
}
void upd(ll curr, ll tl, ll tr, ll l, ll r) {
if (l > r) {
return;
}
if (tl == l && tr == r) {
lazy[curr] = 1;
t[curr] = 0;
} else {
ll tm = (tl+tr)/2;
push(curr);
upd(curr*2, tl, tm, l, min(r, tm));
upd(curr*2+1, tm+1, tr, max(tm+1, l), r);
t[curr] = t[curr*2]+t[curr*2+1];
}
}
ll qry(ll curr, ll tl, ll tr, ll val) {
if (tl == tr) {
return tl;
}
push(curr);
ll tm = (tl+tr)/2;
if (t[curr*2] > val) {
return qry(curr*2, tl, tm, val);
} else {
return qry(curr*2+1, tm+1, tr, val-t[curr*2]);
}
}
void pushmax(ll curr) {
if (lazymax[curr]) {
tmax[curr*2].f+=lazymax[curr];
lazymax[curr*2]+=lazymax[curr];
tmax[curr*2+1].f+=lazymax[curr];
lazymax[curr*2+1]+=lazymax[curr];
lazymax[curr] = 0;
}
}
void updmax(ll curr, ll tl, ll tr, ll l, ll r, ll val, ll node) {
if (l > r) {
return;
}
if (tl == l && tr == r) {
if (l == r && node != -1e8) {
tmax[curr].s = node;
}
tmax[curr].f+=val;
lazymax[curr]+=val;
} else {
ll tm = (tl+tr)/2;
pushmax(curr);
updmax(curr*2, tl, tm, l, min(tm, r), val, node);
updmax(curr*2+1, tm+1, tr, max(tm+1, l), r, val, node);
tmax[curr] = max(tmax[curr*2], tmax[curr*2+1]);
}
}
pl qrymax(ll curr, ll tl, ll tr, ll l, ll r) {
if (l > r) {
return mp(-1e8, -1e8);
}
if (tl >= l && tr <= r) {
return tmax[curr];
}
pushmax(curr);
ll tm = (tl+tr)/2;
return max(qrymax(curr*2, tl, tm, l, min(tm, r)), qrymax(curr*2+1, tm+1, tr, max(tm+1, l), r));
}
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) {
build(1, 1, N);
FOR (i, 1, N) {
updmax(1, 1, N, i, i, K[i-1], i);
}
updmax(1, 1, N, N, N, -1, N);
F0R (i, C) {
rounds[i+1] = {S[i]+1, E[i]+1};
}
multiset<ll> sweep;
vector<pl> events;
FOR (i, 1, C+1) {
ll L = qry(1, 1, N, rounds[i].f-1);
ll R = qry(1, 1, N, rounds[i].s-1);
pl winner = qrymax(1, 1, N, L, R);
intervals[i] = {L, R};
if (winner.s != 1)
upd(1, 1, N, L, winner.s-1);
if (winner.s != N) {
upd(1, 1, N, winner.s+1, R);
}
rwin[i] = winner.f;
events.emplace_back(L, i);
events.emplace_back(R, i);
}
memset(tmax, 0, sizeof(tmax));
memset(lazymax, 0, sizeof(lazymax));
sort(events.begin(), events.end(), cmp);
F0R (i, events.size()) {
if (events[i].f == intervals[events[i].s].f) {
sweep.insert(rwin[events[i].s]);
} else {
ll put;
if (K[intervals[events[i].s].s-1] == rwin[events[i].s]) {
put = intervals[events[i].s].s;
} else {
put = intervals[events[i].s].s;
}
updmax(1, 1, N, put, put, max(sz(sweep)-qrymax(1, 1, N, put, put).f, 0ll), -1e8);
// cout << "asdf" << " " << sz(sweep) << endl;
sweep.erase(sweep.find(rwin[events[i].s]));
ll asdf = intervals[events[i].s].f, asdf2 = intervals[events[i].s].s;
// cout << qrymax(1, 1, N, asdf, asdf2).f << endl;
maxdepth[events[i].s] = qrymax(1, 1, N, asdf, asdf2).f-sz(sweep);
}
}
ll best = -1;
FOR (i, 1, C+1) {
if (rwin[i] < R || (K[intervals[i].s-1] == rwin[i] && rwin[i]-1 == R)) {
best = max(best, maxdepth[i]);
}
}
return best;
}
Compilation message (stderr)
tournament.cpp: In function 'int GetBestPosition(int, int, int, int*, int*, int*)':
tournament.cpp:53:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
tournament.cpp:54:18: note: in expansion of macro 'FOR'
54 | #define F0R(i,a) FOR(i,0,a)
| ^~~
tournament.cpp:311:5: note: in expansion of macro 'F0R'
311 | F0R (i, events.size()) {
| ^~~
tournament.cpp: In function 'void io::setIn(std::string)':
tournament.cpp:67:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
67 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
tournament.cpp: In function 'void io::setOut(std::string)':
tournament.cpp:68:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
68 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |