답안 #719381

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
719381 2023-04-05T21:17:24 Z n0sk1ll Toll (BOI17_toll) C++14
100 / 100
239 ms 22340 KB
#include <bits/stdc++.h>
 
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)
 
using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;
 
 
 
 
 
 
 
//Note to self: Check for overflow
 
struct mat
{
    vector<vector<int>> a;
    int n,m;
 
    mat()
    {
        n=0,m=0;
    }
 
    mat(int _n, int _m)
    {
        n=_n,m=_m;
        vector<int> tmp(m,mod);
        ff(i,0,n) a.pb(tmp);
    }
 
    mat operator* (mat X)
    {
        mat ret(n,X.m);
        ff(i,0,n) ff(j,0,m) ff(k,0,X.m) ret.a[i][k]=min(ret.a[i][k],a[i][j]+X.a[j][k]);
        return ret;
    }
} val[135003];
int l[135003],r[135003];
int stk=1;
 
mat neutral;
mat impo;
 
mat Sta(int p, int ll, int rr)
{
    if (ll>r[p] || rr<l[p]) return neutral;
    if (ll<=l[p] && rr>=r[p]) return val[p];
    return Sta(2*p,ll,rr)*Sta(2*p+1,ll,rr);
}
 
int main()
{
    FAST;
 
    int k,n,m,q;
    cin>>k>>n>>m>>q;
 
    neutral=mat(k,k),impo=neutral;
    ff(i,0,k) neutral.a[i][i]=0;
 
    int stn=(n+k-1)/k;
    while (stk<stn) stk*=2;
    ff(i,0,stk) l[i+stk]=i,r[i+stk]=i;
    bff(i,1,stk) l[i]=l[2*i],r[i]=r[2*i+1];
    ff(i,1,2*stk) val[i]=neutral;
    ff(i,0,stn) val[i+stk]=impo;
 
    while (m--)
    {
        int u,v,w; cin>>u>>v>>w;
        val[u/k+stk].a[u%k][v%k]=w;
    }
 
    bff(i,1,stk) val[i]=val[2*i]*val[2*i+1];
 
    while (q--)
    {
        int s,t; cin>>s>>t;
        mat T=Sta(1,s/k,t/k-1);
        if (s/k==t/k) T=impo;
        mat V(1,k);
        ff(i,0,k) V.a[0][i]=mod;
        V.a[0][s%k]=0;
        V=V*T;
        if (V.a[0][t%k]>1e9) cout<<-1<<"\n";
        else cout<<V.a[0][t%k]<<"\n";
    }
}
 
//Note to self: Check for overflow
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 14780 KB Output is correct
2 Correct 4 ms 4560 KB Output is correct
3 Correct 3 ms 4436 KB Output is correct
4 Correct 3 ms 4556 KB Output is correct
5 Correct 6 ms 4728 KB Output is correct
6 Correct 6 ms 4696 KB Output is correct
7 Correct 6 ms 4692 KB Output is correct
8 Correct 106 ms 14716 KB Output is correct
9 Correct 98 ms 14676 KB Output is correct
10 Correct 74 ms 13888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 14904 KB Output is correct
2 Correct 4 ms 4436 KB Output is correct
3 Correct 3 ms 4556 KB Output is correct
4 Correct 3 ms 4552 KB Output is correct
5 Correct 3 ms 4556 KB Output is correct
6 Correct 3 ms 4564 KB Output is correct
7 Correct 25 ms 4812 KB Output is correct
8 Correct 27 ms 4820 KB Output is correct
9 Correct 69 ms 14648 KB Output is correct
10 Correct 147 ms 22340 KB Output is correct
11 Correct 105 ms 15036 KB Output is correct
12 Correct 127 ms 21236 KB Output is correct
13 Correct 95 ms 13396 KB Output is correct
14 Correct 66 ms 13652 KB Output is correct
15 Correct 93 ms 12112 KB Output is correct
16 Correct 65 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4564 KB Output is correct
2 Correct 2 ms 4552 KB Output is correct
3 Correct 3 ms 4564 KB Output is correct
4 Correct 3 ms 4552 KB Output is correct
5 Correct 3 ms 4436 KB Output is correct
6 Correct 3 ms 4692 KB Output is correct
7 Correct 5 ms 4692 KB Output is correct
8 Correct 5 ms 4820 KB Output is correct
9 Correct 4 ms 4688 KB Output is correct
10 Correct 52 ms 14668 KB Output is correct
11 Correct 45 ms 14668 KB Output is correct
12 Correct 73 ms 22084 KB Output is correct
13 Correct 81 ms 22340 KB Output is correct
14 Correct 68 ms 21696 KB Output is correct
15 Correct 50 ms 12092 KB Output is correct
16 Correct 36 ms 12096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4564 KB Output is correct
2 Correct 2 ms 4552 KB Output is correct
3 Correct 3 ms 4564 KB Output is correct
4 Correct 3 ms 4552 KB Output is correct
5 Correct 3 ms 4436 KB Output is correct
6 Correct 3 ms 4692 KB Output is correct
7 Correct 5 ms 4692 KB Output is correct
8 Correct 5 ms 4820 KB Output is correct
9 Correct 4 ms 4688 KB Output is correct
10 Correct 52 ms 14668 KB Output is correct
11 Correct 45 ms 14668 KB Output is correct
12 Correct 73 ms 22084 KB Output is correct
13 Correct 81 ms 22340 KB Output is correct
14 Correct 68 ms 21696 KB Output is correct
15 Correct 50 ms 12092 KB Output is correct
16 Correct 36 ms 12096 KB Output is correct
17 Correct 76 ms 14820 KB Output is correct
18 Correct 3 ms 4564 KB Output is correct
19 Correct 3 ms 4564 KB Output is correct
20 Correct 4 ms 4564 KB Output is correct
21 Correct 3 ms 4564 KB Output is correct
22 Correct 4 ms 4436 KB Output is correct
23 Correct 13 ms 4752 KB Output is correct
24 Correct 17 ms 4692 KB Output is correct
25 Correct 32 ms 4940 KB Output is correct
26 Correct 24 ms 4696 KB Output is correct
27 Correct 64 ms 14536 KB Output is correct
28 Correct 98 ms 22088 KB Output is correct
29 Correct 113 ms 22336 KB Output is correct
30 Correct 94 ms 21744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 14780 KB Output is correct
2 Correct 4 ms 4560 KB Output is correct
3 Correct 3 ms 4436 KB Output is correct
4 Correct 3 ms 4556 KB Output is correct
5 Correct 6 ms 4728 KB Output is correct
6 Correct 6 ms 4696 KB Output is correct
7 Correct 6 ms 4692 KB Output is correct
8 Correct 106 ms 14716 KB Output is correct
9 Correct 98 ms 14676 KB Output is correct
10 Correct 74 ms 13888 KB Output is correct
11 Correct 98 ms 14904 KB Output is correct
12 Correct 4 ms 4436 KB Output is correct
13 Correct 3 ms 4556 KB Output is correct
14 Correct 3 ms 4552 KB Output is correct
15 Correct 3 ms 4556 KB Output is correct
16 Correct 3 ms 4564 KB Output is correct
17 Correct 25 ms 4812 KB Output is correct
18 Correct 27 ms 4820 KB Output is correct
19 Correct 69 ms 14648 KB Output is correct
20 Correct 147 ms 22340 KB Output is correct
21 Correct 105 ms 15036 KB Output is correct
22 Correct 127 ms 21236 KB Output is correct
23 Correct 95 ms 13396 KB Output is correct
24 Correct 66 ms 13652 KB Output is correct
25 Correct 93 ms 12112 KB Output is correct
26 Correct 65 ms 12160 KB Output is correct
27 Correct 3 ms 4564 KB Output is correct
28 Correct 2 ms 4552 KB Output is correct
29 Correct 3 ms 4564 KB Output is correct
30 Correct 3 ms 4552 KB Output is correct
31 Correct 3 ms 4436 KB Output is correct
32 Correct 3 ms 4692 KB Output is correct
33 Correct 5 ms 4692 KB Output is correct
34 Correct 5 ms 4820 KB Output is correct
35 Correct 4 ms 4688 KB Output is correct
36 Correct 52 ms 14668 KB Output is correct
37 Correct 45 ms 14668 KB Output is correct
38 Correct 73 ms 22084 KB Output is correct
39 Correct 81 ms 22340 KB Output is correct
40 Correct 68 ms 21696 KB Output is correct
41 Correct 50 ms 12092 KB Output is correct
42 Correct 36 ms 12096 KB Output is correct
43 Correct 76 ms 14820 KB Output is correct
44 Correct 3 ms 4564 KB Output is correct
45 Correct 3 ms 4564 KB Output is correct
46 Correct 4 ms 4564 KB Output is correct
47 Correct 3 ms 4564 KB Output is correct
48 Correct 4 ms 4436 KB Output is correct
49 Correct 13 ms 4752 KB Output is correct
50 Correct 17 ms 4692 KB Output is correct
51 Correct 32 ms 4940 KB Output is correct
52 Correct 24 ms 4696 KB Output is correct
53 Correct 64 ms 14536 KB Output is correct
54 Correct 98 ms 22088 KB Output is correct
55 Correct 113 ms 22336 KB Output is correct
56 Correct 94 ms 21744 KB Output is correct
57 Correct 239 ms 15856 KB Output is correct
58 Correct 94 ms 14708 KB Output is correct
59 Correct 124 ms 14980 KB Output is correct