# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
997344 | saddd | Train (APIO24_train) | C++17 | 0 ms | 0 KiB |
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>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1000003, pr = 31;
const int mxn = 2e5 + 5, mxq = 1e5 + 5, sq = 500, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e17 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
//#include "train_apio24.h"
#include <vector>
struct CC{
vt<int>comp;
void init(bool sign ){
sort(ALL(comp));
if(sign){
comp.resize(unique(ALL(comp)) - comp.begin());
}
}
int findl(int x){
return(lower_bound(ALL(comp), x) - comp.begin() + 1);
}
int findr(int x){
return(upper_bound(ALL(comp), x) - comp.begin() + 1);
}
void add(int x){
comp.pb(x);
}
};
CC cmpl, cmpr;
struct Node{
Node *lson, *rson;
int sm = 0;
};
Node *root[mxn + 1];
struct E{
ll x, y, a, b, c;
bool operator <(const E &other){
return(a < other.a);
}
};
int n, m, w;
vt<E>edge;
vt<pll>food;
vt<int>comp;
deque<pii>mn[mxn + 1];
bool cmp(int a, int b){
return(edge[a].b < edge[b].b);
}
ll t[mxn + 1], dp[mxn + 1];
void build(Node *nd, int l, int r){
if(l == r)return;
int mid = (l + r) >> 1;
nd->lson = new Node(); nd->rson = new Node();
build(nd->lson, l, mid); build(nd->rson, mid + 1, r);
}
void upd(Node *nd, Node *pre, int l, int r, int id){
if(l == r){
nd->sm = pre->sm + 1;
return;
}
int mid = (l + r) >> 1;
if(id <= mid){
nd->lson = new Node(); nd->rson = pre->rson;
upd(nd->lson, pre->lson, l, mid, id);
}else{
nd->lson = pre->lson;
nd->rson = new Node(); upd(nd->rson, pre->rson, mid + 1, r, id);
}
nd->sm = nd->lson->sm + nd->rson->sm;
}
int get(Node *nd, Node *pre, int l, int r, int ql, int qr){
if(ql > r || qr < l)return(0);
if(ql <= l && qr >= r)return(nd->sm - pre->sm);
int mid = (l + r) >> 1;
return(max(0, get(nd->lson, pre->lson, l, mid, ql, qr) + get(nd->rson, pre->rson, mid + 1, r, ql, qr)));
}
ll getval(int ide, int rp){
int lx = cmpl.findr(edge[ide].b) - 1, rx = cmpl.findr(cmpr.comp[rp - 1]) - 1;
int ly = cmpr.findr(edge[ide].b), ry = rp;
//cout << lx << " " << rx << " " << ly << " " << ry << "\n";
if(lx > rx || ly > ry)return(dp[ide]);
ll ans = dp[ide] + get(root[rx], root[lx], 1, sz(cmpr.comp), ly, ry) * t[edge[ide].y];
//cout << ans << " ";
return(ans);
}
ll solve(){
sort(ALL(edge)); sort(ALL(comp), cmp); sort(ALL(food));
cmpr.add(0);
for(int i = 0; i < sz(food); i++){
cmpl.add(food[i].fi); cmpr.add(food[i].se);
}
cmpl.init(0); cmpr.init(1);
root[0] = new Node();
build(root[0], 1, sz(cmpr.comp));
int rp = 0;
for(int i = 1; i <= sz(food); i++){
root[i] = new Node();
upd(root[i], root[i - 1], 1, sz(cmpr.comp), cmpr.findl(food[i - 1].se));
}
rp = 0;
ll ans = inf;
mn[0].pb(mpp(m, 1));
for(int i = 0; i < m; i++){
while(rp < sz(comp) && edge[comp[rp]].b <= edge[i].a){
if(dp[comp[rp]] == inf){
rp++; continue;
}
auto [x, y, a, b, c] = edge[comp[rp]];
while(sz(mn[y])){
auto [ide, idr] = mn[y].back();
if(getval(ide, idr) >= getval(comp[rp], idr)){
mn[y].pop_back();
}else{
break;
}
}
if(!sz(mn[y])){
mn[y].pb(mpp(comp[rp], 1));
}else{
auto [ide, idr] = mn[y].back();
int l = idr + 1, r = sz(cmpr.comp), ans = -1;
if(l > r || getval(comp[rp], r) > getval(ide, r)){
continue;
}
ans = r--;
while(l <= r){
int mid = (l + r) >> 1;
if(getval(comp[rp], mid) <= getval(ide, mid)){
ans = mid; r = mid - 1;
}else{
l = mid + 1;
}
}
if(ans != -1)mn[y].pb(mpp(comp[rp], ans));
}
rp++;
}
auto [x, y, a, b, c] = edge[i];
int id = lower_bound(ALL(food), mpp(a, 1LL * -1)) - food.begin() - 1;
if(sz(mn[x]) == 0)dp[i] = inf;
else{
int idr = cmpr.findl(a) - 1;
while(sz(mn[x]) >= 2 && mn[x][1].se <= idr)mn[x].pop_front();
dp[i] = getval(mn[x].front().fi, idr) + c;
//cout << c << " ";
}
if(y == n - 1){
int idl = cmpl.findr(edge[i].b) - 1, idr = cmpr.findr(edge[i].b);
ll cnt = get(root[sz(cmpl.comp)], root[idl], 1, sz(cmpr.comp), idr, sz(cmpr.comp));
ans = min(ans, dp[i] + cnt * t[n - 1]);
}
//cout << x << " " << y << " " << a << " " << b << " " << c << " " << dp[i] << "\n";
}
if(ans == inf)ans = -1;
return(ans);
}
long long solve(int N, int M, int W, std::vector<int> T, std::vector<int> X, std::vector<int> Y,
std::vector<int> A, std::vector<int> B, std::vector<int> C, std::vector<int> L,
std::vector<int> R) {
edge.clear(); food.clear(); comp.clear();
n = N; m = M; w = W;
for(int i = 0; i < n; i++)t[i] = T[i];
for(int i = 0; i < m; i++){
edge.pb({X[i], Y[i], A[i], B[i], C[i]}); comp.pb(i);
}
for(int i = 0; i < w; i++){
food.pb(mpp(L[i], R[i]));
}
return(solve());
}
#include <cassert>
#include <cstdio>
#include <vector>
signed main() {
int N, M, W;
assert(3 == scanf("%d %d %d", &N, &M, &W));
std::vector<int> t(N);
std::vector<int> x(M);
std::vector<int> y(M);
std::vector<int> a(M);
std::vector<int> b(M);
std::vector<int> c(M);
std::vector<int> l(W);
std::vector<int> r(W);
for (int i = 0; i < N; i++)
assert(1 == scanf("%d", &t[i]));
for (int i = 0; i < M; i++)
assert(5 == scanf("%d %d %d %d %d", &x[i], &y[i], &a[i], &b[i], &c[i]));
for (int i = 0; i < W; i++)
assert(2 == scanf("%d %d", &l[i], &r[i]));
printf("%lld", solve(N, M, W, t, x, y, a, b, c, l, r));
}