Submission #957295

# Submission time Handle Problem Language Result Execution time Memory
957295 2024-04-03T12:21:59 Z amine_aroua Toll (BOI17_toll) C++17
100 / 100
103 ms 13288 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define int long long
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int,int>>
#define vll vector<pair<long long,long long>>
#define pb push_back
#define ll long long
#define ld long double
#define nl '\n'
#define boost ios::sync_with_stdio(false)
#define mp make_pair
#define se second
#define fi first
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i,x,y) for(int i = x;i<=y;i++)
#define forn(i,y,x) for(int i = y; i >= x; i--)
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define clr(v,k) memset(v,k,sizeof(v))
#define rall(v) v.rbegin() , v.rend()
#define pii pair<int,int>
#define pll pair<ll , ll>
#define vvi vector<vi>
#define vvii vector<vii>

const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 1;

ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (gcd)
ll lcm(ll a , ll b) {return a * (b / gcd(a , b));} // least common multiple (lcm)

// HERE IS THE SOLUTION
int N , K , M , O;
const int LOG = 15;
int lim;
vector<vi> comb(vector<vi> a , vector<vi> b)
{
    vector<vi> target(K , vi(K , INF));
    fore(x , K)
    {
        fore(y , K)
        {
            fore(z , K)
            {
                target[x][y] = min(target[x][y]  , a[x][z] + b[z][y]);
            }
        }
    }
    return target;
}
vector<vvi> tree;
vvi query(int l , int r)
{
    l+=lim;
    r+=lim;
    if(l == r)
    {
        return tree[l];
    }
    vvi resL = tree[l] , resR = tree[r];
    while(l + 1< r)
    {
        if(l%2 == 0)
        {
            resL = comb(resL , tree[l + 1]);
        }
        if(r%2 == 1)
        {
            resR = comb(tree[r - 1] , resR);
        }
        l>>=1;
        r>>=1;
    }
    return comb(resL ,resR);
}

signed main()
{
    boost;
    cin.tie(0);
    cout.tie(0);
    cin>>K>>N>>M>>O;
    lim = (N+ K - 1)/K;
    while(__builtin_popcount(lim) != 1)
    {
        lim++;
    }
    tree.assign(2*lim , vvi(K , vi(K , INF)));
    fore(i , M)
    {
        int a , b ,t;
        cin>>a>>b>>t;
        tree[lim + (a/K)][a%K][b%K] = min(tree[lim + (a/K)][a%K][b%K] , t);
    }
    forn(i , lim - 1 , 1)
    {
        tree[i] = comb(tree[2*i] , tree[2*i + 1]);
    }
    while(O--)
    {
        int a , b;
        cin>>a>>b;
        if(a/K == b/K)
        {
            cout<<-1<<nl;
            continue;
        }
        int ans = query(a/K , (b/K) - 1)[a%K][b%K];
        if(ans == INF)
            cout<<-1<<nl;
        else
            cout<<ans<<nl;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 49 ms 11608 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 51 ms 11752 KB Output is correct
9 Correct 50 ms 11612 KB Output is correct
10 Correct 40 ms 11612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 10252 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 18 ms 604 KB Output is correct
8 Correct 23 ms 604 KB Output is correct
9 Correct 55 ms 11608 KB Output is correct
10 Correct 97 ms 13144 KB Output is correct
11 Correct 70 ms 10212 KB Output is correct
12 Correct 88 ms 13284 KB Output is correct
13 Correct 62 ms 6748 KB Output is correct
14 Correct 42 ms 6744 KB Output is correct
15 Correct 67 ms 6748 KB Output is correct
16 Correct 58 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 27 ms 11736 KB Output is correct
11 Correct 33 ms 10076 KB Output is correct
12 Correct 45 ms 13148 KB Output is correct
13 Correct 63 ms 13144 KB Output is correct
14 Correct 42 ms 13144 KB Output is correct
15 Correct 23 ms 6744 KB Output is correct
16 Correct 24 ms 6744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 27 ms 11736 KB Output is correct
11 Correct 33 ms 10076 KB Output is correct
12 Correct 45 ms 13148 KB Output is correct
13 Correct 63 ms 13144 KB Output is correct
14 Correct 42 ms 13144 KB Output is correct
15 Correct 23 ms 6744 KB Output is correct
16 Correct 24 ms 6744 KB Output is correct
17 Correct 41 ms 10208 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 6 ms 600 KB Output is correct
24 Correct 6 ms 600 KB Output is correct
25 Correct 12 ms 652 KB Output is correct
26 Correct 9 ms 604 KB Output is correct
27 Correct 36 ms 11632 KB Output is correct
28 Correct 58 ms 13288 KB Output is correct
29 Correct 60 ms 13192 KB Output is correct
30 Correct 59 ms 13148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 11608 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 51 ms 11752 KB Output is correct
9 Correct 50 ms 11612 KB Output is correct
10 Correct 40 ms 11612 KB Output is correct
11 Correct 73 ms 10252 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 18 ms 604 KB Output is correct
18 Correct 23 ms 604 KB Output is correct
19 Correct 55 ms 11608 KB Output is correct
20 Correct 97 ms 13144 KB Output is correct
21 Correct 70 ms 10212 KB Output is correct
22 Correct 88 ms 13284 KB Output is correct
23 Correct 62 ms 6748 KB Output is correct
24 Correct 42 ms 6744 KB Output is correct
25 Correct 67 ms 6748 KB Output is correct
26 Correct 58 ms 6748 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 344 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 27 ms 11736 KB Output is correct
37 Correct 33 ms 10076 KB Output is correct
38 Correct 45 ms 13148 KB Output is correct
39 Correct 63 ms 13144 KB Output is correct
40 Correct 42 ms 13144 KB Output is correct
41 Correct 23 ms 6744 KB Output is correct
42 Correct 24 ms 6744 KB Output is correct
43 Correct 41 ms 10208 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 6 ms 600 KB Output is correct
50 Correct 6 ms 600 KB Output is correct
51 Correct 12 ms 652 KB Output is correct
52 Correct 9 ms 604 KB Output is correct
53 Correct 36 ms 11632 KB Output is correct
54 Correct 58 ms 13288 KB Output is correct
55 Correct 60 ms 13192 KB Output is correct
56 Correct 59 ms 13148 KB Output is correct
57 Correct 103 ms 10988 KB Output is correct
58 Correct 49 ms 11608 KB Output is correct
59 Correct 78 ms 10072 KB Output is correct