This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 3e5 + 5;
const double eps = 1e-9;
struct node
{
int val[5][5];
};
node seg[MAXN];
int K;
vii adj[MAXN];
void merge(node &c, node &lC, node &rC, int a1, int a2, int a3) {
for (int i = 0; i < K; i++) {
for (int j = 0; j < K; j++) {
c.val[i][j] = INF;
for (int k = 0; k < K; k++) {
if (a1 == a2) k = i;
for (pii nx: adj[a2 * K + k]) {
if (a2 + 1 == a3 && j != nx.fi % K) continue;
c.val[i][j] = min(c.val[i][j], lC.val[i][k] + rC.val[nx.fi % K][j] + nx.se);
}
if (a1 == a2) break;
}
}
}
}
void build(int l, int r, int k) {
if (l == r) {
memset(seg[k].val, 0, sizeof seg[k].val);
return;
}
int m = (l + r) / 2;
build(l, m, k*2);
build(m + 1, r, k*2 + 1);
merge(seg[k], seg[k*2], seg[k*2 + 1], l, m, r);
}
node qry(int l, int r, int k, int a, int b) {
if (a <= l && r <= b) return seg[k];
int m = (l + r) / 2;
node ret;
if (a <= m && m < b) {
node lC = qry(l, m, k*2, a, b);
node rC = qry(m + 1, r, k*2 + 1, a, b);
merge(ret, lC, rC, max(l, a), m, min(b, r));
} else if (a <= m)
return qry(l, m, k*2, a, b);
else
return qry(m + 1, r, k*2 + 1, a, b);
return ret;
}
int main() {
int N, M, O;
scanf("%d %d %d %d", &K, &N, &M, &O);
for (int i = 0; i < M; i++) {
int a, b, t;
scanf("%d %d %d", &a, &b, &t);
adj[a].pb({b, t});
}
build(0, (N - 1) / K, 1);
for (int i = 0; i < O; i++) {
int a, b; scanf("%d %d", &a, &b);
if (a / K >= b / K) {
printf("-1\n");
continue;
}
node ans = qry(0, (N - 1) / K, 1, a / K, b / K);
int val = ans.val[a % K][b % K];
if (val == INF) val = -1;
printf("%d\n", val);
}
}
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:89:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d", &K, &N, &M, &O);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:92:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &a, &b, &t);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:97:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a, b; scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# | 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... |