Submission #1040767

#TimeUsernameProblemLanguageResultExecution timeMemory
1040767n3rm1nToll (BOI17_toll)C++17
0 / 100
3069 ms11940 KiB
#include<bits/stdc++.h> #define endl '\n' using namespace std; typedef long long ll; const ll MAXN = 5e4 + 10; void speed() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } struct querie { ll ql, qr, index; querie(){}; querie(ll _ql, ll _qr, ll _index) { ql = _ql; qr = _qr; index = _index; } }; ll k, n, m, q; vector < pair < ll, ll > > g[MAXN]; map < pair < ll, ll >, ll > mp; void read() { cin >> k >> n >> m >> q; ll from, to, cost; for (ll i = 1; i <= m; ++ i) { cin >> from >> to >> cost; from ++; to ++; mp[{from, to}] = cost; } } vector < pair < ll, ll > > ques[MAXN]; ll inf = 1e17; /// fix struct info { ll ver; ll u[MAXN]; info() { memset(u, 0, sizeof(u)); }; }; deque < info > dq; ll ans[MAXN]; void queries() { ll ql, qr; for (ll i = 1; i <= q; ++ i) { cin >> ql >> qr; ql ++; qr ++; ques[ql].push_back(make_pair(qr, i)); } for (int i = 1; i <= q; ++ i) ans[i] = inf; ll last_bucket = (n-1)/k, last = n; while((last-1) / k == last_bucket) { info newinfo; newinfo.ver = last; dq.push_back(newinfo); last --; } ll i = last; while(i >= 1) { ll curr_bucket = (i-1)/k; // cout << "start at " << i << " " << curr_bucket << endl; ll cut_later = dq.size(); // cout << i << "- >" << endl; while(i >= 1 && (i-1)/k == curr_bucket) { info newinfo; newinfo.ver = i; /* cout << "initt " << i << endl; for (int jj = 0; jj <= n; ++ jj) cout << newinfo.u[jj] << " "; cout << endl;*/ for (ll j = 0; j < cut_later; ++ j) { ll nb = dq[j].ver; ll newcost = mp[make_pair(i, nb)]; if(!newcost)continue; // cout << i << " " << nb << " -> " << newcost << endl; for (ll to = 1; to <= n; ++ to) { if(to == i)continue; if(to == nb) { newinfo.u[to] = newcost; continue; } if(!dq[j].u[to])continue; if(newinfo.u[to] == 0)newinfo.u[to] = dq[j].u[to] + newcost; else newinfo.u[to] = min(newinfo.u[to], dq[j].u[to] + newcost); } } /* cout << i << endl; for (int ii = 1; ii <= n; ++ ii) cout << newinfo.u[ii] << " "; cout << endl;*/ dq.push_back(newinfo); i --; } ///cout << i << "<- " << endl; while(cut_later --) dq.pop_front(); // cout << "* " << dq.size() << endl; for (ll j = 0; j < dq.size(); ++ j) { ll v = dq[j].ver; for (auto query: ques[v]) { if(dq[j].u[query.first])ans[query.second] = dq[j].u[query.first]; //cout << "here act" << endl; } } } for (ll i = 1; i <= q; ++ i) { if(ans[i] == inf)cout << -1 << endl; else cout << ans[i] << endl; } } int main() { speed(); read(); queries(); return 0; }

Compilation message (stderr)

toll.cpp: In function 'void queries()':
toll.cpp:123:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::deque<info>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |         for (ll j = 0; j < dq.size(); ++ j)
      |                        ~~^~~~~~~~~~~
#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...