Submission #531478

#TimeUsernameProblemLanguageResultExecution timeMemory
531478mat50013Toll (BOI17_toll)C++11
7 / 100
415 ms524292 KiB
#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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...