#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define int long long
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int,int>>
#define vll vector<pair<long long,long long>>
#define pb push_back
#define ll long long
#define ld long double
#define nl '\n'
#define boost ios::sync_with_stdio(false)
#define mp make_pair
#define se second
#define fi first
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i,x,y) for(int i = x;i<=y;i++)
#define forn(i,y,x) for(int i = y; i >= x; i--)
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define clr(v,k) memset(v,k,sizeof(v))
#define rall(v) v.rbegin() , v.rend()
#define pii pair<int,int>
#define pll pair<ll , ll>
#define vvi vector<vi>
#define vvii vector<vii>
const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 1;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (gcd)
ll lcm(ll a , ll b) {return a * (b / gcd(a , b));} // least common multiple (lcm)
// HERE IS THE SOLUTION
int N , K , M , O;
const int LOG = 15;
int lim;
vvii adj;
vector<vi> comb(vector<vi> a , vector<vi> b)
{
vector<vi> target(K , vi(K , INF));
fore(x , K)
{
fore(y , K)
{
fore(z , K)
{
target[x][y] = min(target[x][y] , a[x][z] + b[z][y]);
}
}
}
return target;
}
vector<vvi> tree;
vvi query(int node , int L , int R , int l , int r)
{
if(l <= L && R <= r)
return tree[node];
int mid = (L + R)/2;
if(r <= mid)
return query(2*node , L , mid , l , r);
if(l > mid)
return query(2*node + 1, mid + 1, R , l, r);
return comb(query(2*node , L , mid , l , r) , query(2*node + 1 , mid + 1 , R , l , r));
}
signed main()
{
boost;
cin.tie(0);
cout.tie(0);
cin>>K>>N>>M>>O;
lim = (N+ K - 1)/K;
while(__builtin_popcount(lim) != 1)
{
lim++;
}
tree.assign(2*lim , vvi(K , vi(K , INF)));
fore(i , M)
{
int a , b ,t;
cin>>a>>b>>t;
tree[(a/K) + lim][a%K][b%K] = min(tree[(a/K) + lim][a%K][b%K] , t);
}
forn(i , lim - 1 ,1)
tree[i] = comb(tree[2*i] , tree[2*i + 1]);
while(O--)
{
int a , b;
cin>>a>>b;
if((a/K) >= (b/K))
{
cout<<-1<<nl;
continue;
}
int ans = query(1 , 0 , lim - 1, a/K , (b/K) - 1)[a%K][b%K];
if(ans >= INF)
cout<<-1<<nl;
else
cout<<ans<<nl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
11612 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
636 KB |
Output is correct |
8 |
Correct |
44 ms |
11864 KB |
Output is correct |
9 |
Correct |
41 ms |
11612 KB |
Output is correct |
10 |
Correct |
45 ms |
11748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
10072 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
728 KB |
Output is correct |
8 |
Correct |
8 ms |
724 KB |
Output is correct |
9 |
Correct |
35 ms |
12472 KB |
Output is correct |
10 |
Correct |
59 ms |
15576 KB |
Output is correct |
11 |
Correct |
44 ms |
11900 KB |
Output is correct |
12 |
Correct |
48 ms |
14428 KB |
Output is correct |
13 |
Correct |
43 ms |
8912 KB |
Output is correct |
14 |
Correct |
32 ms |
8276 KB |
Output is correct |
15 |
Correct |
29 ms |
7760 KB |
Output is correct |
16 |
Correct |
29 ms |
7960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
668 KB |
Output is correct |
10 |
Correct |
27 ms |
12380 KB |
Output is correct |
11 |
Correct |
32 ms |
11600 KB |
Output is correct |
12 |
Correct |
45 ms |
15452 KB |
Output is correct |
13 |
Correct |
49 ms |
15708 KB |
Output is correct |
14 |
Correct |
41 ms |
14940 KB |
Output is correct |
15 |
Correct |
23 ms |
7768 KB |
Output is correct |
16 |
Correct |
24 ms |
7884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
668 KB |
Output is correct |
10 |
Correct |
27 ms |
12380 KB |
Output is correct |
11 |
Correct |
32 ms |
11600 KB |
Output is correct |
12 |
Correct |
45 ms |
15452 KB |
Output is correct |
13 |
Correct |
49 ms |
15708 KB |
Output is correct |
14 |
Correct |
41 ms |
14940 KB |
Output is correct |
15 |
Correct |
23 ms |
7768 KB |
Output is correct |
16 |
Correct |
24 ms |
7884 KB |
Output is correct |
17 |
Correct |
38 ms |
11600 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
4 ms |
600 KB |
Output is correct |
24 |
Correct |
4 ms |
604 KB |
Output is correct |
25 |
Correct |
7 ms |
604 KB |
Output is correct |
26 |
Correct |
6 ms |
604 KB |
Output is correct |
27 |
Correct |
42 ms |
12592 KB |
Output is correct |
28 |
Correct |
54 ms |
15440 KB |
Output is correct |
29 |
Correct |
57 ms |
15696 KB |
Output is correct |
30 |
Correct |
49 ms |
15052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
11612 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
636 KB |
Output is correct |
8 |
Correct |
44 ms |
11864 KB |
Output is correct |
9 |
Correct |
41 ms |
11612 KB |
Output is correct |
10 |
Correct |
45 ms |
11748 KB |
Output is correct |
11 |
Correct |
42 ms |
10072 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
7 ms |
728 KB |
Output is correct |
18 |
Correct |
8 ms |
724 KB |
Output is correct |
19 |
Correct |
35 ms |
12472 KB |
Output is correct |
20 |
Correct |
59 ms |
15576 KB |
Output is correct |
21 |
Correct |
44 ms |
11900 KB |
Output is correct |
22 |
Correct |
48 ms |
14428 KB |
Output is correct |
23 |
Correct |
43 ms |
8912 KB |
Output is correct |
24 |
Correct |
32 ms |
8276 KB |
Output is correct |
25 |
Correct |
29 ms |
7760 KB |
Output is correct |
26 |
Correct |
29 ms |
7960 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
472 KB |
Output is correct |
35 |
Correct |
1 ms |
668 KB |
Output is correct |
36 |
Correct |
27 ms |
12380 KB |
Output is correct |
37 |
Correct |
32 ms |
11600 KB |
Output is correct |
38 |
Correct |
45 ms |
15452 KB |
Output is correct |
39 |
Correct |
49 ms |
15708 KB |
Output is correct |
40 |
Correct |
41 ms |
14940 KB |
Output is correct |
41 |
Correct |
23 ms |
7768 KB |
Output is correct |
42 |
Correct |
24 ms |
7884 KB |
Output is correct |
43 |
Correct |
38 ms |
11600 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
4 ms |
600 KB |
Output is correct |
50 |
Correct |
4 ms |
604 KB |
Output is correct |
51 |
Correct |
7 ms |
604 KB |
Output is correct |
52 |
Correct |
6 ms |
604 KB |
Output is correct |
53 |
Correct |
42 ms |
12592 KB |
Output is correct |
54 |
Correct |
54 ms |
15440 KB |
Output is correct |
55 |
Correct |
57 ms |
15696 KB |
Output is correct |
56 |
Correct |
49 ms |
15052 KB |
Output is correct |
57 |
Correct |
84 ms |
14324 KB |
Output is correct |
58 |
Correct |
46 ms |
12888 KB |
Output is correct |
59 |
Correct |
53 ms |
11860 KB |
Output is correct |