Submission #567447

# Submission time Handle Problem Language Result Execution time Memory
567447 2022-05-23T13:19:49 Z SavicS Toll (BOI17_toll) C++17
100 / 100
314 ms 32528 KB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ldb;
 
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;

#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for (auto& a : x)
 
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)

const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 200005; 

int K, n, m, q;

vector<pii> g[mxN];
vector<pii> rg[mxN];

int res[mxN];

int dp[mxN][5];
void divi(int l, int r, vector<array<int,5>> upit){
    if(l > r || sz(upit) == 0)return;
    int mid = (l + r) / 2;

    ff(i,l * K, min((r + 1) * K, n) - 1){
        ff(o,0,K - 1)dp[i][o] = inf;
    }

    ff(i,mid * K,min((mid + 1) * K, n) - 1){
        ff(o,0,K - 1)dp[i][o] = inf;
        dp[i][i % K] = 0;
    }

    // desno
    ff(i,mid + 1,r){
        int L = i * K;
        int R = min((i + 1) * K, n) - 1;
        ff(j,L,R){
            for(auto c : rg[j]){
                ff(o,0,K - 1){
                    dp[j][o] = min(dp[j][o], dp[c.fi][o] + c.se);
                }
            }
        }
    }

    // levo
    fb(i,mid - 1,l){
        int L = i * K;
        int R = min((i + 1) * K, n) - 1;
        ff(j,L,R){
            for(auto c : g[j]){
                ff(o,0,K - 1){
                    dp[j][o] = min(dp[j][o], dp[c.fi][o] + c.se);
                }
            }
        }
    }

    vector<array<int,5>> todo[2];
    for(auto c : upit){
        int L = c[0];
        int R = c[1];
        int a = c[2];
        int b = c[3];
        int i = c[4];

        if(R < mid || L > mid){
            todo[L > mid].pb(c);
        }
        else{
            // mogu da odgovorim, sta vise moram
            res[i] = inf;
            ff(o,0,K - 1){
                if(dp[a][o] != inf && dp[b][o] != inf){
                    res[i] = min(res[i], dp[a][o] + dp[b][o]);
                }
            }

            if(res[i] == inf)res[i] = -1;
            
        }

    }

    divi(l, mid - 1, todo[0]); divi(mid + 1, r, todo[1]);

}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    cin >> K >> n >> m >> q; 

    map<pii,int> ima;
    ff(i,1,m){
        int u, v, w;
        cin >> u >> v >> w;
        g[u].pb({v, w});
        rg[v].pb({u, w});
        ima[{u, v}] = w;
    }

    vector<array<int,5>> upit;
    ff(i,1,q){
        int a, b;
        cin >> a >> b;

        int A = a / K, B = b / K;
        if(A == B)res[i] = -1;
        else if(B == A + 1)res[i] = (ima.count({a, b}) == 1 ? ima[{a, b}] : -1);
        else upit.pb({a / K, b / K, a, b, i});
    }

    divi(0, (n - 1) / K, upit);

    ff(i,1,q)cout << res[i] << '\n';

    return 0;
}
/*

5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13

// probati bojenje sahovski
*/
 
 
 
 
 
# Verdict Execution time Memory Grader output
1 Correct 60 ms 17544 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 7 ms 9940 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 70 ms 17576 KB Output is correct
9 Correct 65 ms 17360 KB Output is correct
10 Correct 11 ms 11336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 20536 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 8 ms 10768 KB Output is correct
8 Correct 10 ms 10820 KB Output is correct
9 Correct 67 ms 18624 KB Output is correct
10 Correct 196 ms 27460 KB Output is correct
11 Correct 113 ms 22604 KB Output is correct
12 Correct 91 ms 20384 KB Output is correct
13 Correct 244 ms 28764 KB Output is correct
14 Correct 98 ms 21036 KB Output is correct
15 Correct 88 ms 19160 KB Output is correct
16 Correct 78 ms 19088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9724 KB Output is correct
2 Correct 6 ms 9628 KB Output is correct
3 Correct 7 ms 9684 KB Output is correct
4 Correct 7 ms 9724 KB Output is correct
5 Correct 6 ms 9740 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 10 ms 10252 KB Output is correct
9 Correct 8 ms 10068 KB Output is correct
10 Correct 77 ms 17756 KB Output is correct
11 Correct 140 ms 21248 KB Output is correct
12 Correct 196 ms 26572 KB Output is correct
13 Correct 217 ms 28120 KB Output is correct
14 Correct 155 ms 23884 KB Output is correct
15 Correct 80 ms 18804 KB Output is correct
16 Correct 93 ms 18848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9724 KB Output is correct
2 Correct 6 ms 9628 KB Output is correct
3 Correct 7 ms 9684 KB Output is correct
4 Correct 7 ms 9724 KB Output is correct
5 Correct 6 ms 9740 KB Output is correct
6 Correct 6 ms 9812 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 10 ms 10252 KB Output is correct
9 Correct 8 ms 10068 KB Output is correct
10 Correct 77 ms 17756 KB Output is correct
11 Correct 140 ms 21248 KB Output is correct
12 Correct 196 ms 26572 KB Output is correct
13 Correct 217 ms 28120 KB Output is correct
14 Correct 155 ms 23884 KB Output is correct
15 Correct 80 ms 18804 KB Output is correct
16 Correct 93 ms 18848 KB Output is correct
17 Correct 160 ms 21488 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 6 ms 9720 KB Output is correct
20 Correct 5 ms 9732 KB Output is correct
21 Correct 5 ms 9708 KB Output is correct
22 Correct 5 ms 9688 KB Output is correct
23 Correct 7 ms 10120 KB Output is correct
24 Correct 7 ms 10124 KB Output is correct
25 Correct 10 ms 10380 KB Output is correct
26 Correct 9 ms 10252 KB Output is correct
27 Correct 67 ms 17936 KB Output is correct
28 Correct 191 ms 26816 KB Output is correct
29 Correct 206 ms 28452 KB Output is correct
30 Correct 173 ms 24072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 17544 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 7 ms 9940 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 70 ms 17576 KB Output is correct
9 Correct 65 ms 17360 KB Output is correct
10 Correct 11 ms 11336 KB Output is correct
11 Correct 120 ms 20536 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 7 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 8 ms 10768 KB Output is correct
18 Correct 10 ms 10820 KB Output is correct
19 Correct 67 ms 18624 KB Output is correct
20 Correct 196 ms 27460 KB Output is correct
21 Correct 113 ms 22604 KB Output is correct
22 Correct 91 ms 20384 KB Output is correct
23 Correct 244 ms 28764 KB Output is correct
24 Correct 98 ms 21036 KB Output is correct
25 Correct 88 ms 19160 KB Output is correct
26 Correct 78 ms 19088 KB Output is correct
27 Correct 5 ms 9724 KB Output is correct
28 Correct 6 ms 9628 KB Output is correct
29 Correct 7 ms 9684 KB Output is correct
30 Correct 7 ms 9724 KB Output is correct
31 Correct 6 ms 9740 KB Output is correct
32 Correct 6 ms 9812 KB Output is correct
33 Correct 6 ms 9940 KB Output is correct
34 Correct 10 ms 10252 KB Output is correct
35 Correct 8 ms 10068 KB Output is correct
36 Correct 77 ms 17756 KB Output is correct
37 Correct 140 ms 21248 KB Output is correct
38 Correct 196 ms 26572 KB Output is correct
39 Correct 217 ms 28120 KB Output is correct
40 Correct 155 ms 23884 KB Output is correct
41 Correct 80 ms 18804 KB Output is correct
42 Correct 93 ms 18848 KB Output is correct
43 Correct 160 ms 21488 KB Output is correct
44 Correct 5 ms 9684 KB Output is correct
45 Correct 6 ms 9720 KB Output is correct
46 Correct 5 ms 9732 KB Output is correct
47 Correct 5 ms 9708 KB Output is correct
48 Correct 5 ms 9688 KB Output is correct
49 Correct 7 ms 10120 KB Output is correct
50 Correct 7 ms 10124 KB Output is correct
51 Correct 10 ms 10380 KB Output is correct
52 Correct 9 ms 10252 KB Output is correct
53 Correct 67 ms 17936 KB Output is correct
54 Correct 191 ms 26816 KB Output is correct
55 Correct 206 ms 28452 KB Output is correct
56 Correct 173 ms 24072 KB Output is correct
57 Correct 314 ms 32528 KB Output is correct
58 Correct 63 ms 18548 KB Output is correct
59 Correct 121 ms 22520 KB Output is correct