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>
using namespace std;
#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define type(x) __typeof(x.begin())
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define pb push_back
#define mp make_pair
#define nd second
#define st first
#define endl '\n'
typedef pair < int ,int > pii;
typedef long long ll;
const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 17;
const int inf = 1e9;
const int N = 2e5 + 5;
class node {
public:
int xx, yy, sum;
node() { xx = yy = sum = 0; }
} ST[N << 2];
int val[N], R, mx[N << 2], a[N], n, m, x, y, z, t, cc, ans, l[N], r[N];
vector< pii > s[N], f[N];
pii ST3[N << 2];
node init(int k, int bas, int son) {
if(bas == son) { ST[k].xx = ST[k].yy = bas; ST[k].sum = 1; return ST[k]; }
init(sol, bas, orta);
init(sag, orta+1, son);
ST[k].xx = bas;
ST[k].yy = son;
ST[k].sum = son - bas + 1;
return ST[k];
}
pii query(int k, int bas, int son, int x) {
if(bas == son) { return mp(ST[k].xx, ST[k].yy); }
if(ST[sol].sum >= x) return query(sol, bas, orta, x);
return query(sag, orta+1, son, x-ST[sol].sum);
}
node update(int k, int bas, int son, int x) {
if(bas == son) { ST[k].xx = ST[k].yy = ST[k].sum = 0; return ST[k]; }
if(ST[sol].sum >= x) update(sol, bas, orta, x);
else update(sag, orta+1, son, x - ST[sol].sum);
ST[k].sum = ST[sol].sum + ST[sag].sum;
return ST[k];
}
node make(int k, int bas, int son, int x, pii t) {
if(bas == son) { ST[k].xx = t.st; ST[k].yy = t.nd; ST[k].sum = 1; return ST[k]; }
if(ST[sol].sum >= x) make(sol, bas, orta, x, t);
else make(sag, orta+1, son, x - ST[k].sum, t);
ST[k].sum = ST[sol].sum + ST[sag].sum;
return ST[k];
}
int init2(int k, int bas, int son) {
if(bas == son) return mx[k] = a[bas];
return mx[k] = max(init2(sag, orta+1, son), init2(sol, bas, orta));
}
int query2(int k, int bas, int son, int x, int y) {
if(bas > y || son < x) return 0;
if(x <= bas && son <= y) return mx[k];
return max(query2(sol, bas, orta, x, y), query2(sag, orta+1, son, x, y));
}
pii update3(int k, int bas, int son, int x, int t) {
if(bas > x || son < x) return ST3[k];
if(bas == son) return ST3[k] = mp(t, t != 0);
update3(sol, bas, orta, x, t);
update3(sag, orta + 1, son, x, t);
return ST3[k] = mp(max(ST3[sol].st, ST3[sag].st), ST3[sol].nd + ST3[sag].nd);
}
int take3(int k, int bas, int son) {
if(bas == son) return ST3[k].nd && ST3[k].st <= R;
if(ST3[sol].st <= R) return take3(sag, orta+1, son) + ST3[sol].nd;
return take3(sol, bas, orta);
}
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) {
n = N;
for(int i = 1; i < n; i++) {
val[i] = 1;
a[i] = ++K[i-1];
} ++R; val[n] = 1;
::R = R;
// init(1, 1, n-1);
init2(1, 1, n);
vector< pair< pii , int > > vv;
for(int i = 1; i <= C; i++) {
int x = S[i-1] + 1,
y = E[i-1] + 1;
int xx = 0, yy = 0, ss = 0;
FOR(j, 1, n) {
ss += val[j];
if(ss == y) { yy = j; break; }
} ss = 0;
int t = 0;
FOR(j, 1, n) {
ss += val[j];
if(ss == x) { xx = j; break; }
}
while(val[xx-1] == 0 && xx > 1) xx--;
int tt = 0;
FOR(j, xx, yy) {
tt += val[j];
val[j] = 0;
}
val[yy] = 1;
// xx = query(1, 1, n-1, x).st,
// yy = query(1, 1, n-1, y).nd;
// make(1, 1, n-1, x, mp(xx, yy));
// for(int j = y; j > x; j--) {
// assert(ST[1].sum >= j);
// update(1, 1, n-1, j);
// }
int mx = query2(1, 1, n, xx, yy-1);
vv.pb(mp(mp(xx, yy-1), mx));
s[xx].pb(mp(i, mx));
f[yy-1].pb(mp(i, mx));
}
int ans = 0, aaa = 0;
for(int i = 1; i <= n; i++) {
foreach(it, s[i]) {
update3(1, 1, C, it->st, it->nd);
}
int cc = take3(1, 1, C);
// cc = 0;
/* foreach(it, vv) {
if(it->st.st <= i && i <= it->st.nd) {
if(it->nd >= R) break;
else cc++;
}
}
*/ if(cc > ans) { ans = cc; aaa = i - 1; }
foreach(it, f[i]) {
update3(1, 1, C, it->st, 0);
}
}
return aaa;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |