Submission #628040

# Submission time Handle Problem Language Result Execution time Memory
628040 2022-08-13T03:51:03 Z BackNoob Toll (BOI17_toll) C++14
100 / 100
167 ms 29048 KB
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
const int mxN = 2e5 + 227;
const int inf = 1e9 + 7;
 
int n;
int m;
int q;
int k;
vector<pair<int, int>> adj[mxN];
int idx[mxN];
int lvl[mxN];
 
struct SegmentTree{
    struct Node{
        int f[5][5];
        int lef;
        int rig;
    };
    int n;
    vector<Node> t;
    Node base;
 
    SegmentTree(){}
    SegmentTree(int _n){
        n = _n;
        t.resize(n * 4 + 7);
    }
 
    Node mergenode(Node &x, Node &y)
    {
        if(x.lef == -1) return y;
        if(y.lef == -1) return x;
        Node res;
        res.lef = x.lef;
        res.rig = y.rig;
 
        // a - > b - > c -> d
        // b -> c
 
        for(int i = 0; i < k; i++)
        for(int j = 0; j < k; j++) res.f[i][j] = inf;
 
        for(int a = 0; a < k; a++)
        for(int b = 0; b < k; b++) {
            if(x.f[a][b] == inf) continue;
            for(auto it : adj[x.rig * k + b]) {
                int c = idx[it.fi];
                int cost = it.se;
                for(int d = 0; d < k; d++) {
                    if(y.f[c][d] == inf) continue;
                    res.f[a][d] = min(res.f[a][d], x.f[a][b] + cost + y.f[c][d]);
                }
            }
        }
        return res;
    }
 
    void build(int v, int tl, int tr)
    {
        if(tl == tr) {
            t[v].lef = t[v].rig = tl - 1;
 
            for(int i = 0; i < k; i++)
            for(int j = 0; j < k; j++)
                if(i == j) t[v].f[i][j] = 0;
                else t[v].f[i][j] = inf;
        }
        else {
            int tm = (tl + tr) / 2;
            build(v * 2, tl, tm);
            build(v * 2 + 1, tm + 1, tr);
            t[v] = mergenode(t[v * 2], t[v * 2 + 1]);
        }
    }
 
    Node query(int v, int tl, int tr, int l, int r)
    {
        if(l > r) return base;
        if(tl == l && tr == r) return t[v];
        else {
            int tm = (tl + tr) / 2;
            Node m1 = query(v * 2, tl, tm, l, min(r, tm));
            Node m2 = query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
            Node rr = mergenode(m1, m2);
            return rr;
        }
    }
    int query(int l, int r, int x, int y)
    {
        Node res = query(1, 1, n, l, r);
        return res.f[x][y];
    }
} seg;
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // freopen("task.inp", "r", stdin);
    // freopen("task.out", "w", stdout);
 
    cin >> k >> n >> m >> q;
    for(int i = 1; i <= m; i++) {
        int u, v, c;
        cin >> u >> v >> c;
        adj[u].pb({v, c});
        // adj[v].pb({u, c});
    }
 
    for(int i = 0; i < n; i++) {
        lvl[i] = i / k;
        idx[i] = i % k;
    }
 
    seg = SegmentTree(n / k + 7);
    seg.build(1, 1, n / k + 7);
    seg.base.lef = -1;
 
    while(q--) {
        int u, v;
        cin >> u >> v;
        //cout << lvl[u] << ' ' << lvl[v] << ' ' << idx[u] << ' ' << idx[v] << endl;
        int res = seg.query(lvl[u] + 1, lvl[v] + 1, idx[u], idx[v]);
        if(res == inf) cout << -1 << '\n';
        else cout << res << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 52 ms 28172 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 5432 KB Output is correct
6 Correct 5 ms 5460 KB Output is correct
7 Correct 4 ms 5420 KB Output is correct
8 Correct 51 ms 28968 KB Output is correct
9 Correct 66 ms 28844 KB Output is correct
10 Correct 30 ms 26676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 17572 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 8 ms 5460 KB Output is correct
8 Correct 9 ms 5332 KB Output is correct
9 Correct 43 ms 28116 KB Output is correct
10 Correct 78 ms 14932 KB Output is correct
11 Correct 72 ms 17592 KB Output is correct
12 Correct 47 ms 13944 KB Output is correct
13 Correct 74 ms 10520 KB Output is correct
14 Correct 43 ms 11212 KB Output is correct
15 Correct 49 ms 9080 KB Output is correct
16 Correct 46 ms 9120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 5032 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 5460 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 5 ms 5204 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 44 ms 28888 KB Output is correct
11 Correct 40 ms 19048 KB Output is correct
12 Correct 54 ms 16996 KB Output is correct
13 Correct 63 ms 17636 KB Output is correct
14 Correct 48 ms 16320 KB Output is correct
15 Correct 38 ms 10188 KB Output is correct
16 Correct 34 ms 10280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 5032 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 5460 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 5 ms 5204 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 44 ms 28888 KB Output is correct
11 Correct 40 ms 19048 KB Output is correct
12 Correct 54 ms 16996 KB Output is correct
13 Correct 63 ms 17636 KB Output is correct
14 Correct 48 ms 16320 KB Output is correct
15 Correct 38 ms 10188 KB Output is correct
16 Correct 34 ms 10280 KB Output is correct
17 Correct 46 ms 19108 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 5028 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 7 ms 5552 KB Output is correct
24 Correct 6 ms 5332 KB Output is correct
25 Correct 21 ms 5204 KB Output is correct
26 Correct 12 ms 5204 KB Output is correct
27 Correct 41 ms 28876 KB Output is correct
28 Correct 67 ms 17100 KB Output is correct
29 Correct 67 ms 17664 KB Output is correct
30 Correct 55 ms 16304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 28172 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 5432 KB Output is correct
6 Correct 5 ms 5460 KB Output is correct
7 Correct 4 ms 5420 KB Output is correct
8 Correct 51 ms 28968 KB Output is correct
9 Correct 66 ms 28844 KB Output is correct
10 Correct 30 ms 26676 KB Output is correct
11 Correct 50 ms 17572 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 8 ms 5460 KB Output is correct
18 Correct 9 ms 5332 KB Output is correct
19 Correct 43 ms 28116 KB Output is correct
20 Correct 78 ms 14932 KB Output is correct
21 Correct 72 ms 17592 KB Output is correct
22 Correct 47 ms 13944 KB Output is correct
23 Correct 74 ms 10520 KB Output is correct
24 Correct 43 ms 11212 KB Output is correct
25 Correct 49 ms 9080 KB Output is correct
26 Correct 46 ms 9120 KB Output is correct
27 Correct 3 ms 4948 KB Output is correct
28 Correct 4 ms 5032 KB Output is correct
29 Correct 3 ms 4948 KB Output is correct
30 Correct 3 ms 4948 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 3 ms 5460 KB Output is correct
33 Correct 3 ms 5204 KB Output is correct
34 Correct 5 ms 5204 KB Output is correct
35 Correct 4 ms 5204 KB Output is correct
36 Correct 44 ms 28888 KB Output is correct
37 Correct 40 ms 19048 KB Output is correct
38 Correct 54 ms 16996 KB Output is correct
39 Correct 63 ms 17636 KB Output is correct
40 Correct 48 ms 16320 KB Output is correct
41 Correct 38 ms 10188 KB Output is correct
42 Correct 34 ms 10280 KB Output is correct
43 Correct 46 ms 19108 KB Output is correct
44 Correct 3 ms 4948 KB Output is correct
45 Correct 3 ms 4948 KB Output is correct
46 Correct 3 ms 5028 KB Output is correct
47 Correct 3 ms 4948 KB Output is correct
48 Correct 3 ms 4948 KB Output is correct
49 Correct 7 ms 5552 KB Output is correct
50 Correct 6 ms 5332 KB Output is correct
51 Correct 21 ms 5204 KB Output is correct
52 Correct 12 ms 5204 KB Output is correct
53 Correct 41 ms 28876 KB Output is correct
54 Correct 67 ms 17100 KB Output is correct
55 Correct 67 ms 17664 KB Output is correct
56 Correct 55 ms 16304 KB Output is correct
57 Correct 167 ms 16844 KB Output is correct
58 Correct 53 ms 29048 KB Output is correct
59 Correct 61 ms 19248 KB Output is correct