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 FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
typedef long long ll;
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (100006)
ll n, c, r, A[MAXN];
struct node {
int s,e,m;
ll v;
node *l,*r;
node(int S,int E){
s=S,e=E,m=(s+e)>>1;
v=0;
if(s^e){
l=new node(s,m);
r=new node(m+1,e);
v=max(l->v,r->v);
} else v=A[s];
}
ll rmq(int x,int y){
if(s==x&&e==y) return v;
if(x>m) return r->rmq(x,y);
else if(y<=m) return l->rmq(x,y);
else return max(l->rmq(x,m),r->rmq(m+1,y));
}
} *seg;
struct fen {
ll fw[MAXN];
fen () { mmst(fw,0); }
void update(int x,int y,ll nval) {
pupdate(x,nval), pupdate(y+1,-nval);
}
void pupdate(int x,ll nval) {
++ x;
for(;x<MAXN;x+=x&(-x)) fw[x]+=nval;
}
ll sum(int x) { // point sum btw
++ x;
ll res=0; for(;x;x-=x&(-x)) res+=fw[x]; return res;
}
} fen;
struct node2 {
int s,e,m;
ll v,add;
node2 *l,*r;
node2(int S,int E){
s=S,e=E,m=(s+e)>>1;
v=-1; add=LLINF;
if(s^e){
l=new node2(s,m);
r=new node2(m+1,e);
}
}
void update(int x,int y,ll nval) {
value();
if(s==x&&e==y) {
add=nval;
return;
}
if(x>m) r->update(x,y,nval);
else if(y<=m) l->update(x,y,nval);
else l->update(x,m,nval),r->update(m+1,y,nval);
}
ll rmq(int x) {
value();
if(s==e) return v;
if(x>m) return r->rmq(x); else return l->rmq(x);
}
ll value() {
if(add==LLINF) return v;
v=add;
if(s^e)l->add=add,r->add=add;
add=LLINF;
return v;
}
} *link;
struct node3 {
int s,e,m;
ll v,add;
node3*l,*r;
node3(int S,int E) {
s=S,e=E,m=(s+e)>>1;
v=e-s+1; add=LLINF;
if(s^e) {
l=new node3(s,m);
r=new node3(m+1,e);
}
}
void update(int x,int y) {
value();
if(s==x&&e==y) {
add=0;
return;
}
if(x>m) r->update(x,y);
else if(y<=m) l->update(x,y);
else l->update(x,m),r->update(m+1,y);
v=l->value()+r->value();
}
ll value() {
if(add==LLINF) return v;
v=0;
if(s^e) l->add=add,r->add=add;
add=LLINF;
return v;
}
ll rmq(int x,int y) {
value();
if(s==x&&e==y) {
return v;
}
if(x>m) return r->rmq(x,y);
else if(y<=m) return l->rmq(x,y);
else return l->rmq(x,m)+r->rmq(m+1,y);
}
} *have;
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) {
n=N,c=C,r=R;
FOR(i,0,n-2)A[i]=K[i];
seg=new node(0,n-2);
have=new node3(0,n-1);
link=new node2(0,n-1);
FOR(i,0,c-1) {
ll s=S[i],e=E[i];
// ll co = 0; bool done=0; FOR(i,0,n-1) { co += have[i]; if(co==s+1) { s=i; done=1; break; } if(~link[i]){assert(have[link[i]]==0); i=link[i]-1; continue; } } assert(done);
// co=0; done=0; FOR(i,0,n-1) { co+=have[i]; if(~link[i]){assert(have[link[i]]==0); i=link[i]-1; continue; } if(co==e+1) { e=i; done=1; break; } } assert(done);
// binary search for first s, and e, for the e connect to link
ll st=-1, en=n-1, mid=0;
while(en-st>1) {
mid=(st+en)>>1;
if(have->rmq(0,mid) >= s+1) en=mid;
else st=mid;
}
s=en;
st=-1, en=n-1, mid=0;
while(en-st>1) {
mid=(st+en)>>1;
if(have->rmq(0,mid) >= e+1) en=mid;
else st=mid;
}
e=link->rmq(en)==-1?en:link->rmq(en);
assert(s+1<=e), have->update(s+1,e);
assert(s<=e-1), link->update(s,e-1,e);
tie(S[i],E[i])=tie(s,e);
}
FOR(i,0,c-1) {
if(seg->rmq(S[i], E[i]-1) < r) {
fen.update(S[i], E[i], 1);
}
}
ll ans=-1,pos=-1;
FOR(i,0,n-1) {
if(fen.sum(i)>ans)ans=fen.sum(i),pos=i;
} assert(~pos);
return pos;
}
/*
5 3 3
1 0 2 4
1 3
0 1
0 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |