This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//by szh
#include<bits/stdc++.h>
using namespace std;
/*
#define inbuf_len 1 << 16
#define outbuf_len 1 << 16
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E);
int main() {
int tmp;
// Set input and output buffering
char *inbuf, *outbuf;
inbuf = (char*) malloc(inbuf_len * sizeof(char));
outbuf = (char*) malloc(outbuf_len * sizeof(char));
tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
assert(tmp == 0);
tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);
assert(tmp == 0);
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]);
assert(tmp == 2);
}
printf("%d\n", GetBestPosition(N, C, R, K, S, E));
return 0;
}
*/
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 1e5+10;
int bit[maxn];
vector <pii> range;
int lst[maxn],nxt[maxn];
void update(int pos,int val) {
while (pos<maxn) {
bit[pos]+=val;
pos+=lowbit(pos);
}
}
int query(int pos) {
int ret=0;
while (pos) {
ret+=bit[pos];
pos-=lowbit(pos);
}
return ret;
}
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) {
rep(i,1,N+2) update(i,1),nxt[i] = i+1;
rep(i,0,C) {
S[i]++,E[i]++;
int l = 1,r = N;
while (l<r) {
int mid=l+r>>1;
if (query(mid)<S[i]) l = mid+1;
else r = mid;
}
int cur = nxt[l];
while (S[i]<E[i]) {
update(cur,-1);
cur = nxt[cur];
S[i]++;
}
nxt[l] = cur;
range.pb({l,cur});
// cout<<l<<" "<<cur<<endl;
}
sort(ALL(range));
int tmp = -1;
rep(i,0,N-1) {
lst[i+1] = tmp;
if (K[i]>R) tmp=i+1;
}
lst[N] = tmp;
tmp = N+1;
for (int i=N-2;i>=0;i--) {
nxt[i+2] = tmp;
if (K[i]>R) tmp=i+2;
}
nxt[1] = tmp;
int bg=0,ed=0;
memset(bit,0,sizeof(bit));
int cnt = -1,ans = 0;
rep(i,1,N+1) {
while (ed<SZ(range) and range[ed].fi<=i) update(range[ed].se,1),ed++;
while (bg<ed and range[bg].fi<=lst[i]) update(range[bg].se,-1),bg++;
int cur = query(nxt[i])-query(i);
if (cur>cnt) cnt=cur,ans = i;
debug(cur);
}
return ans-1;
}
/*
5 3 3
1
0
2
4
1 3
0 1
0 1
*/
Compilation message (stderr)
tournament.cpp: In function 'int GetBestPosition(int, int, int, int*, int*, int*)':
tournament.cpp:96:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
96 | int mid=l+r>>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... |