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;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pii pair<long long,long long>
#define F first
#define S second
#define pb push_back
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
void setio(){
freopen("/Users/iantsai/cpp/input.txt","r",stdin);
freopen("/Users/iantsai/cpp/output.txt","w",stdout);
}
const long long mxn = (1<<17)+10;
const long long inf=1e18;
long long n, m, rk, l[mxn], r[mxn], nl[mxn], nr[mxn], a[mxn];
struct BIT{
long long bit[mxn];
void init(){
for(long long i=0;i<mxn;i++){
bit[i] = 0;
}
}
void m(long long p, long long v){
for(;p<mxn;p+=p&-p){
bit[p] += v;
}
}
long long q(long long p){
long long res = 0;
for(;p;p-=p&-p){
res += bit[p];
}
return res;
}
long long bs(long long x){
long long pos = 0;
//cout << x<<":\n";
for(long long i=16;i>=0;i--){
if((pos | (1 << i)) <= n and bit[pos|(1<<i)] < x){
pos |= (1 << i);
x -= bit[pos];
//cout<< bit[pos] << ','<< x <<','<<pos<<'\n';
}
}
//cout<<'\n';
return pos+1;
}
}bt;
struct segtree{
struct data{
long long l, r, cnt;
}seg[4*mxn];
void build(long long l,long long r,long long id){
seg[id]={inf,-inf,0};
if(l==r){
return ;
}
long long mm=l+r>>1;
build(l,mm,id*2);
build(mm+1,r,id*2+1);
}
void m(long long l,long long r,long long id,long long p,pii v){
if(l==r){
seg[id].l = v.F;
seg[id].r = v.S;
seg[id].cnt = 1;
if(v.F==inf) seg[id].cnt=0;
return ;
}
long long mm = l+r>>1;
if(p<=mm){
m(l,mm,id*2,p,v);
}
else{
m(mm+1,r,id*2+1,p,v);
}
seg[id].l=min(seg[id*2].l,seg[id*2+1].l);
seg[id].r=max(seg[id*2].r,seg[id*2+1].r);
seg[id].cnt=seg[id*2].cnt+seg[id*2+1].cnt;
}
data q(long long l,long long r,long long id,long long ql,long long qr){
if(qr<l or r<ql){
return {inf,-inf,0};
}
if(ql<=l and r<=qr){
return seg[id];
}
long long mm = l+r>>1;
data p =q(l,mm,id*2,ql,qr);
data p2=q(mm+1,r,id*2+1,ql,qr);
return {min(p.l,p2.l),max(p.r,p2.r),p.cnt+p2.cnt};
}
}tr;
vector<pair<long long,pii>>in[mxn], out[mxn];
long long nxt[mxn];
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) {
n = N, m = C, rk = R;
bt.init();
for(long long i=1;i<=m;i++){
l[i] = S[i-1]+1;
r[i] = E[i-1]+1;
}
for(long long i=1;i<=n;i++){
bt.m(i, 1);
}
for(int i=1;i<n;i++){
a[i] = K[i-1];
}
tr.build(1,m,1);
/*for(long long i=1;i<=m;i++){
for(long long j=r[i];j>=l[i];j--){
long long p = bt.bs(j);
if(j>l[i]){
bt.m(p, -1);
}
else{
nl[i] = p;
}
if(j == r[i]){
nr[i] = p;
}
}
in[nl[i]].pb({i,{nl[i],nr[i]}});
out[nr[i]].pb({i,{nl[i],nr[i]}});
//cout<< nl[i] << ',' << nr[i] << '\n';
}*/
Tree<pii> os;
rep(i,n) os.insert({i,i});
rep(i,m){
int l = S[i], r = E[i];
int times = r-l;
auto it = os.find_by_order(l);
auto pl = *it;
vector<pii> torem;
torem.pb(pl);
while(times--){
it++;
torem.pb(*it);
}
auto pr = *it;
trav(px,torem) os.erase(px);
os.insert({pl.F,pr.S});
S[i] = pl.F, E[i] = pr.S;
}
for(int i=0;i<m;i++){
in[S[i]].pb({i,{nl[i],nr[i]}});
out[E[i]].pb({i,{nl[i],nr[i]}});
}
nxt[n] = n+1;
for(long long i=n-1;i>=1;i--){
if(a[i]>rk){
nxt[i] = i+1;
}
else{
nxt[i] = nxt[i+1];
}
}
//tr.m(0,{inf,-inf},m);
long long prev = -1, mx = -1, pos = 1;
for(long long i=1;i<=n;i++){
//cout << prev << ',' << nxt[i] << '\n';b
for(auto [id, p]:in[i]){
tr.m(1,m,1,id,p);
}
long long l=1,r=m,ck=0;
while(l<r){
long long mm=l+r+1>>1;
auto [ql,qr,cnt] = tr.q(1,m,1,1,mm);
if(ql<=prev or qr>=nxt[i]){
r=mm-1;
}
else{
l=mm;
}
}
long long cnt = tr.q(1,m,1,1,l).cnt;
//cout << l << ',' << cnt << '\n';
if(cnt>mx){
mx=cnt;
pos=i;
}
for(auto [id, p] : out[i]){
tr.m(1, m, 1, id, {inf,-inf});
}
if(a[i]>rk)prev=i;
}
return pos-1;
}
#ifdef local
int main() {
setio();
long long tmp;
int N, C, R;
int *K, *S, *E;
tmp = scanf("%d %d %d", &N, &C, &R);
assert(tmp == 3);
K = (int*) malloc((N-1) * sizeof(int));
S = (int*) malloc(C * sizeof(int));
E = (int*) malloc(C * sizeof(int));
int i;
for (i = 0; i < N-1; i++) {
tmp = scanf("%d", &K[i]);
assert(tmp == 1);
}
for (i = 0; i < C; i++) {
tmp = scanf("%d %d", &S[i], &E[i]);
}
printf("%d\n", GetBestPosition(N, C, R, K, S, E));
return 0;
}
#endif
Compilation message (stderr)
tournament.cpp: In member function 'void segtree::build(long long int, long long int, long long int)':
tournament.cpp:69:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
69 | long long mm=l+r>>1;
| ~^~
tournament.cpp: In member function 'void segtree::m(long long int, long long int, long long int, long long int, std::pair<long long int, long long int>)':
tournament.cpp:81:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | long long mm = l+r>>1;
| ~^~
tournament.cpp: In member function 'segtree::data segtree::q(long long int, long long int, long long int, long long int, long long int)':
tournament.cpp:99:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
99 | long long mm = l+r>>1;
| ~^~
tournament.cpp: In function 'int GetBestPosition(int, int, int, int*, int*, int*)':
tournament.cpp:181:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
181 | long long mm=l+r+1>>1;
| ~~~^~
tournament.cpp:179:27: warning: unused variable 'ck' [-Wunused-variable]
179 | long long l=1,r=m,ck=0;
| ^~
tournament.cpp: In function 'void setio()':
tournament.cpp:21:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen("/Users/iantsai/cpp/input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tournament.cpp:22:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen("/Users/iantsai/cpp/output.txt","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... |