/*#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
#include<bits/stdc++.h>
/**/
//typedef int ll;
typedef long long ll;
typedef unsigned long long ull;
typedef std::string str;
//typedef vector<std::vector<ll>> vv;
/*typedef __int128 vll;
typedef unsigned __int128 uvll;*/
#define llll std::pair<ll , ll>
#define pb push_back
#define pf push_front
#define halo cout << "hello\n"
#define fi first
#define sec second
#define vv(x) std::vector<std::vector<x>>
#define all(a) a.begin() , a.end()
const ll limit = 1e15+7;
const ll ous = 3e5 + 7;
const ll dx[4] = {-1 , 0 , 1 , 0} , dy[4] = {0,1,0,-1};
ll n , m , k , o;
std::vector<std::vector<std::vector<ll>>> valt , ar;
std::vector<std::vector<ll>> mult(std::vector<std::vector<ll>> a , std::vector<std::vector<ll>> b){
std::vector<std::vector<ll>> res(k , std::vector<ll>(k , limit));
for(ll i = 0;k>i;i++){
for(ll j = 0;k>j;j++){
ll cur = limit;
for(ll tt =0;k>tt;tt++){
cur = std::min(cur , a[i][tt] + b[tt][j]);
}
res[i][j] = cur;
}
}
return res;
}
void build(ll l , ll r , ll a){
if(l == r){
valt[a] = ar[l];
return;
}
ll md = (l + r)/2;
build(l , md , a*2);
build(md+1 , r , a*2+1);
valt[a] = mult(valt[a*2] , valt[a*2+1]);
return;
}
std::vector<std::vector<ll>> rq(ll l , ll r , ll nl , ll nr , ll a){
if(nl > r || nr < l){
std::vector<std::vector<ll>> rte(k , std::vector<ll>(k , limit));
for(ll i = 0;k>i;i++){
rte[i][i] = 0;
}
return rte;
}
if(l <= nl && r >= nr){
return valt[a];
}
ll md = (nl + nr)/2;
std::vector<std::vector<ll>> f = rq(l , r , nl , md , a*2) , s = rq(l , r , md+1 , nr , a*2+1);
return mult(f , s);
}
void solve(){
std::cin >> k >> n >> m >> o;
ar.resize(n/k+15 , std::vector<std::vector<ll>>(k , std::vector<ll>(k , limit)));
valt.resize((n/k+1) * 4 , std::vector<std::vector<ll>>(k , std::vector<ll>(k , limit)));
for(ll i = 0;m>i;i++){
ll x , y , z;std::cin >> x >> y >> z;
ar[x/k][x%k][y%k] = std::min(ar[x/k][x%k][y%k] , z);
}
/*for(ll i = 0;n/k>=i;i++){
for(ll j = 0;k>j;j++){
for(ll tso = 0;k>tso;tso++){
std::cout << ar[i][j][tso] << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}*/
build(0 , (n-1)/k , 1);
while(o--){
ll x , y;std::cin >> x >> y;
if(x/k >= y/k){
std::cout << -1 << "\n";
}
else{
std::vector<std::vector<ll>> bt = rq(x/k , y/k - 1 , 0 , (n-1)/k , 1);
/*for(ll j = 0;k>j;j++){
for(ll tso = 0;k>tso;tso++){
std::cout << bt[j][tso] << " ";
}
std::cout << "\n";
}*/
if(bt[x%k][y%k] < limit){
std::cout << bt[x%k][y%k] << "\n";
}
else{
std::cout << -1 << "\n";
}
}
}
return;/**/
}
signed main(){
std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
ll t=1;
//std::cin >> t;
ll o = 1;
while(t--){
//cout << "Case " << o++ << ":\n";
solve();
}
return 0;
}/**/
Compilation message
toll.cpp:5:78: warning: "/*" within comment [-Wcomment]
5 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
|
toll.cpp: In function 'int main()':
toll.cpp:112:8: warning: unused variable 'o' [-Wunused-variable]
112 | ll o = 1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
21864 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
724 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
5 ms |
752 KB |
Output is correct |
8 |
Correct |
124 ms |
21852 KB |
Output is correct |
9 |
Correct |
133 ms |
21964 KB |
Output is correct |
10 |
Correct |
108 ms |
21876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
18960 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
26 ms |
764 KB |
Output is correct |
8 |
Correct |
38 ms |
744 KB |
Output is correct |
9 |
Correct |
91 ms |
21856 KB |
Output is correct |
10 |
Correct |
152 ms |
16624 KB |
Output is correct |
11 |
Correct |
111 ms |
20664 KB |
Output is correct |
12 |
Correct |
119 ms |
17832 KB |
Output is correct |
13 |
Correct |
88 ms |
14124 KB |
Output is correct |
14 |
Correct |
60 ms |
11508 KB |
Output is correct |
15 |
Correct |
67 ms |
13004 KB |
Output is correct |
16 |
Correct |
67 ms |
13068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
3 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
55 ms |
21796 KB |
Output is correct |
11 |
Correct |
57 ms |
18900 KB |
Output is correct |
12 |
Correct |
60 ms |
16600 KB |
Output is correct |
13 |
Correct |
75 ms |
19044 KB |
Output is correct |
14 |
Correct |
57 ms |
18412 KB |
Output is correct |
15 |
Correct |
40 ms |
12976 KB |
Output is correct |
16 |
Correct |
40 ms |
12896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
3 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
55 ms |
21796 KB |
Output is correct |
11 |
Correct |
57 ms |
18900 KB |
Output is correct |
12 |
Correct |
60 ms |
16600 KB |
Output is correct |
13 |
Correct |
75 ms |
19044 KB |
Output is correct |
14 |
Correct |
57 ms |
18412 KB |
Output is correct |
15 |
Correct |
40 ms |
12976 KB |
Output is correct |
16 |
Correct |
40 ms |
12896 KB |
Output is correct |
17 |
Correct |
85 ms |
20432 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
15 ms |
792 KB |
Output is correct |
24 |
Correct |
24 ms |
724 KB |
Output is correct |
25 |
Correct |
34 ms |
788 KB |
Output is correct |
26 |
Correct |
31 ms |
800 KB |
Output is correct |
27 |
Correct |
71 ms |
22684 KB |
Output is correct |
28 |
Correct |
102 ms |
18820 KB |
Output is correct |
29 |
Correct |
109 ms |
19092 KB |
Output is correct |
30 |
Correct |
103 ms |
18380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
21864 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
724 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
5 ms |
752 KB |
Output is correct |
8 |
Correct |
124 ms |
21852 KB |
Output is correct |
9 |
Correct |
133 ms |
21964 KB |
Output is correct |
10 |
Correct |
108 ms |
21876 KB |
Output is correct |
11 |
Correct |
109 ms |
18960 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
26 ms |
764 KB |
Output is correct |
18 |
Correct |
38 ms |
744 KB |
Output is correct |
19 |
Correct |
91 ms |
21856 KB |
Output is correct |
20 |
Correct |
152 ms |
16624 KB |
Output is correct |
21 |
Correct |
111 ms |
20664 KB |
Output is correct |
22 |
Correct |
119 ms |
17832 KB |
Output is correct |
23 |
Correct |
88 ms |
14124 KB |
Output is correct |
24 |
Correct |
60 ms |
11508 KB |
Output is correct |
25 |
Correct |
67 ms |
13004 KB |
Output is correct |
26 |
Correct |
67 ms |
13068 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
3 ms |
724 KB |
Output is correct |
36 |
Correct |
55 ms |
21796 KB |
Output is correct |
37 |
Correct |
57 ms |
18900 KB |
Output is correct |
38 |
Correct |
60 ms |
16600 KB |
Output is correct |
39 |
Correct |
75 ms |
19044 KB |
Output is correct |
40 |
Correct |
57 ms |
18412 KB |
Output is correct |
41 |
Correct |
40 ms |
12976 KB |
Output is correct |
42 |
Correct |
40 ms |
12896 KB |
Output is correct |
43 |
Correct |
85 ms |
20432 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
320 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
15 ms |
792 KB |
Output is correct |
50 |
Correct |
24 ms |
724 KB |
Output is correct |
51 |
Correct |
34 ms |
788 KB |
Output is correct |
52 |
Correct |
31 ms |
800 KB |
Output is correct |
53 |
Correct |
71 ms |
22684 KB |
Output is correct |
54 |
Correct |
102 ms |
18820 KB |
Output is correct |
55 |
Correct |
109 ms |
19092 KB |
Output is correct |
56 |
Correct |
103 ms |
18380 KB |
Output is correct |
57 |
Correct |
264 ms |
23704 KB |
Output is correct |
58 |
Correct |
123 ms |
22760 KB |
Output is correct |
59 |
Correct |
157 ms |
20628 KB |
Output is correct |