#include <bits//stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_multiset;
#define ll long long
#define ld long double
#define iloop(m, h) for (ll i = m; i != h; i += (m < h ? 1 : -1))
#define jloop(m, h) for (auto j = m; j != h; j += (m < h ? 1 : -1))
#define kloop(m, h) for (auto k = m; k != h; k += (m < h ? 1 : -1))
#define lloop(m, h) for (auto l = m; l != h; l += (m < h ? 1 : -1))
#define iloop_(m, h, g) for (auto i = m; i < h; i += g)
#define jloop_(m, h, g) for (auto j = m; j < h; j += g)
#define kloop_(m, h, g) for (auto k = m; k < h; k += g)
#define lloop_(m, h, g) for (auto l = m; l < h; l += g)
#define getchar_unlocked _getchar_nolock // comment before submission
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define pllll pair<pll, pll>
#define vll vector<ll>
#define qll queue<ll>
#define dll deque<ll>
#define pqll priority_queue<ll>
#define gll greater<ll>
#define INF 1000000000000000
#define MOD1 1000000007
#define MOD2 998244353
#define MOD3 1000000009
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
vector<pll> adj[50005];
ll n, m, t, q;
struct sv {
ll a[5][5];
sv (ll tl = -1, ll tu = -1) {
iloop(0, 5) jloop(0, 5) a[i][j] = ((i == j && tl <= i && i <= tu) ? 0 : INF);
}
};
sv merge(sv x, sv y, ll l) {
sv z = sv();
iloop(0, 5) for (auto it : adj[l*t + i]) jloop(0, 5) kloop(0, 5) z.a[j][k] = min(z.a[j][k], x.a[j][i] + it.first + y.a[it.second - (l+1)*t][k]);
return z;
}
struct node {
ll s, e, m;
sv v = sv();
node *l, *r;
node (ll S, ll E) {
s = S, e = E, m = (S+E)>>1;
if (s == e) {
v = sv(0, min(n - (s*t), 5LL));
//cout << "nd " << s << " " << e << ":\n";
//iloop(0, 5) {jloop(0, 5) {cout << v.a[i][j] << " ";} cout << "\n";}
return;
}
l = new node(s, m);
r = new node(m+1, e);
v = merge(l->v, r->v, m);
//cout << "nd " << s << " " << e << ":\n";
//iloop(0, 5) {jloop(0, 5) {cout << v.a[i][j] << " ";} cout << "\n";}
}
sv query(ll S, ll E) {
if (s == S && e == E) return v;
if (E <= m) return l->query(S, E);
if (S > m) return r->query(S, E);
return merge(l->query(S, m), r->query(m+1, E), m);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> t >> n >> m >> q;
ll t1, t2, t3;
iloop(0, m) {
cin >> t1 >> t2 >> t3;
adj[t1].push_back({t3, t2});
}
node root = node(0, (n-1)/t);
while (q--) {
cin >> t1 >> t2;
ll tmp = root.query(t1/t, t2/t).a[t1%t][t2%t];
cout << (tmp == INF ? -1 : tmp) << "\n";
}
}
| # | 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... |