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 <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
class segment_tree{
public:
vector<int> tree, alive;
int N;
segment_tree(int K){
N = 1;
while(N <= K){
N *= 2;
}
tree.assign(N * 2, 0);
alive.assign(N * 2, 0);
}
void add_alive(int head, int l, int r, int pos, int val){
if(r - l == 1){
alive[head] = val;
return;
}
int mid = (l + r) / 2;
if(pos < mid){
add_alive(head * 2 + 1, l, mid, pos, val);
}
else{
add_alive(head * 2 + 2, mid, r, pos, val);
}
alive[head] = alive[head * 2 + 1] + alive[head * 2 + 2];
}
void add_alive(int pos, int val){
add_alive(0, 0, N, pos, val);
}
int get_alive(int head, int l, int r, int need, int left){
if(r - l == 1){
return l;
}
int mid = (l + r) / 2;
if(need <= alive[head * 2 + 1] + left){
return get_alive(head * 2 + 1, l, mid, need, left);
}
else{
return get_alive(head * 2 + 2, mid, r, need, left + alive[head * 2 + 1]);
}
}
int get_alive(int need){
return get_alive(0, 0, N, need, 0);
}
void add_val(int head, int l, int r, int pos, int val){
if(r - l == 1){
tree[head] = val;
return;
}
int mid = (l + r) / 2;
if(pos < mid){
add_val(head * 2 + 1, l, mid, pos, val);
}
else{
add_val(head * 2 + 2, mid, r, pos, val);
}
tree[head] = max(tree[head * 2 + 1], tree[head * 2 + 2]);
}
void add_val(int pos, int val){
add_val(0, 0, N, pos, val);
}
int get_val(int head, int l, int r, int L, int R){
if(L <= l && R >= r){
return tree[head];
}
if(L >= r || R <= l){
return 0;
}
int mid = (l + r) / 2;
return max(get_val(head * 2 + 1, l, mid, L, R), get_val(head * 2 + 2, mid, r, L, R));
}
int get_val(int l, int r){
return get_val(0, 0, N, l, r);
}
};
int GetBestPosition(int N, int C, int R, int K[], int S[], int E[]){
segment_tree tree(N);
vector<int> pref(N + 5);
int ans = 0;
int id = 0;
vector<int> arr(N-1);
for(int i = 0; i < N-1; i ++){
arr[i] = K[i];
}
vector<int> brr = arr;
fill(pref.begin(), pref.end(), 0);
for(int j = 0; j < N-1; j ++){
tree.add_val(j, brr[j]);
tree.add_alive(j, 1);
}
tree.add_alive(N, 1);
tree.add_alive(N-1, 1);
for(int j = 0; j < C; j ++){
int s = tree.get_alive(S[j] + 1), e = tree.get_alive(E[j] + 2), b = tree.get_alive(E[j] + 1);
if(tree.get_val(s, b) < R){
pref[s] ++;
pref[e]--;
}
for(int k = S[j]; k < E[j]; k ++){
tree.add_alive(tree.get_alive(S[j] + 2), 0);
}
}
int p = 0;
for(int j = 0; j < N; j ++){
p += pref[j];
if(ans < p){
ans = p;
id = j;
}
}
return id;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |