#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
class T{
public:
vector<vector<int>> mat;
T(): mat(5,vector<int>(5,1e18)){};
};
class U{
public:
int temp;
U(): temp(-1){};
};
vector<vector<int>> multiply(vector<vector<int>> &a,vector<vector<int>> &b){
vector<vector<int>> curr(a.size(),vector<int>(5,1e18));
for(int i=0;i<a.size();i++){
for(int k=0;k<5;k++){
for(int j=0;j<5;j++){
curr[i][j]=min(curr[i][j],a[i][k]+b[k][j]);
}
}
}
return curr;
}
class trtr{
public:
T operator()(T trl,T trr){
T pa;
if(trl.mat[0][0]==-1){
return trr;
}
if(trr.mat[0][0]==-1){
return trl;
}
pa.mat=multiply(trl.mat,trr.mat);
return pa;
}
};
class lztr{
public:
T operator()(U &app,T tr){
return tr;
}
};
class lzlz{
public:
U operator()(U &app,U lz){
return lz;
}
};
void print(T &check){
cout << "GOT" << endl;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cout << check.mat[i][j] << ' ';
}
cout << endl;
}
}
template<class T,class U,class trtr,class lztr,class lzlz> struct segtree{
vector<T> seg;
vector<U> d;
trtr TT;
lztr UT;
lzlz UU;
T Tid;
U Uid;
int n;
int lg;
segtree(T Tid,U Uid,trtr TT,lztr UT,lzlz UU): Tid(Tid),Uid(Uid),TT(TT),UT(UT),UU(UU){};
void refresh(int v){
seg[v]=TT(seg[v<<1],seg[(v<<1)|1]);
}
void build(vector<T> &a){
int siz=a.size();
n=1;
while(n<siz){
n<<=1;
}
lg=__lg(n);
seg.assign(n<<1,Tid);
d.assign(n,Uid);
copy(a.begin(),a.end(),seg.begin()+n);
for(int i=n-1;i>1;i--){
refresh(i);
}
}
void apply(int v,U f){
seg[v]=UT(f,seg[v]);
if(v<n){
d[v]=UU(f,d[v]);
}
}
void push(int v){
apply(v<<1,d[v]);
apply((v<<1)|1,d[v]);
d[v]=Uid;
}
void update(int l,int r,U f){
if(l==r){
return;
}
l+=n,r+=n;
int l0=l,r0=r;
for(int i=lg;i>=1;i--){
if((l>>i<<i)!=l){
push(l>>i);
}
if((r>>i<<i)!=r){
push(r>>i);
}
}
for(;l<r;l>>=1,r>>=1){
if(l&1){
apply(l,f);
l++;
}
if(r&1){
r--;
apply(r,f);
}
}
l=l0,r=r0;
for(int i=1;i<=lg;i++){
if((l>>i<<i)!=l){
refresh(l>>i);
}
if((r>>i<<i)!=r){
refresh(r>>i);
}
}
}
T query(int l,int r){
if(l==r){
return Tid;
}
l+=n,r+=n;
for(int i=lg;i>=1;i--){
if((l>>i<<i)!=l){
push(l>>i);
}
if((r>>i<<i)!=r){
push(r>>i);
}
}
T trl=Tid,trr=Tid;
for(;l<r;l>>=1,r>>=1){
if(l&1){
trl=TT(trl,seg[l]);
l++;
}
if(r&1){
r--;
trr=TT(seg[r],trr);
}
}
return TT(trl,trr);
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
trtr TT;
lztr UT;
lzlz UU;
T Tid;
U Uid;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
Tid.mat[i][j]=-1;
}
}
segtree<T,U,trtr,lztr,lzlz> seg(Tid,Uid,TT,UT,UU);
int k,n,m,o;
cin >> k >> n >> m >> o;
vector<T> a((n-1)/k+1);
for(int i=0;i<m;i++){
int u,v,t;
cin >> u >> v >> t;
a[u/k].mat[u%k][v%k]=t;
}
/*for(int i=0;i<a.size();i++){
cout << i << ": " << endl;
print(a[i]);
}*/
seg.build(a);
while(o--){
int u,v;
cin >> u >> v;
if(u/k==v/k){
cout << -1 << endl;
continue;
}
vector<vector<int>> base(1,vector<int>(5,1e18));
base[0][u%k]=0;
T trans=seg.query(u/k,v/k);
base=multiply(base,trans.mat);
if(base[0][v%k]==1e18){
base[0][v%k]=-1;
}
cout << base[0][v%k] << endl;
}
}
Compilation message
toll.cpp: In function 'std::vector<std::vector<long long int> > multiply(std::vector<std::vector<long long int> >&, std::vector<std::vector<long long int> >&)':
toll.cpp:17:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i=0;i<a.size();i++){
| ~^~~~~~~~~
toll.cpp: In instantiation of 'segtree<T, U, trtr, lztr, lzlz>::segtree(T, U, trtr, lztr, lzlz) [with T = T; U = U; trtr = trtr; lztr = lztr; lzlz = lzlz]':
toll.cpp:174:53: required from here
toll.cpp:68:7: warning: 'segtree<T, U, trtr, lztr, lzlz>::Uid' will be initialized after [-Wreorder]
68 | U Uid;
| ^~~
toll.cpp:64:10: warning: 'trtr segtree<T, U, trtr, lztr, lzlz>::TT' [-Wreorder]
64 | trtr TT;
| ^~
toll.cpp:71:5: warning: when initialized here [-Wreorder]
71 | segtree(T Tid,U Uid,trtr TT,lztr UT,lzlz UU): Tid(Tid),Uid(Uid),TT(TT),UT(UT),UU(UU){};
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
71252 KB |
Output is correct |
2 |
Correct |
1 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 |
10 ms |
1628 KB |
Output is correct |
6 |
Correct |
10 ms |
1656 KB |
Output is correct |
7 |
Correct |
10 ms |
1664 KB |
Output is correct |
8 |
Correct |
281 ms |
71200 KB |
Output is correct |
9 |
Correct |
282 ms |
71252 KB |
Output is correct |
10 |
Correct |
270 ms |
70480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
36436 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 |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
65 ms |
1708 KB |
Output is correct |
8 |
Correct |
44 ms |
1144 KB |
Output is correct |
9 |
Correct |
197 ms |
71252 KB |
Output is correct |
10 |
Correct |
147 ms |
34376 KB |
Output is correct |
11 |
Correct |
161 ms |
37200 KB |
Output is correct |
12 |
Correct |
138 ms |
33360 KB |
Output is correct |
13 |
Correct |
57 ms |
11456 KB |
Output is correct |
14 |
Correct |
59 ms |
18456 KB |
Output is correct |
15 |
Correct |
45 ms |
10332 KB |
Output is correct |
16 |
Correct |
54 ms |
10172 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
104 ms |
70996 KB |
Output is correct |
11 |
Correct |
66 ms |
36908 KB |
Output is correct |
12 |
Correct |
81 ms |
34388 KB |
Output is correct |
13 |
Correct |
86 ms |
34644 KB |
Output is correct |
14 |
Correct |
67 ms |
34052 KB |
Output is correct |
15 |
Correct |
27 ms |
10072 KB |
Output is correct |
16 |
Correct |
25 ms |
10076 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
104 ms |
70996 KB |
Output is correct |
11 |
Correct |
66 ms |
36908 KB |
Output is correct |
12 |
Correct |
81 ms |
34388 KB |
Output is correct |
13 |
Correct |
86 ms |
34644 KB |
Output is correct |
14 |
Correct |
67 ms |
34052 KB |
Output is correct |
15 |
Correct |
27 ms |
10072 KB |
Output is correct |
16 |
Correct |
25 ms |
10076 KB |
Output is correct |
17 |
Correct |
123 ms |
36944 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
26 ms |
1628 KB |
Output is correct |
24 |
Correct |
23 ms |
1116 KB |
Output is correct |
25 |
Correct |
20 ms |
600 KB |
Output is correct |
26 |
Correct |
20 ms |
604 KB |
Output is correct |
27 |
Correct |
184 ms |
71080 KB |
Output is correct |
28 |
Correct |
121 ms |
34384 KB |
Output is correct |
29 |
Correct |
113 ms |
34640 KB |
Output is correct |
30 |
Correct |
103 ms |
33856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
71252 KB |
Output is correct |
2 |
Correct |
1 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 |
10 ms |
1628 KB |
Output is correct |
6 |
Correct |
10 ms |
1656 KB |
Output is correct |
7 |
Correct |
10 ms |
1664 KB |
Output is correct |
8 |
Correct |
281 ms |
71200 KB |
Output is correct |
9 |
Correct |
282 ms |
71252 KB |
Output is correct |
10 |
Correct |
270 ms |
70480 KB |
Output is correct |
11 |
Correct |
163 ms |
36436 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
600 KB |
Output is correct |
17 |
Correct |
65 ms |
1708 KB |
Output is correct |
18 |
Correct |
44 ms |
1144 KB |
Output is correct |
19 |
Correct |
197 ms |
71252 KB |
Output is correct |
20 |
Correct |
147 ms |
34376 KB |
Output is correct |
21 |
Correct |
161 ms |
37200 KB |
Output is correct |
22 |
Correct |
138 ms |
33360 KB |
Output is correct |
23 |
Correct |
57 ms |
11456 KB |
Output is correct |
24 |
Correct |
59 ms |
18456 KB |
Output is correct |
25 |
Correct |
45 ms |
10332 KB |
Output is correct |
26 |
Correct |
54 ms |
10172 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 |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
3 ms |
1628 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
104 ms |
70996 KB |
Output is correct |
37 |
Correct |
66 ms |
36908 KB |
Output is correct |
38 |
Correct |
81 ms |
34388 KB |
Output is correct |
39 |
Correct |
86 ms |
34644 KB |
Output is correct |
40 |
Correct |
67 ms |
34052 KB |
Output is correct |
41 |
Correct |
27 ms |
10072 KB |
Output is correct |
42 |
Correct |
25 ms |
10076 KB |
Output is correct |
43 |
Correct |
123 ms |
36944 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
26 ms |
1628 KB |
Output is correct |
50 |
Correct |
23 ms |
1116 KB |
Output is correct |
51 |
Correct |
20 ms |
600 KB |
Output is correct |
52 |
Correct |
20 ms |
604 KB |
Output is correct |
53 |
Correct |
184 ms |
71080 KB |
Output is correct |
54 |
Correct |
121 ms |
34384 KB |
Output is correct |
55 |
Correct |
113 ms |
34640 KB |
Output is correct |
56 |
Correct |
103 ms |
33856 KB |
Output is correct |
57 |
Correct |
199 ms |
21224 KB |
Output is correct |
58 |
Correct |
294 ms |
71508 KB |
Output is correct |
59 |
Correct |
221 ms |
37084 KB |
Output is correct |