#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FOR(i,a) for (int i=0;i<(a);++i)
#define FORD(i,a) for (int i=(a)-1;i>=0;i--)
#define FORT(i,a,b) for (int i=(a);i<=(b);++i)
#define FORTD(i,b,a) for (int i=(b);i>=(a);--i)
#define trav(i,v) for (auto i : v)
#define all(v) v.begin(),v.end()
#define ad push_back
#define fr first
#define sc second
#define mpr(a,b) make_pair(a,b)
#define pir pair<int,int>
#define all(v) v.begin(),v.end()
#define make_unique(v) v.erase(unique(all(v)),v.end())
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define srng mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define y1 EsiHancagorcRepa
const int N=1e5+10;
const ll MOD=1e9+7;
int k,n,m,o;
struct node{
int a[5][5];
node(){
FOR(i,5)FOR(j,5)a[i][j]=INT_MAX;
}
void operator=(node t){
FOR(i,5)FOR(j,5)a[i][j]=t.a[i][j];
}
}sg[4*N],zro;
node mrg(node a,node b){
if (b.a[0][0]==-1)return a;
if (a.a[0][0]==-1)return b;
node c;
FOR(i,k){
FOR(j,k){
c.a[i][j]=INT_MAX;
}
}
FOR(i,k){
FOR(j,k){
FOR(mj,5){
if (a.a[i][mj]==INT_MAX)continue;
if (b.a[mj][j]==INT_MAX)continue;
c.a[i][j]=min(c.a[i][j],a.a[i][mj]+b.a[mj][j]);
}
}
}
return c;
}
vector<pir> g[N];
void build(int v,int tl,int tr){
//sg[v]=new node();
if (tl==tr){
FOR(i,k){
trav(to,g[tl*k+i]){
//cout<<"heey "<<sg[v].a[i][to.fr%k]<<endl;
sg[v].a[i][to.fr%k]=to.sc;
}
}
/*
cout<<v<<" "<<tl<<" "<<tr<<endl;
FOR(i,k){
FOR(j,k){
cout<<sg[v].a[i][j]<<" ";
}
cout<<endl;
}
cout<<"************"<<endl;*/
return;
}
int tm=(tl+tr)/2;
build(v+v,tl,tm);
build(v+v+1,tm+1,tr);
sg[v]=mrg(sg[v+v],sg[v+v+1]);
/*
cout<<v<<" "<<tl<<" "<<tr<<endl;;
FOR(i,k){
FOR(j,k){
cout<<sg[v].a[i][j]<<" ";
}
cout<<endl;
}
cout<<"************"<<endl;*/
}
node query(int v,int tl,int tr,int l,int r){
//cout<<v<<" "<<tl<<" "<<tr<<" "<<l<<" "<<r<<endl;
if (l>r) return zro;
if (tl==l && tr==r)return sg[v];
int tm=(tl+tr)/2;
return mrg(query(v+v,tl,tm,l,min(tm,r)),query(v+v+1,tm+1,tr,max(tm+1,l),r));
}
int main(){
fastio;
srng;
//cout<<zro.a[1][1]<<endl;
//zro=new node();
cin>>k>>n>>m>>o;
FOR(i,m){
int a,b,c;
cin>>a>>b>>c;
g[a].ad({b,c});
//g[b].ad({a,c});
}
zro.a[0][0]=-1;
while(n%k)n++;
int qan=n/k;
build(1,0,qan);
FOR(i,o){
int l,r;
cin>>l>>r;
if (l/k>=r/k){
cout<<-1<<endl;
continue;
}
node tv=query(1,0,qan,l/k,r/k-1);
if (tv.a[l%k][r%k]==INT_MAX)cout<<-1<<endl;
else cout<<tv.a[l%k][r%k]<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
44408 KB |
Output is correct |
2 |
Correct |
37 ms |
41848 KB |
Output is correct |
3 |
Correct |
41 ms |
41848 KB |
Output is correct |
4 |
Correct |
43 ms |
41848 KB |
Output is correct |
5 |
Correct |
49 ms |
41848 KB |
Output is correct |
6 |
Correct |
46 ms |
41840 KB |
Output is correct |
7 |
Correct |
42 ms |
41848 KB |
Output is correct |
8 |
Correct |
121 ms |
44536 KB |
Output is correct |
9 |
Correct |
114 ms |
44148 KB |
Output is correct |
10 |
Correct |
101 ms |
41976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
45176 KB |
Output is correct |
2 |
Correct |
37 ms |
41848 KB |
Output is correct |
3 |
Correct |
38 ms |
41828 KB |
Output is correct |
4 |
Correct |
38 ms |
41764 KB |
Output is correct |
5 |
Correct |
44 ms |
41848 KB |
Output is correct |
6 |
Correct |
39 ms |
41844 KB |
Output is correct |
7 |
Correct |
70 ms |
41976 KB |
Output is correct |
8 |
Correct |
71 ms |
42040 KB |
Output is correct |
9 |
Correct |
117 ms |
44432 KB |
Output is correct |
10 |
Correct |
153 ms |
46712 KB |
Output is correct |
11 |
Correct |
130 ms |
45248 KB |
Output is correct |
12 |
Correct |
124 ms |
44664 KB |
Output is correct |
13 |
Correct |
130 ms |
47184 KB |
Output is correct |
14 |
Correct |
95 ms |
45148 KB |
Output is correct |
15 |
Correct |
93 ms |
44384 KB |
Output is correct |
16 |
Correct |
94 ms |
44496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
41848 KB |
Output is correct |
2 |
Correct |
40 ms |
41948 KB |
Output is correct |
3 |
Correct |
38 ms |
41848 KB |
Output is correct |
4 |
Correct |
38 ms |
41848 KB |
Output is correct |
5 |
Correct |
38 ms |
41868 KB |
Output is correct |
6 |
Correct |
39 ms |
41848 KB |
Output is correct |
7 |
Correct |
39 ms |
41820 KB |
Output is correct |
8 |
Correct |
40 ms |
41976 KB |
Output is correct |
9 |
Correct |
40 ms |
41892 KB |
Output is correct |
10 |
Correct |
72 ms |
44280 KB |
Output is correct |
11 |
Correct |
114 ms |
44904 KB |
Output is correct |
12 |
Correct |
140 ms |
46532 KB |
Output is correct |
13 |
Correct |
122 ms |
47096 KB |
Output is correct |
14 |
Correct |
100 ms |
45660 KB |
Output is correct |
15 |
Correct |
79 ms |
44280 KB |
Output is correct |
16 |
Correct |
78 ms |
44360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
41848 KB |
Output is correct |
2 |
Correct |
40 ms |
41948 KB |
Output is correct |
3 |
Correct |
38 ms |
41848 KB |
Output is correct |
4 |
Correct |
38 ms |
41848 KB |
Output is correct |
5 |
Correct |
38 ms |
41868 KB |
Output is correct |
6 |
Correct |
39 ms |
41848 KB |
Output is correct |
7 |
Correct |
39 ms |
41820 KB |
Output is correct |
8 |
Correct |
40 ms |
41976 KB |
Output is correct |
9 |
Correct |
40 ms |
41892 KB |
Output is correct |
10 |
Correct |
72 ms |
44280 KB |
Output is correct |
11 |
Correct |
114 ms |
44904 KB |
Output is correct |
12 |
Correct |
140 ms |
46532 KB |
Output is correct |
13 |
Correct |
122 ms |
47096 KB |
Output is correct |
14 |
Correct |
100 ms |
45660 KB |
Output is correct |
15 |
Correct |
79 ms |
44280 KB |
Output is correct |
16 |
Correct |
78 ms |
44360 KB |
Output is correct |
17 |
Correct |
120 ms |
45032 KB |
Output is correct |
18 |
Correct |
37 ms |
41744 KB |
Output is correct |
19 |
Correct |
38 ms |
41848 KB |
Output is correct |
20 |
Correct |
37 ms |
41848 KB |
Output is correct |
21 |
Correct |
38 ms |
41848 KB |
Output is correct |
22 |
Correct |
38 ms |
41832 KB |
Output is correct |
23 |
Correct |
52 ms |
41848 KB |
Output is correct |
24 |
Correct |
50 ms |
41976 KB |
Output is correct |
25 |
Correct |
56 ms |
42104 KB |
Output is correct |
26 |
Correct |
51 ms |
41948 KB |
Output is correct |
27 |
Correct |
85 ms |
44412 KB |
Output is correct |
28 |
Correct |
146 ms |
46556 KB |
Output is correct |
29 |
Correct |
159 ms |
47052 KB |
Output is correct |
30 |
Correct |
121 ms |
46064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
44408 KB |
Output is correct |
2 |
Correct |
37 ms |
41848 KB |
Output is correct |
3 |
Correct |
41 ms |
41848 KB |
Output is correct |
4 |
Correct |
43 ms |
41848 KB |
Output is correct |
5 |
Correct |
49 ms |
41848 KB |
Output is correct |
6 |
Correct |
46 ms |
41840 KB |
Output is correct |
7 |
Correct |
42 ms |
41848 KB |
Output is correct |
8 |
Correct |
121 ms |
44536 KB |
Output is correct |
9 |
Correct |
114 ms |
44148 KB |
Output is correct |
10 |
Correct |
101 ms |
41976 KB |
Output is correct |
11 |
Correct |
128 ms |
45176 KB |
Output is correct |
12 |
Correct |
37 ms |
41848 KB |
Output is correct |
13 |
Correct |
38 ms |
41828 KB |
Output is correct |
14 |
Correct |
38 ms |
41764 KB |
Output is correct |
15 |
Correct |
44 ms |
41848 KB |
Output is correct |
16 |
Correct |
39 ms |
41844 KB |
Output is correct |
17 |
Correct |
70 ms |
41976 KB |
Output is correct |
18 |
Correct |
71 ms |
42040 KB |
Output is correct |
19 |
Correct |
117 ms |
44432 KB |
Output is correct |
20 |
Correct |
153 ms |
46712 KB |
Output is correct |
21 |
Correct |
130 ms |
45248 KB |
Output is correct |
22 |
Correct |
124 ms |
44664 KB |
Output is correct |
23 |
Correct |
130 ms |
47184 KB |
Output is correct |
24 |
Correct |
95 ms |
45148 KB |
Output is correct |
25 |
Correct |
93 ms |
44384 KB |
Output is correct |
26 |
Correct |
94 ms |
44496 KB |
Output is correct |
27 |
Correct |
37 ms |
41848 KB |
Output is correct |
28 |
Correct |
40 ms |
41948 KB |
Output is correct |
29 |
Correct |
38 ms |
41848 KB |
Output is correct |
30 |
Correct |
38 ms |
41848 KB |
Output is correct |
31 |
Correct |
38 ms |
41868 KB |
Output is correct |
32 |
Correct |
39 ms |
41848 KB |
Output is correct |
33 |
Correct |
39 ms |
41820 KB |
Output is correct |
34 |
Correct |
40 ms |
41976 KB |
Output is correct |
35 |
Correct |
40 ms |
41892 KB |
Output is correct |
36 |
Correct |
72 ms |
44280 KB |
Output is correct |
37 |
Correct |
114 ms |
44904 KB |
Output is correct |
38 |
Correct |
140 ms |
46532 KB |
Output is correct |
39 |
Correct |
122 ms |
47096 KB |
Output is correct |
40 |
Correct |
100 ms |
45660 KB |
Output is correct |
41 |
Correct |
79 ms |
44280 KB |
Output is correct |
42 |
Correct |
78 ms |
44360 KB |
Output is correct |
43 |
Correct |
120 ms |
45032 KB |
Output is correct |
44 |
Correct |
37 ms |
41744 KB |
Output is correct |
45 |
Correct |
38 ms |
41848 KB |
Output is correct |
46 |
Correct |
37 ms |
41848 KB |
Output is correct |
47 |
Correct |
38 ms |
41848 KB |
Output is correct |
48 |
Correct |
38 ms |
41832 KB |
Output is correct |
49 |
Correct |
52 ms |
41848 KB |
Output is correct |
50 |
Correct |
50 ms |
41976 KB |
Output is correct |
51 |
Correct |
56 ms |
42104 KB |
Output is correct |
52 |
Correct |
51 ms |
41948 KB |
Output is correct |
53 |
Correct |
85 ms |
44412 KB |
Output is correct |
54 |
Correct |
146 ms |
46556 KB |
Output is correct |
55 |
Correct |
159 ms |
47052 KB |
Output is correct |
56 |
Correct |
121 ms |
46064 KB |
Output is correct |
57 |
Correct |
204 ms |
47932 KB |
Output is correct |
58 |
Correct |
119 ms |
44280 KB |
Output is correct |
59 |
Correct |
134 ms |
45176 KB |
Output is correct |