#include <bits/stdc++.h>
using namespace std;;
#define ll long long
#define ar array
#define ld long double
//#define int long long
#define all(v) v.begin(), v.end()
const int N = 5e4 + 20;
const int K = 469;
const int LOG = 26;
const int INF = 1e9;
int MOD = 998244353;
void chmin(int &x,int y){x = min(x, y);}
int n, k, m, q;
struct Koce{
vector<vector<int> > A;
Koce(){
A.resize(k, vector<int>(k, INF));
}
};
Koce operator+(Koce a, Koce b){
Koce ans;
for(int i = 0;i < k;i++){
for(int j = 0;j < k;j++){
for(int x = 0;x < k;x++)chmin(ans.A[i][j], a.A[i][x] + b.A[x][j]);
}
}
return ans;
}
Koce s[4 * N];
void bld(int u,int tl,int tr, Koce A[]){
if(tl == tr){
s[u] = A[tl];
return;
}
int tm = (tl + tr) / 2;
bld(u * 2, tl, tm, A);
bld(u * 2 + 1, tm + 1, tr, A);
s[u] = s[u * 2] + s[u * 2 + 1];
}
Koce qry(int u,int tl,int tr,int l,int r){
if(l > tr || tl > r){
Koce ret;
for(int i = 0;i < k;i++)ret.A[i][i] = 0;
return ret;
}
if(l <= tl && tr <= r)return s[u];
int tm = (tl + tr) / 2;
return qry(u * 2, tl, tm, l, r) + qry(u * 2 + 1,tm + 1, tr, l, r);
}
void orz(){
cin>>k>>n>>m>>q;
Koce A[n / k + 1];
while(m--){
int a, b, c;
cin>>a>>b>>c;
int i = a / k;
chmin(A[i].A[a % k][b % k], c);
}
bld(1, 0, n / k, A);
while(q--){
int a, b;
cin>>a>>b;
int i = a / k;
int j = b / k;
if(i >= j){
cout<<"-1\n";
continue;
}
Koce ret = qry(1, 0, n / k, i, j - 1);
int ans = ret.A[a % k][b % k];
if(ans >= INF)ans = -1;
cout<<ans<<'\n';
}
}
signed main(){ios_base::sync_with_stdio(false);cin.tie(0);
int t;
//cin>>t;
t = 1;
while(t--)orz();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |