Submission #696952

# Submission time Handle Problem Language Result Execution time Memory
696952 2023-02-07T16:50:50 Z SOCIOPATE Toll (BOI17_toll) C++17
17 / 100
183 ms 130492 KB
#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

#define pll pair<ll, ll>
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define ff first
#define ss second
#define all(v) v.begin(),v.end()

typedef tree<
    pii,
    null_type,
    less<pii>,
    rb_tree_tag,
    tree_order_statistics_node_update> ordset;

#pragma GCC optimize("-O3")
#pragma GCC optimize("unroll-loops")

ll INF = 4e18;
const ll mod = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll binpow(ll n, ll m){
    if(m == 0) return 1ll;
    if(m & 1ll) return (n * binpow(n, m - 1ll)) % mod;
    ll b = binpow(n, m / 2ll);
    return (b*b) % mod;
}

int n, k, m, o;
vector<vector<pll>> b, a;

ll ans[50005][18][17] = {0};

void dq(int l, int r, int lev){
    if(l > r) return;
    int m = (l + r) / 2;
    for(int i = m; i >= l; i--)
        for(int j = 0; j <= 3 * k; j++) ans[i][lev][j] = (i == m - j ? 0ll : INF);
    for(int i = m + 1; i <= r; i++){
        for(int j = 0; j <= 3 * k; j++) ans[i][lev][j] = INF;
        for(pll u : b[i])
            for(int j = 0; j <= 3 * k; j++) ans[i][lev][j] = min(ans[i][lev][j], ans[u.ff][lev][j] + u.ss);
    }
    //cout << l << ' ' << r << ' ' << m << ' ' << lev << '\n';
    for(int i = m; i >= l; i--){
        for(pll u : a[i]){
            if(u.ff > m) continue;
            //cout << i << ' ' << u.ff << ' ' << u.ss << '\n';
            for(int j = 0; j <= 3 * k; j++) ans[i][lev][j] = min(ans[i][lev][j], ans[u.ff][lev][j] + u.ss);
        }
    }
//    for(int i = l; i <= r; i++){
//        cout << i << '\n';
//        for(int j = 0; j <= 3 * k; j++) cout << ans[i][lev][j] << ' ';
//        cout << '\n';
//    }
    dq(l, m - 1, lev + 1);
    dq(m + 1, r, lev + 1);
}

ll ask(int l, int r, int lev, int a, int b){
    assert(l <= r);
    int m = (l + r) / 2;
    //cout << l << ' ' << r << ' ' << m << '\n';
    if(a <= m && m + 1 <= b){
        //cout << l << ' ' << r << ' ' << m << ' ' << a << ' ' << b << '\n';
        ll res = INF;
        for(int j = 0; j <= 3*k; j++) res = min(res, ans[a][lev][j] + ans[b][lev][j]);
        return res;
    }
    if(a < m && b == m) return ans[a][lev][0];
    if(b > m && a == m) return ans[b][lev][0];
    if(a > m) return ask(m + 1, r, lev + 1, a, b);
    return ask(l, m - 1, lev + 1, a, b);
}

void solve(){
    cin >> k >> n >> m >> o;
    a.resize(n);
    b.resize(n);
    for(int i = 0; i < m; i++){
        int u, v, w;
        cin >> u >> v >> w;
        a[u].push_back({v, w});
        b[v].push_back({u, w});
    }
    dq(0, n - 1, 0);
    for(int i = 0; i < o; i++){
        int u, v;
        cin >> u >> v;
        if(u == v){
            cout << 0 << '\n';
            continue;
        }
        if(u > v){
            cout << -1 << '\n';
            continue;
        }
        //cout << u << ' ' << v << '\n';
        ll res = ask(0, n - 1, 0, u, v);
        cout << (res == INF ? -1 : res) << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #endif
	int tt;
    //cin >> tt;
    tt = 1;
    while(tt--){
        solve();
		#ifdef LOCAL
            cout << "__________________________________" << endl;
		#endif
	}
	#ifdef LOCAL
        cout << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << "sec" << '\n';
	#endif
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 93 ms 125884 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 90 ms 125824 KB Output is correct
9 Correct 91 ms 125636 KB Output is correct
10 Correct 70 ms 122520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 127704 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 4 ms 3028 KB Output is correct
8 Correct 4 ms 3028 KB Output is correct
9 Correct 95 ms 125776 KB Output is correct
10 Correct 183 ms 130492 KB Output is correct
11 Correct 142 ms 128032 KB Output is correct
12 Correct 117 ms 126780 KB Output is correct
13 Correct 137 ms 83224 KB Output is correct
14 Correct 94 ms 79036 KB Output is correct
15 Correct 93 ms 77844 KB Output is correct
16 Correct 96 ms 77808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 0 ms 344 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 0 ms 344 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 93 ms 125884 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 336 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 90 ms 125824 KB Output is correct
9 Correct 91 ms 125636 KB Output is correct
10 Correct 70 ms 122520 KB Output is correct
11 Correct 155 ms 127704 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 4 ms 3028 KB Output is correct
18 Correct 4 ms 3028 KB Output is correct
19 Correct 95 ms 125776 KB Output is correct
20 Correct 183 ms 130492 KB Output is correct
21 Correct 142 ms 128032 KB Output is correct
22 Correct 117 ms 126780 KB Output is correct
23 Correct 137 ms 83224 KB Output is correct
24 Correct 94 ms 79036 KB Output is correct
25 Correct 93 ms 77844 KB Output is correct
26 Correct 96 ms 77808 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Incorrect 0 ms 344 KB Output isn't correct
29 Halted 0 ms 0 KB -