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<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<cassert>
#include<set>
using namespace std;
typedef long long ll;
ll inf = 8e18;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
ll d, a, b;
cin >> d >> a >> b;
vector<pair<ll, ll>> vec, free;
for(int i = 0; i < n; i++){
ll l, r;
cin >> l >> r;
vec.emplace_back(l, r);
if(i == 0) free.emplace_back(0, l - 1);
else free.emplace_back(vec[i - 1].second + 1, l - 1);
}
free.emplace_back(vec[n - 1].second + 1, inf);
set<pair<ll, ll>> s, sq;
for(auto &[l, r]: free){
ll fst = r + 1;
if(l == 0) fst = 0;
else{
auto iter = s.lower_bound(make_pair(l, -1));
if(iter != s.end()){
if((*iter).first <= r) fst = (*iter).first;
}
if(iter != s.begin()){
iter = prev(iter);
if((*iter).second >= l) fst = l;
}
}
if(fst <= r) s.emplace(fst + d, r + d), sq.emplace(fst, r);
}
vector<ll> ans(q, -1);
for(int i = 0; i < q; i++){
ll x;
cin >> x;
ll ori = x;
auto iter = sq.lower_bound(make_pair(x + 1, 0));
if(iter == sq.begin()){
cout << "-1\n";
continue;
}
iter = prev(iter);
if((*iter).second < x){
cout << "-1\n";
continue;
}
if(d * a <= b){
ll cnt = 0;
while(x > 0){
iter = prev(sq.lower_bound(make_pair(x + 1, 0)));
if((*iter).first == 0){
break;
}
x = (*iter).first - d;
cnt++;
}
cout << (ori - cnt * d) * a + cnt * b;
}
else{
ll cnt = 0;
while(x >= d){
iter = prev(sq.lower_bound(make_pair(x + 1, 0)));
//cout << (*iter).first << " " << (*iter).second << "\n";
cnt += (x - (*iter).first) / d;
x = (*iter).first + (x - (*iter).first) % d;
x -= d;
iter = prev(sq.lower_bound(make_pair(x + 1, 0)));
if((*iter).second < x) x = (*iter).second;
cnt++;
//cout << cnt << "\n";
//cout << x << "\n";
}
cout << (ori - cnt * d) * a + cnt * b << "\n";
//cout << cnt << "\n";
}
}
/*for(auto &[l, r]: free){
cout << l << " " << r << "\n";
}*/
}
// g++ -std=c++17 -Wall -Wextra -Wshadow -fsanitize=undefined -fsanitize=address -o run tower.cpp
# | 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... |