#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#ifdef _DEBUG
#define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el
#else
#define dout(x)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define uid(a,b) uniform_int_distribution<int>(a,b)(rng)
#define ins insert
#define ssize(x) (int((x).size()))
#define bs(args...) binary_search(args)
#define lb(args...) lower_bound(args)
#define ub(args...) upper_bound(args)
#define all(x) (x).begin(),(x).end()
#define mp(a, b) make_pair(a, b)
#define mt(args...) make_tuple(args)
#define pb(x) push_back(x)
#define eb(args...) emplace_back(args)
#define ff first
#define ss second
#define die exit(0)
template<typename T>
using vc = vector<T>;
template<typename T>
using uset = unordered_set<T>;
template<typename A, typename B>
using umap = unordered_map<A, B>;
template<typename T, typename Comp>
using pq = std::priority_queue<T, vc<T>, Comp>;
template<typename T>
using maxpq = pq<T, less<T>>;
template<typename T>
using minpq = pq<T, greater<T>>;
template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using db = double;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vc<int>;
using vll = vc<ll>;
using vpi = vc<pi>;
using vpll = vc<pll>;
using str = string;
constexpr char el = '\n';
constexpr char sp = ' ';
constexpr int inf = 0x3f3f3f3f;
constexpr ll llinf = 0x3f3f3f3f3f3f3f3fLL;
// ---------------------------------------------------------------------
const int LG = 16;
const int N = 5e4+5;
const int K = 6;
int n, m, k, o, up[LG][N][K], d[2][K];
int dis(int x, int y){
memset(d[0], 0x3f, sizeof d[0]);
d[0][y%k] = 0;
int s = y/k, u = y/k-x/k;
for(int p = 0; u > 0; ++p, u /= 2){
if(u&1^1)
continue;
for(int i = 0; i < k; ++i){
d[1][i] = inf;
for(int j = 0; j < k; ++j)
d[1][i] = min(d[1][i], up[p][s*k+j][i] + d[0][j]);
}
s -= 1<<p;
swap(d[0], d[1]);
}
return d[0][x%k];
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cout << fixed; clog << fixed; clog << unitbuf;
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("debug.txt", "w", stderr);
#else
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
#endif
memset(up, 0x3f, sizeof up);
cin >> k >> n >> m >> o;
while(m--){
int x, y, w;
cin >> x >> y >> w;
up[0][y][x%k] = min(up[0][y][x%k], w);
}
for(int g = 1; g < LG; ++g)
for(int v = 0; v < n; ++v)
for(int i = 0; i < k; ++i)
for(int j = 0; j < k; ++j)
if((v/k-(1<<(g-1)))*k+j>=0)
up[g][v][i] = min(up[g][v][i],
up[g-1][(v/k-(1<<(g-1)))*k+j][i] +
up[g-1][v][j]);
#ifdef _DEBUG
clog << "Line " << __LINE__ << ":\n";
for(int i = 0; i < n; ++i){
for(int j = 0; j < k; ++j)
clog << up[2][i][j] << sp;
clog << el;
}
clog << el;
#endif
while(o--){
int x, y;
cin >> x >> y;
if(x/k >= y/k){
dout(1);
cout << -1 << el;
continue;
}
int ans = dis(x, y);
if(ans>=inf)
cout << -1 << el;
else
cout << ans << el;
}
}
Compilation message
toll.cpp: In function 'int dis(int, int)':
toll.cpp:73:13: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
73 | if(u&1^1)
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
20076 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
8 ms |
19100 KB |
Output is correct |
4 |
Correct |
8 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19020 KB |
Output is correct |
6 |
Correct |
9 ms |
19020 KB |
Output is correct |
7 |
Correct |
9 ms |
19228 KB |
Output is correct |
8 |
Correct |
33 ms |
19972 KB |
Output is correct |
9 |
Correct |
31 ms |
19976 KB |
Output is correct |
10 |
Correct |
22 ms |
19276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
20700 KB |
Output is correct |
2 |
Correct |
9 ms |
19020 KB |
Output is correct |
3 |
Correct |
8 ms |
19020 KB |
Output is correct |
4 |
Correct |
8 ms |
19088 KB |
Output is correct |
5 |
Correct |
8 ms |
19020 KB |
Output is correct |
6 |
Correct |
8 ms |
19112 KB |
Output is correct |
7 |
Correct |
11 ms |
19148 KB |
Output is correct |
8 |
Correct |
12 ms |
19152 KB |
Output is correct |
9 |
Correct |
29 ms |
19972 KB |
Output is correct |
10 |
Correct |
57 ms |
21388 KB |
Output is correct |
11 |
Correct |
47 ms |
20776 KB |
Output is correct |
12 |
Correct |
42 ms |
20372 KB |
Output is correct |
13 |
Correct |
62 ms |
21484 KB |
Output is correct |
14 |
Correct |
40 ms |
20552 KB |
Output is correct |
15 |
Correct |
46 ms |
20292 KB |
Output is correct |
16 |
Correct |
47 ms |
20236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
18984 KB |
Output is correct |
2 |
Correct |
8 ms |
19108 KB |
Output is correct |
3 |
Correct |
9 ms |
19096 KB |
Output is correct |
4 |
Correct |
8 ms |
19040 KB |
Output is correct |
5 |
Correct |
8 ms |
19108 KB |
Output is correct |
6 |
Correct |
10 ms |
19020 KB |
Output is correct |
7 |
Correct |
9 ms |
19020 KB |
Output is correct |
8 |
Correct |
10 ms |
19116 KB |
Output is correct |
9 |
Correct |
13 ms |
19136 KB |
Output is correct |
10 |
Correct |
26 ms |
19840 KB |
Output is correct |
11 |
Correct |
39 ms |
20548 KB |
Output is correct |
12 |
Correct |
51 ms |
21260 KB |
Output is correct |
13 |
Correct |
54 ms |
21504 KB |
Output is correct |
14 |
Correct |
46 ms |
20804 KB |
Output is correct |
15 |
Correct |
43 ms |
20172 KB |
Output is correct |
16 |
Correct |
43 ms |
20164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
18984 KB |
Output is correct |
2 |
Correct |
8 ms |
19108 KB |
Output is correct |
3 |
Correct |
9 ms |
19096 KB |
Output is correct |
4 |
Correct |
8 ms |
19040 KB |
Output is correct |
5 |
Correct |
8 ms |
19108 KB |
Output is correct |
6 |
Correct |
10 ms |
19020 KB |
Output is correct |
7 |
Correct |
9 ms |
19020 KB |
Output is correct |
8 |
Correct |
10 ms |
19116 KB |
Output is correct |
9 |
Correct |
13 ms |
19136 KB |
Output is correct |
10 |
Correct |
26 ms |
19840 KB |
Output is correct |
11 |
Correct |
39 ms |
20548 KB |
Output is correct |
12 |
Correct |
51 ms |
21260 KB |
Output is correct |
13 |
Correct |
54 ms |
21504 KB |
Output is correct |
14 |
Correct |
46 ms |
20804 KB |
Output is correct |
15 |
Correct |
43 ms |
20172 KB |
Output is correct |
16 |
Correct |
43 ms |
20164 KB |
Output is correct |
17 |
Correct |
40 ms |
20612 KB |
Output is correct |
18 |
Correct |
8 ms |
19020 KB |
Output is correct |
19 |
Correct |
8 ms |
19100 KB |
Output is correct |
20 |
Correct |
8 ms |
19100 KB |
Output is correct |
21 |
Correct |
11 ms |
19020 KB |
Output is correct |
22 |
Correct |
8 ms |
19020 KB |
Output is correct |
23 |
Correct |
10 ms |
19148 KB |
Output is correct |
24 |
Correct |
9 ms |
19148 KB |
Output is correct |
25 |
Correct |
11 ms |
19120 KB |
Output is correct |
26 |
Correct |
10 ms |
19184 KB |
Output is correct |
27 |
Correct |
37 ms |
19908 KB |
Output is correct |
28 |
Correct |
64 ms |
21260 KB |
Output is correct |
29 |
Correct |
58 ms |
21512 KB |
Output is correct |
30 |
Correct |
54 ms |
20996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
20076 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
8 ms |
19100 KB |
Output is correct |
4 |
Correct |
8 ms |
19020 KB |
Output is correct |
5 |
Correct |
9 ms |
19020 KB |
Output is correct |
6 |
Correct |
9 ms |
19020 KB |
Output is correct |
7 |
Correct |
9 ms |
19228 KB |
Output is correct |
8 |
Correct |
33 ms |
19972 KB |
Output is correct |
9 |
Correct |
31 ms |
19976 KB |
Output is correct |
10 |
Correct |
22 ms |
19276 KB |
Output is correct |
11 |
Correct |
41 ms |
20700 KB |
Output is correct |
12 |
Correct |
9 ms |
19020 KB |
Output is correct |
13 |
Correct |
8 ms |
19020 KB |
Output is correct |
14 |
Correct |
8 ms |
19088 KB |
Output is correct |
15 |
Correct |
8 ms |
19020 KB |
Output is correct |
16 |
Correct |
8 ms |
19112 KB |
Output is correct |
17 |
Correct |
11 ms |
19148 KB |
Output is correct |
18 |
Correct |
12 ms |
19152 KB |
Output is correct |
19 |
Correct |
29 ms |
19972 KB |
Output is correct |
20 |
Correct |
57 ms |
21388 KB |
Output is correct |
21 |
Correct |
47 ms |
20776 KB |
Output is correct |
22 |
Correct |
42 ms |
20372 KB |
Output is correct |
23 |
Correct |
62 ms |
21484 KB |
Output is correct |
24 |
Correct |
40 ms |
20552 KB |
Output is correct |
25 |
Correct |
46 ms |
20292 KB |
Output is correct |
26 |
Correct |
47 ms |
20236 KB |
Output is correct |
27 |
Correct |
11 ms |
18984 KB |
Output is correct |
28 |
Correct |
8 ms |
19108 KB |
Output is correct |
29 |
Correct |
9 ms |
19096 KB |
Output is correct |
30 |
Correct |
8 ms |
19040 KB |
Output is correct |
31 |
Correct |
8 ms |
19108 KB |
Output is correct |
32 |
Correct |
10 ms |
19020 KB |
Output is correct |
33 |
Correct |
9 ms |
19020 KB |
Output is correct |
34 |
Correct |
10 ms |
19116 KB |
Output is correct |
35 |
Correct |
13 ms |
19136 KB |
Output is correct |
36 |
Correct |
26 ms |
19840 KB |
Output is correct |
37 |
Correct |
39 ms |
20548 KB |
Output is correct |
38 |
Correct |
51 ms |
21260 KB |
Output is correct |
39 |
Correct |
54 ms |
21504 KB |
Output is correct |
40 |
Correct |
46 ms |
20804 KB |
Output is correct |
41 |
Correct |
43 ms |
20172 KB |
Output is correct |
42 |
Correct |
43 ms |
20164 KB |
Output is correct |
43 |
Correct |
40 ms |
20612 KB |
Output is correct |
44 |
Correct |
8 ms |
19020 KB |
Output is correct |
45 |
Correct |
8 ms |
19100 KB |
Output is correct |
46 |
Correct |
8 ms |
19100 KB |
Output is correct |
47 |
Correct |
11 ms |
19020 KB |
Output is correct |
48 |
Correct |
8 ms |
19020 KB |
Output is correct |
49 |
Correct |
10 ms |
19148 KB |
Output is correct |
50 |
Correct |
9 ms |
19148 KB |
Output is correct |
51 |
Correct |
11 ms |
19120 KB |
Output is correct |
52 |
Correct |
10 ms |
19184 KB |
Output is correct |
53 |
Correct |
37 ms |
19908 KB |
Output is correct |
54 |
Correct |
64 ms |
21260 KB |
Output is correct |
55 |
Correct |
58 ms |
21512 KB |
Output is correct |
56 |
Correct |
54 ms |
20996 KB |
Output is correct |
57 |
Correct |
82 ms |
22444 KB |
Output is correct |
58 |
Correct |
32 ms |
20036 KB |
Output is correct |
59 |
Correct |
48 ms |
20864 KB |
Output is correct |