#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int NMAX(50005);
const ll INF(LLONG_MAX / 2 - 5);
vector<pair<int, int> > G[NMAX];
int k;
struct matrice
{
vector<vector<ll> > mat;
matrice()
{
mat = vector<vector<ll> > (k, vector<ll>(k, INF));
}
matrice operator+(matrice b)
{
matrice rez;
for(int i = 0; i < k; ++i)
for(int j = 0; j < k; ++j)
for(int interm = 0; interm < k; ++interm)
rez.mat[i][j] = min(rez.mat[i][j], mat[i][interm] + b.mat[interm][j]);
return rez;
}
} v[NMAX];
struct SegmentTree
{
vector<matrice> Arb;
SegmentTree(int n)
{
int put = 1;
while(put <= n)
put <<= 1;
put <<= 1;
Arb.resize(put);
}
inline void build(int nod, int st, int dr)
{
if(st > dr)
return;
if(st == dr)
{
Arb[nod] = v[st];
return;
}
int mij = (st + dr) / 2;
build(2 * nod, st, mij);
build(2 * nod + 1, mij + 1, dr);
Arb[nod] = Arb[2 * nod] + Arb[2 * nod + 1];
}
matrice Query(int nod, int st, int dr, int a, int b)
{
if(a <= st && dr <= b)
return Arb[nod];
int mij = (st + dr) / 2;
matrice rez1, rez2;
bool ok1 = 0, ok2 = 0;
if(a <= mij)
rez1 = Query(2 * nod, st, mij, a, b), ok1 = 1;
if(b > mij)
rez2 = Query(2 * nod + 1, mij + 1, dr, a, b), ok2 = 1;
if(ok1 && ok2)
return rez1 + rez2;
if(ok1)
return rez1;
return rez2;
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m, o;
cin >> k >> n >> m >> o;
for(int i = 1; i <= m; ++i)
{
int a, b, t;
cin >> a >> b >> t;
G[a].push_back({b, t});
}
for(int segm = 0; segm <= n / k; ++segm)
{
matrice aux;
for(int j = 0; j < k; ++j)
{
int nod = segm * k + j;
for(auto it: G[nod])
aux.mat[j][it.first % k] = it.second;
}
v[segm] = aux;
}
SegmentTree Seg(n / k);
Seg.build(1, 0, n / k);
for(int i = 1; i <= o; ++i)
{
int a, b;
cin >> a >> b;
auto rez = Seg.Query(1, 0, n / k, a / k, b / k - 1);
cout << (rez.mat[a % k][b % k] == INF ? -1 : rez.mat[a % k][b % k]) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
19560 KB |
Output is correct |
2 |
Correct |
2 ms |
2660 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2664 KB |
Output is correct |
5 |
Correct |
7 ms |
2952 KB |
Output is correct |
6 |
Correct |
7 ms |
2892 KB |
Output is correct |
7 |
Correct |
7 ms |
2928 KB |
Output is correct |
8 |
Correct |
141 ms |
19568 KB |
Output is correct |
9 |
Correct |
119 ms |
19440 KB |
Output is correct |
10 |
Correct |
113 ms |
17176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
18736 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
3 |
Runtime error |
415 ms |
524292 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2664 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Runtime error |
379 ms |
524292 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2664 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Runtime error |
379 ms |
524292 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
19560 KB |
Output is correct |
2 |
Correct |
2 ms |
2660 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2664 KB |
Output is correct |
5 |
Correct |
7 ms |
2952 KB |
Output is correct |
6 |
Correct |
7 ms |
2892 KB |
Output is correct |
7 |
Correct |
7 ms |
2928 KB |
Output is correct |
8 |
Correct |
141 ms |
19568 KB |
Output is correct |
9 |
Correct |
119 ms |
19440 KB |
Output is correct |
10 |
Correct |
113 ms |
17176 KB |
Output is correct |
11 |
Correct |
111 ms |
18736 KB |
Output is correct |
12 |
Correct |
3 ms |
2636 KB |
Output is correct |
13 |
Runtime error |
415 ms |
524292 KB |
Execution killed with signal 9 |
14 |
Halted |
0 ms |
0 KB |
- |