#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 |
331 ms |
70484 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
11 ms |
1616 KB |
Output is correct |
6 |
Correct |
11 ms |
1616 KB |
Output is correct |
7 |
Correct |
13 ms |
1616 KB |
Output is correct |
8 |
Correct |
309 ms |
70216 KB |
Output is correct |
9 |
Correct |
305 ms |
70316 KB |
Output is correct |
10 |
Correct |
291 ms |
70356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
35384 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
380 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
52 ms |
1616 KB |
Output is correct |
8 |
Correct |
45 ms |
1084 KB |
Output is correct |
9 |
Correct |
227 ms |
70216 KB |
Output is correct |
10 |
Correct |
154 ms |
32328 KB |
Output is correct |
11 |
Correct |
174 ms |
35404 KB |
Output is correct |
12 |
Correct |
151 ms |
32100 KB |
Output is correct |
13 |
Correct |
66 ms |
9032 KB |
Output is correct |
14 |
Correct |
63 ms |
17000 KB |
Output is correct |
15 |
Correct |
45 ms |
9208 KB |
Output is correct |
16 |
Correct |
45 ms |
9112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
4 ms |
1616 KB |
Output is correct |
7 |
Correct |
3 ms |
1020 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
125 ms |
70328 KB |
Output is correct |
11 |
Correct |
74 ms |
35384 KB |
Output is correct |
12 |
Correct |
74 ms |
32072 KB |
Output is correct |
13 |
Correct |
83 ms |
32192 KB |
Output is correct |
14 |
Correct |
69 ms |
32076 KB |
Output is correct |
15 |
Correct |
27 ms |
9040 KB |
Output is correct |
16 |
Correct |
33 ms |
9040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
4 ms |
1616 KB |
Output is correct |
7 |
Correct |
3 ms |
1020 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
125 ms |
70328 KB |
Output is correct |
11 |
Correct |
74 ms |
35384 KB |
Output is correct |
12 |
Correct |
74 ms |
32072 KB |
Output is correct |
13 |
Correct |
83 ms |
32192 KB |
Output is correct |
14 |
Correct |
69 ms |
32076 KB |
Output is correct |
15 |
Correct |
27 ms |
9040 KB |
Output is correct |
16 |
Correct |
33 ms |
9040 KB |
Output is correct |
17 |
Correct |
128 ms |
35400 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
27 ms |
1788 KB |
Output is correct |
24 |
Correct |
24 ms |
848 KB |
Output is correct |
25 |
Correct |
19 ms |
592 KB |
Output is correct |
26 |
Correct |
21 ms |
592 KB |
Output is correct |
27 |
Correct |
180 ms |
70232 KB |
Output is correct |
28 |
Correct |
122 ms |
32072 KB |
Output is correct |
29 |
Correct |
131 ms |
32072 KB |
Output is correct |
30 |
Correct |
117 ms |
32196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
331 ms |
70484 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
11 ms |
1616 KB |
Output is correct |
6 |
Correct |
11 ms |
1616 KB |
Output is correct |
7 |
Correct |
13 ms |
1616 KB |
Output is correct |
8 |
Correct |
309 ms |
70216 KB |
Output is correct |
9 |
Correct |
305 ms |
70316 KB |
Output is correct |
10 |
Correct |
291 ms |
70356 KB |
Output is correct |
11 |
Correct |
171 ms |
35384 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
380 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
52 ms |
1616 KB |
Output is correct |
18 |
Correct |
45 ms |
1084 KB |
Output is correct |
19 |
Correct |
227 ms |
70216 KB |
Output is correct |
20 |
Correct |
154 ms |
32328 KB |
Output is correct |
21 |
Correct |
174 ms |
35404 KB |
Output is correct |
22 |
Correct |
151 ms |
32100 KB |
Output is correct |
23 |
Correct |
66 ms |
9032 KB |
Output is correct |
24 |
Correct |
63 ms |
17000 KB |
Output is correct |
25 |
Correct |
45 ms |
9208 KB |
Output is correct |
26 |
Correct |
45 ms |
9112 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
4 ms |
1616 KB |
Output is correct |
33 |
Correct |
3 ms |
1020 KB |
Output is correct |
34 |
Correct |
2 ms |
592 KB |
Output is correct |
35 |
Correct |
2 ms |
592 KB |
Output is correct |
36 |
Correct |
125 ms |
70328 KB |
Output is correct |
37 |
Correct |
74 ms |
35384 KB |
Output is correct |
38 |
Correct |
74 ms |
32072 KB |
Output is correct |
39 |
Correct |
83 ms |
32192 KB |
Output is correct |
40 |
Correct |
69 ms |
32076 KB |
Output is correct |
41 |
Correct |
27 ms |
9040 KB |
Output is correct |
42 |
Correct |
33 ms |
9040 KB |
Output is correct |
43 |
Correct |
128 ms |
35400 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
27 ms |
1788 KB |
Output is correct |
50 |
Correct |
24 ms |
848 KB |
Output is correct |
51 |
Correct |
19 ms |
592 KB |
Output is correct |
52 |
Correct |
21 ms |
592 KB |
Output is correct |
53 |
Correct |
180 ms |
70232 KB |
Output is correct |
54 |
Correct |
122 ms |
32072 KB |
Output is correct |
55 |
Correct |
131 ms |
32072 KB |
Output is correct |
56 |
Correct |
117 ms |
32196 KB |
Output is correct |
57 |
Correct |
228 ms |
17992 KB |
Output is correct |
58 |
Correct |
310 ms |
70428 KB |
Output is correct |
59 |
Correct |
221 ms |
35408 KB |
Output is correct |