#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
using namespace __gnu_pbds;
/*
ll add(ll x,ll y){
x+=y;
if(x<0){
x%=mod;
x+=mod;
}else{
if(x>=mod) x%=mod;
}
return x;
}
ll mul(ll a,ll b){
ll ans = (a*b)%mod;
if(ans<0) ans+=mod;
return ans;
}
*/
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
#define maxn 50005
#define lg 20
ll n,k,m,q;
struct mat{
ll a[5][5];
mat(){
for(ll i = 0;i<5;i++) for(ll j = 0;j<5;j++) a[i][j] = llinf;
}
mat operator + (mat b){
mat c;
for(ll i = 0;i<5;i++){
for(ll j = 0;j<5;j++){
for(ll k = 0;k<5;k++){
c.a[i][j] = min(c.a[i][j],a[i][k] + b.a[k][j]);
}
}
}
return c;
}
};
mat dp[maxn][lg];
int main(){
daj_mi_malo_vremena
cin >> k >> n >> m >> q;
for(ll i = 1;i<=m;i++){
ll x,y,w; cin >> x >> y >> w;
dp[x/k][0].a[x%k][y%k] = w;
}
for(ll j = 1;j<lg;j++){
for(ll i = 0;i<n;i++){
if(i+(1<<j)+1>n) break;
dp[i][j] = dp[i][j-1] + dp[i+(1<<(j-1))][j-1];
}
}
while(q--){
ll x,y; cin >> x >> y;
ll xd = x/k,yd = y/k;
if(xd==yd){cout<<-1<<endl; continue;}
mat ans;
for(ll i = 0;i<5;i++) ans.a[i][i] = 0;
for(ll j = lg-1;j>=0;j--){
if(xd+(1<<j)<=yd){
ans = ans + dp[xd][j];
xd+=(1<<j);
}
}
ll val = ans.a[x%k][y%k];
if(val==llinf) val = -1;
cout<<val<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
298 ms |
196056 KB |
Output is correct |
2 |
Correct |
74 ms |
195960 KB |
Output is correct |
3 |
Correct |
81 ms |
195992 KB |
Output is correct |
4 |
Correct |
90 ms |
195972 KB |
Output is correct |
5 |
Correct |
80 ms |
196016 KB |
Output is correct |
6 |
Correct |
89 ms |
195916 KB |
Output is correct |
7 |
Correct |
76 ms |
195988 KB |
Output is correct |
8 |
Correct |
318 ms |
195964 KB |
Output is correct |
9 |
Correct |
275 ms |
196040 KB |
Output is correct |
10 |
Correct |
238 ms |
196016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
196024 KB |
Output is correct |
2 |
Correct |
104 ms |
195924 KB |
Output is correct |
3 |
Correct |
88 ms |
195900 KB |
Output is correct |
4 |
Correct |
82 ms |
195912 KB |
Output is correct |
5 |
Correct |
77 ms |
195952 KB |
Output is correct |
6 |
Correct |
78 ms |
195964 KB |
Output is correct |
7 |
Correct |
95 ms |
196020 KB |
Output is correct |
8 |
Correct |
109 ms |
196008 KB |
Output is correct |
9 |
Correct |
258 ms |
195964 KB |
Output is correct |
10 |
Correct |
359 ms |
196016 KB |
Output is correct |
11 |
Correct |
285 ms |
196088 KB |
Output is correct |
12 |
Correct |
298 ms |
196052 KB |
Output is correct |
13 |
Correct |
205 ms |
196020 KB |
Output is correct |
14 |
Correct |
194 ms |
195992 KB |
Output is correct |
15 |
Correct |
190 ms |
196044 KB |
Output is correct |
16 |
Correct |
253 ms |
196028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
195968 KB |
Output is correct |
2 |
Correct |
88 ms |
195916 KB |
Output is correct |
3 |
Correct |
78 ms |
195960 KB |
Output is correct |
4 |
Correct |
83 ms |
196016 KB |
Output is correct |
5 |
Correct |
83 ms |
195928 KB |
Output is correct |
6 |
Correct |
85 ms |
195928 KB |
Output is correct |
7 |
Correct |
84 ms |
196016 KB |
Output is correct |
8 |
Correct |
82 ms |
195916 KB |
Output is correct |
9 |
Correct |
94 ms |
195988 KB |
Output is correct |
10 |
Correct |
261 ms |
196120 KB |
Output is correct |
11 |
Correct |
268 ms |
196016 KB |
Output is correct |
12 |
Correct |
268 ms |
196076 KB |
Output is correct |
13 |
Correct |
263 ms |
195916 KB |
Output is correct |
14 |
Correct |
267 ms |
196016 KB |
Output is correct |
15 |
Correct |
190 ms |
196016 KB |
Output is correct |
16 |
Correct |
230 ms |
196016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
195968 KB |
Output is correct |
2 |
Correct |
88 ms |
195916 KB |
Output is correct |
3 |
Correct |
78 ms |
195960 KB |
Output is correct |
4 |
Correct |
83 ms |
196016 KB |
Output is correct |
5 |
Correct |
83 ms |
195928 KB |
Output is correct |
6 |
Correct |
85 ms |
195928 KB |
Output is correct |
7 |
Correct |
84 ms |
196016 KB |
Output is correct |
8 |
Correct |
82 ms |
195916 KB |
Output is correct |
9 |
Correct |
94 ms |
195988 KB |
Output is correct |
10 |
Correct |
261 ms |
196120 KB |
Output is correct |
11 |
Correct |
268 ms |
196016 KB |
Output is correct |
12 |
Correct |
268 ms |
196076 KB |
Output is correct |
13 |
Correct |
263 ms |
195916 KB |
Output is correct |
14 |
Correct |
267 ms |
196016 KB |
Output is correct |
15 |
Correct |
190 ms |
196016 KB |
Output is correct |
16 |
Correct |
230 ms |
196016 KB |
Output is correct |
17 |
Correct |
353 ms |
196016 KB |
Output is correct |
18 |
Correct |
94 ms |
195984 KB |
Output is correct |
19 |
Correct |
91 ms |
196052 KB |
Output is correct |
20 |
Correct |
83 ms |
195996 KB |
Output is correct |
21 |
Correct |
85 ms |
195916 KB |
Output is correct |
22 |
Correct |
104 ms |
195972 KB |
Output is correct |
23 |
Correct |
96 ms |
196060 KB |
Output is correct |
24 |
Correct |
88 ms |
196044 KB |
Output is correct |
25 |
Correct |
87 ms |
195928 KB |
Output is correct |
26 |
Correct |
90 ms |
195976 KB |
Output is correct |
27 |
Correct |
250 ms |
196020 KB |
Output is correct |
28 |
Correct |
402 ms |
195976 KB |
Output is correct |
29 |
Correct |
261 ms |
196016 KB |
Output is correct |
30 |
Correct |
275 ms |
196024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
298 ms |
196056 KB |
Output is correct |
2 |
Correct |
74 ms |
195960 KB |
Output is correct |
3 |
Correct |
81 ms |
195992 KB |
Output is correct |
4 |
Correct |
90 ms |
195972 KB |
Output is correct |
5 |
Correct |
80 ms |
196016 KB |
Output is correct |
6 |
Correct |
89 ms |
195916 KB |
Output is correct |
7 |
Correct |
76 ms |
195988 KB |
Output is correct |
8 |
Correct |
318 ms |
195964 KB |
Output is correct |
9 |
Correct |
275 ms |
196040 KB |
Output is correct |
10 |
Correct |
238 ms |
196016 KB |
Output is correct |
11 |
Correct |
264 ms |
196024 KB |
Output is correct |
12 |
Correct |
104 ms |
195924 KB |
Output is correct |
13 |
Correct |
88 ms |
195900 KB |
Output is correct |
14 |
Correct |
82 ms |
195912 KB |
Output is correct |
15 |
Correct |
77 ms |
195952 KB |
Output is correct |
16 |
Correct |
78 ms |
195964 KB |
Output is correct |
17 |
Correct |
95 ms |
196020 KB |
Output is correct |
18 |
Correct |
109 ms |
196008 KB |
Output is correct |
19 |
Correct |
258 ms |
195964 KB |
Output is correct |
20 |
Correct |
359 ms |
196016 KB |
Output is correct |
21 |
Correct |
285 ms |
196088 KB |
Output is correct |
22 |
Correct |
298 ms |
196052 KB |
Output is correct |
23 |
Correct |
205 ms |
196020 KB |
Output is correct |
24 |
Correct |
194 ms |
195992 KB |
Output is correct |
25 |
Correct |
190 ms |
196044 KB |
Output is correct |
26 |
Correct |
253 ms |
196028 KB |
Output is correct |
27 |
Correct |
86 ms |
195968 KB |
Output is correct |
28 |
Correct |
88 ms |
195916 KB |
Output is correct |
29 |
Correct |
78 ms |
195960 KB |
Output is correct |
30 |
Correct |
83 ms |
196016 KB |
Output is correct |
31 |
Correct |
83 ms |
195928 KB |
Output is correct |
32 |
Correct |
85 ms |
195928 KB |
Output is correct |
33 |
Correct |
84 ms |
196016 KB |
Output is correct |
34 |
Correct |
82 ms |
195916 KB |
Output is correct |
35 |
Correct |
94 ms |
195988 KB |
Output is correct |
36 |
Correct |
261 ms |
196120 KB |
Output is correct |
37 |
Correct |
268 ms |
196016 KB |
Output is correct |
38 |
Correct |
268 ms |
196076 KB |
Output is correct |
39 |
Correct |
263 ms |
195916 KB |
Output is correct |
40 |
Correct |
267 ms |
196016 KB |
Output is correct |
41 |
Correct |
190 ms |
196016 KB |
Output is correct |
42 |
Correct |
230 ms |
196016 KB |
Output is correct |
43 |
Correct |
353 ms |
196016 KB |
Output is correct |
44 |
Correct |
94 ms |
195984 KB |
Output is correct |
45 |
Correct |
91 ms |
196052 KB |
Output is correct |
46 |
Correct |
83 ms |
195996 KB |
Output is correct |
47 |
Correct |
85 ms |
195916 KB |
Output is correct |
48 |
Correct |
104 ms |
195972 KB |
Output is correct |
49 |
Correct |
96 ms |
196060 KB |
Output is correct |
50 |
Correct |
88 ms |
196044 KB |
Output is correct |
51 |
Correct |
87 ms |
195928 KB |
Output is correct |
52 |
Correct |
90 ms |
195976 KB |
Output is correct |
53 |
Correct |
250 ms |
196020 KB |
Output is correct |
54 |
Correct |
402 ms |
195976 KB |
Output is correct |
55 |
Correct |
261 ms |
196016 KB |
Output is correct |
56 |
Correct |
275 ms |
196024 KB |
Output is correct |
57 |
Correct |
280 ms |
196024 KB |
Output is correct |
58 |
Correct |
263 ms |
196024 KB |
Output is correct |
59 |
Correct |
266 ms |
195992 KB |
Output is correct |