#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cmath>
#include <stack>
#include <set>
#include <functional>
#include <bitset>
#include <map>
#include <unordered_map>
#include <queue>
#include <array>
#include <numeric>
#include <complex>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename A> ostream& operator<<(ostream& os, vector<A>& a) { for(auto &c:a)os<<c<<' '; return os;}
template<typename A> istream& operator>>(istream& os, vector<A>& a) { for(auto &c:a)os>>c; return os;}
template<typename A,size_t N> istream& operator>>(istream &os, array<A,N>&a) { for(auto &c:a)os>>c; return os;}
template<typename A,typename B> istream& operator>>(istream& os, pair<A,B>&a) { os>>a.first>>a.second; return os;}
template<typename A> istream& operator >>(istream& os,complex<A>& a){pair<A,A>aux;os>>aux;a=complex<A>(aux.first,aux.second);return os;}
#define bug(a) cerr << "(" << #a << ": " << a << ")\n";
#define all(x) x.begin(),x.end()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define PQ priority_queue
#define x real
#define y imag
using pii= pair<int,int>;
using VI= vector<int>;
using v64= vector<int64_t>;
using point=complex<double>;
using i64= int64_t;
using i16= int16_t;
using u64= uint64_t;
using u32= uint32_t;
using i32= int32_t;
using u16= uint16_t;
const i32 inf=1e9;
const i64 INF=1e18;
const int mod=1e9+7;
const int sigma=26;
///sparse table dp[(int)log2(saritura)][poz/k][x][y]
#define vvii vector<vector<int>>
void solve()
{
int k,n,m,q;
cin>>k>>n>>m>>q;
int mx_jump=(n-1)/k;
//position of last element
vector<vector<vector<vector<int>>>>dp((int)log2(mx_jump)+1,vector<vector<vector<int>>>(mx_jump+1,vector<vector<int>>(k,vector<int>(k,inf))));
for(int i=0;i<m;i++)
{
int x,y,z;
cin>>x>>y>>z;
dp[0][x/k][x%k][y%k]=z;
}
auto combine=[&](const vvii& x,const vvii& y)
{
vvii sol(k,vector<int>(k,inf));
for(int i=0;i<k;i++)
{
for(int j=0;j<k;j++)
{
for(int m=0;m<k;m++)
{
sol[i][j]=min(sol[i][j],x[i][m]+y[m][j]);
}
}
}
return sol;
};
for(int i=1;(1<<i)<=mx_jump;i++)
{
for(int j=0;j+(1<<i)<=mx_jump;j++)
{
dp[i][j]=combine(dp[i-1][j],dp[i-1][j+(1<<(i-1))]);
}
}
while(q--)
{
int x,y;
cin>>x>>y;
vector<int> sol(5,inf);
sol[x%k]=0;
int caut=y%k;
x/=k;
y/=k;
if(x!=y)
{
for(int i=(int)log2(y-x);i>=0;i--)
{
if(x+(1<<i)<=y)
{
auto old=sol;
fill(all(sol),inf);
for(int a=0;a<k;a++)
{
for(int b=0;b<k;b++)
{
sol[b]=min(sol[b],old[a]+dp[i][x][a][b]);
}
}
x+=(1<<i);
}
}
}
if(sol[caut]==inf)
sol[caut]=-1;
cout<<sol[caut]<<'\n';
}
}
main()
{
i32 tt=1;
ios::sync_with_stdio(false);
cin.tie(0);
//cin>>tt;
while(tt--)
{
solve();
}
}
Compilation message
toll.cpp:118:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
118 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
73500 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
1372 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1400 KB |
Output is correct |
8 |
Correct |
138 ms |
73556 KB |
Output is correct |
9 |
Correct |
137 ms |
73556 KB |
Output is correct |
10 |
Correct |
140 ms |
73556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
59732 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
1372 KB |
Output is correct |
8 |
Correct |
6 ms |
1228 KB |
Output is correct |
9 |
Correct |
140 ms |
73564 KB |
Output is correct |
10 |
Correct |
147 ms |
52556 KB |
Output is correct |
11 |
Correct |
136 ms |
59904 KB |
Output is correct |
12 |
Correct |
103 ms |
52564 KB |
Output is correct |
13 |
Correct |
87 ms |
26348 KB |
Output is correct |
14 |
Correct |
61 ms |
29784 KB |
Output is correct |
15 |
Correct |
63 ms |
25984 KB |
Output is correct |
16 |
Correct |
57 ms |
26004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1384 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
118 ms |
73524 KB |
Output is correct |
11 |
Correct |
104 ms |
59768 KB |
Output is correct |
12 |
Correct |
105 ms |
52608 KB |
Output is correct |
13 |
Correct |
115 ms |
52588 KB |
Output is correct |
14 |
Correct |
104 ms |
52796 KB |
Output is correct |
15 |
Correct |
59 ms |
26068 KB |
Output is correct |
16 |
Correct |
64 ms |
26124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1384 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
118 ms |
73524 KB |
Output is correct |
11 |
Correct |
104 ms |
59768 KB |
Output is correct |
12 |
Correct |
105 ms |
52608 KB |
Output is correct |
13 |
Correct |
115 ms |
52588 KB |
Output is correct |
14 |
Correct |
104 ms |
52796 KB |
Output is correct |
15 |
Correct |
59 ms |
26068 KB |
Output is correct |
16 |
Correct |
64 ms |
26124 KB |
Output is correct |
17 |
Correct |
108 ms |
59740 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 |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
3 ms |
1220 KB |
Output is correct |
25 |
Correct |
3 ms |
860 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
121 ms |
73460 KB |
Output is correct |
28 |
Correct |
103 ms |
52596 KB |
Output is correct |
29 |
Correct |
115 ms |
52624 KB |
Output is correct |
30 |
Correct |
113 ms |
52452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
73500 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
1372 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1400 KB |
Output is correct |
8 |
Correct |
138 ms |
73556 KB |
Output is correct |
9 |
Correct |
137 ms |
73556 KB |
Output is correct |
10 |
Correct |
140 ms |
73556 KB |
Output is correct |
11 |
Correct |
110 ms |
59732 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
5 ms |
1372 KB |
Output is correct |
18 |
Correct |
6 ms |
1228 KB |
Output is correct |
19 |
Correct |
140 ms |
73564 KB |
Output is correct |
20 |
Correct |
147 ms |
52556 KB |
Output is correct |
21 |
Correct |
136 ms |
59904 KB |
Output is correct |
22 |
Correct |
103 ms |
52564 KB |
Output is correct |
23 |
Correct |
87 ms |
26348 KB |
Output is correct |
24 |
Correct |
61 ms |
29784 KB |
Output is correct |
25 |
Correct |
63 ms |
25984 KB |
Output is correct |
26 |
Correct |
57 ms |
26004 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1384 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
2 ms |
860 KB |
Output is correct |
36 |
Correct |
118 ms |
73524 KB |
Output is correct |
37 |
Correct |
104 ms |
59768 KB |
Output is correct |
38 |
Correct |
105 ms |
52608 KB |
Output is correct |
39 |
Correct |
115 ms |
52588 KB |
Output is correct |
40 |
Correct |
104 ms |
52796 KB |
Output is correct |
41 |
Correct |
59 ms |
26068 KB |
Output is correct |
42 |
Correct |
64 ms |
26124 KB |
Output is correct |
43 |
Correct |
108 ms |
59740 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 |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
3 ms |
1372 KB |
Output is correct |
50 |
Correct |
3 ms |
1220 KB |
Output is correct |
51 |
Correct |
3 ms |
860 KB |
Output is correct |
52 |
Correct |
3 ms |
860 KB |
Output is correct |
53 |
Correct |
121 ms |
73460 KB |
Output is correct |
54 |
Correct |
103 ms |
52596 KB |
Output is correct |
55 |
Correct |
115 ms |
52624 KB |
Output is correct |
56 |
Correct |
113 ms |
52452 KB |
Output is correct |
57 |
Correct |
147 ms |
48716 KB |
Output is correct |
58 |
Correct |
138 ms |
73400 KB |
Output is correct |
59 |
Correct |
140 ms |
59728 KB |
Output is correct |