This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef LOCAL
#include "overtaking.h"
#endif // LOCAL
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#include <vector>
void init(int L, int N, std::vector<long long> T, std::vector<int> W, int X, int M, std::vector<int> S);
long long arrival_time(long long Y);
#endif // LOCAL
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int time_infty = 1e18 + 7;
const int nmax = 1000 + 24;
const int nmax_bs_step = (1 << 10);
struct bus {
int start_time;
int speed;
};
vector<bus> glob_busses;
vector<vector<bus>> busses_by_station;
vector<vector<int>> endtime_of_bus_by_station;
vector<int> stations;
int X_init;
void precompute_strips() {
// pentru fiecare statie, retin autobuzele ca perechi de (timp, viteza)
auto busses = glob_busses;
sort(busses.begin(), busses.end(), [](bus a, bus b) {
if(a.start_time != b.start_time)
return a.start_time < b.start_time;
return a.speed < b.speed;
});
busses_by_station.push_back(busses); // pentru statia 0
for(int i = 0; i + 1 < stations.size(); i++) {
int distance = stations[i + 1] - stations[i];
int max_prefix = -1e9;
vector<bus> new_busses;
vector<int> new_endtime_of_bus;
for(auto b : busses) {
max_prefix = max(max_prefix, b.start_time + distance * b.speed);
new_busses.push_back({max_prefix, b.speed});
new_endtime_of_bus.push_back(max_prefix);
}
endtime_of_bus_by_station.push_back(new_endtime_of_bus);
busses = new_busses;
sort(busses.begin(), busses.end(), [](bus a, bus b) {
if(a.start_time != b.start_time)
return a.start_time < b.start_time;
return a.speed < b.speed;
});
busses_by_station.push_back(busses);
}
}
struct intervals {
int start, end;
bool operator<(const intervals &other) const {
if(start != other.start) return start < other.start;
return end < other.end;
}
bool operator==(const intervals &other) const {
return start == other.start && end == other.end;
}
};
// daca eu ma aflu la timpul Y si Y intre [start, end], atunci
// eu voi termina la timpul end la final
set<intervals> solution_set;
long long arrival_time_fast(long long Y) {
// care doar da query in structura curenta a solution_set-ului
// si returneaza timpul de sosire
// cautam in solution_set intervalul care contine Y
auto it = solution_set.lower_bound({Y, time_infty});
if(it == solution_set.begin()) {
// nu exista niciun interval care sa contina Y
return Y;
}
auto interval = *prev(it);
if(Y < interval.start || Y > interval.end) {
// nu exista niciun interval care sa contina Y
return Y;
}
return interval.end;
}
long long query_strip(int strip_index, long long Y) {
// strip-ul se intinde de la stations[strip_index] la stations[strip_index + 1]
// cautam in busses_by_station[strip_index] autobuzul care ma tine in spate
// facem o cautare binara pentru ultimul autobuz care (conform comparatorului custom) este
// mai mic decat mine
const auto& busses = busses_by_station[strip_index]; // O(1)
const auto& endtime_of_bus = endtime_of_bus_by_station[strip_index]; // O(1)
int ok = -1;
for(int step = nmax_bs_step; step >= 1; step /= 2) {
while(ok + step < busses.size() && busses[ok + step].start_time < Y) {
ok += step;
}
}
if(ok == -1) {
// nu exista niciun autobuz care sa ma tina in spate
return Y;
}
if(endtime_of_bus[ok] < Y) {
// nu exista niciun autobuz care sa ma tina in spate
return Y;
}
return endtime_of_bus[ok];
}
void add_strip(int strip_index) {
// adaugam un nou strip in solution_set
// practic la final sa dam query(Y) <=> Y' = query_strip(strip_index, Y) si apoi Y'' = arrival_time_fast(Y')
set<intervals> new_solution_set;
}
#undef int
void init(int L, int N, std::vector<long long> T, std::vector<int> W, int X, int M, std::vector<int> S) {
#define int long long
// scad X (viteza autobuzului de query) din fiecare viteza de autobuz (W)
for(int i = 0; i < N; ++i) {
W[i] -= X;
}
X_init = X;
// initialize busses
for(int i = 0; i < N; ++i) {
bus b;
b.start_time = T[i];
b.speed = W[i];
if(W[i] <= 0) continue; // acest autobuz nu afecteaza autobuzul meu!
glob_busses.push_back(b);
}
// initialize stations
for(int i = 0; i < M; ++i) {
stations.push_back(S[i]);
}
precompute_strips();
}
long long arrival_time_brut(long long Y) {
auto busses = glob_busses;
busses.push_back({Y, 0}); // autobuzul meu incepe la timpul Y si are viteza 0
sort(busses.begin(), busses.end(), [](bus a, bus b) {
if(a.start_time != b.start_time)
return a.start_time < b.start_time;
return a.speed < b.speed;
});
for(int i = 0; i + 1 < stations.size(); i++) {
int distance = stations[i + 1] - stations[i];
int max_prefix = -1e9;
vector<bus> new_busses;
for(auto b : busses) {
max_prefix = max(max_prefix, b.start_time + distance * b.speed);
new_busses.push_back({max_prefix, b.speed});
}
busses = new_busses;
sort(busses.begin(), busses.end(), [](bus a, bus b) {
if(a.start_time != b.start_time)
return a.start_time < b.start_time;
return a.speed < b.speed;
});
}
for(auto b : busses) {
if(b.speed == 0) return b.start_time;
}
assert(false); // nu se poate ajunge aici
}
long long arrival_time_slow(long long Y) {
// repeatedly use query_strip()
for(int strip_index = 0; strip_index + 1 < stations.size(); strip_index++) {
Y = query_strip(strip_index, Y);
}
return Y;
}
long long arrival_time(long long Y) {
long long answer = arrival_time_slow(Y);
#ifdef LOCAL
assert (answer == arrival_time_brut(Y));
#endif // LOCAL
return answer + 1LL * (stations.back() - stations[0]) * X_init;
}
#ifdef LOCAL
#undef int
// Grader
#include <cassert>
#include <cstdio>
#include <vector>
int main()
{
int L, N, X, M, Q;
assert(5 == scanf("%d %d %d %d %d", &L, &N, &X, &M, &Q));
std::vector<long long> T(N);
for (int i = 0; i < N; i++)
assert(1 == scanf("%lld", &T[i]));
std::vector<int> W(N);
for (int i = 0; i < N; i++)
assert(1 == scanf("%d", &W[i]));
std::vector<int> S(M);
for (int i = 0; i < M; i++)
assert(1 == scanf("%d", &S[i]));
std::vector<long long> Y(Q);
for (int i = 0; i < Q; i++)
assert(1 == scanf("%lld", &Y[i]));
fclose(stdin);
init(L, N, T, W, X, M, S);
std::vector<long long> res(Q);
for (int i = 0; i < Q; i++)
res[i] = arrival_time(Y[i]);
for (int i = 0; i < Q; i++)
printf("%lld\n", res[i]);
fclose(stdout);
return 0;
}
#endif // LOCAL
Compilation message (stderr)
overtaking.cpp: In function 'void precompute_strips()':
overtaking.cpp:45:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0; i + 1 < stations.size(); i++) {
| ~~~~~~^~~~~~~~~~~~~~~~~
overtaking.cpp: In function 'long long int query_strip(long long int, long long int)':
overtaking.cpp:124:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<bus>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | while(ok + step < busses.size() && busses[ok + step].start_time < Y) {
| ~~~~~~~~~~^~~~~~~~~~~~~~~
overtaking.cpp: In function 'long long int arrival_time_brut(long long int)':
overtaking.cpp:187:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
187 | for(int i = 0; i + 1 < stations.size(); i++) {
| ~~~~~~^~~~~~~~~~~~~~~~~
overtaking.cpp: In function 'long long int arrival_time_slow(long long int)':
overtaking.cpp:217:43: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
217 | for(int strip_index = 0; strip_index + 1 < stations.size(); strip_index++) {
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |