Submission #1112265

# Submission time Handle Problem Language Result Execution time Memory
1112265 2024-11-14T00:40:37 Z azberjibiou Wild Boar (JOI18_wild_boar) C++17
100 / 100
4983 ms 646820 KB
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define lb lower_bound
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
using namespace std;
typedef long long ll;
typedef array<ll, 3> p3;
typedef array<p3, 5> node;
/* 
    d[0] : 최단 경로
    d[1] : d[0]의 시작 간선이 불가능할 때 최단경로
    d[2] : d[0]의 시작 간선과 d[1]의 끝 간선이 불가능할 때 최단경로
    d[3] : d[0]의 끝 간선이 불가능할 때 최단경로
    d[4] : d[0]의 끝 간선과 d[3]의 시작 간선이 불가능할 때 최단경로
    각 경로에 대해서 (길이, 시작 간선, 끝 간선)으로 저장
    경로가 없으면 -1
*/
const int mxN=2050;
const int mxQ=100100;
const int MOD=998244353;
const ll INF=1e18; // max = 2MCL = 4e17
p3 mn(p3 a, p3 b){return a[0]<b[0] ? a : b;}
int N, M, Q, L;
node mrg(node a, node b){
    node no, res;
    for(int i=0;i<5;i++) no[i][0]=-1, res[i][0]=INF;
    if(a[0][0]==-1 || b[0][0]==-1) return no;
    array <ll, 2> cond;
    array <ll, 3> tmp;
    for(int i=0;i<5;i++){
        if(i==2 && res[1][0]==INF) continue;
        if(i==4 && res[3][0]==INF) continue;
        cond={-1, -1}, tmp={INF, 0, 0};
        // set cond
        if(i==1 || i==2) cond[0]=res[0][1];
        if(i==2) cond[1]=res[1][2];
        if(i==3 || i==4) cond[1]=res[0][2];
        if(i==4) cond[0]=res[3][1];
        // find shortest path
        for(int j=0;j<5;j++) if(a[j][0]!=-1){
            if(a[j][1]==cond[0]) continue;
            if(abs(b[0][1]-a[j][2])!=M){
                if(b[0][2]!=cond[1]) tmp={a[j][0]+b[0][0], a[j][1], b[0][2]};
                else if(b[3][0]!=-1 && abs(b[3][1]-a[j][2])!=M) tmp={a[j][0]+b[3][0], a[j][1], b[3][2]};
                else if(b[4][0]!=-1) tmp={a[j][0]+b[4][0], a[j][1], b[4][2]};
            }
            else{
                if(b[1][0]!=-1 && b[1][2]!=cond[1]) tmp={a[j][0]+b[1][0], a[j][1], b[1][2]};
                else if(b[2][0]!=-1) tmp={a[j][0]+b[2][0], a[j][1], b[2][2]};
            }
            res[i]=mn(res[i], tmp);
        }
        if(res[i][0]==INF) res[i][0]=-1;
    }
    return res;
}
p3 E[mxN];
pii eidx[mxN]; // i번째 edge가 v[a]에서 몇번째인지
vector <p3> v[mxN];
vector <pll> nv[6*mxN];
int A[mxQ];
pii qry[mxQ];
int pdeg[mxN];
ll dist[2*mxN][2*mxN];
pii se[2*mxN][2*mxN];
node D[mxN][mxN];
void input(){
    cin >> N >> M >> Q >> L;
    for(int i=0;i<M;i++){
        int a, b, c;
        cin >> a >> b >> c;
        eidx[i]=pii(v[a].size(), v[b].size());
        v[a].push_back({b, c, i});
        v[b].push_back({a, c, i});
        E[i][0]=a, E[i][1]=b, E[i][2]=c;
    }
    for(int i=1;i<=L;i++) cin >> A[i];
    for(int i=1;i<=Q;i++) cin >> qry[i].fi >> qry[i].se;
}
void makeGraph(){
    // 0~2M-1 : edge
    // 2M-1~4M-1 : prefix of edges
    // 4M~6M-1 : suffix of edges
    for(int i=1;i<=N;i++) pdeg[i]=v[i].size();
    for(int i=2;i<=N;i++) pdeg[i]+=pdeg[i-1];
    for(int i=1;i<=N;i++){
        int sz=v[i].size();
        int st=pdeg[i-1];
        for(int j=0;j<sz;j++){
            // i에서 j번째로 나가는 간선 : s -> e, 비용 x, 간선 번호 idx
            // 역간선은 e에서 ipos번째로 나가는 간선
            int s=i;
            auto [e, x, idx] = v[i][j];
            int now = s<e ? idx : idx+M;
            int ipos = (s<e ? eidx[idx].se : eidx[idx].fi);

            // real edge에서 나가는 간선
            if(ipos!=0) nv[now].emplace_back(2*M+pdeg[e-1]+ipos-1, 0);
            if(ipos!=v[e].size()-1) nv[now].emplace_back(4*M+pdeg[e-1]+ipos+1, 0);

            // prefix 만들기
            // 2M+st+j -> 2M+st+j-1
            // 2M+st+j -> v[i][j]
            if(j!=0) nv[2*M+st+j].emplace_back(2*M+st+j-1, 0);
            nv[2*M+st+j].emplace_back(now, x);

            // suffix 만들기
            // 4M+st+j -> 4M+st+j+1
            // 4M+st+j -> v[i][j]
            if(j!=sz-1) nv[4*M+st+j].emplace_back(4*M+st+j+1, 0);
            nv[4*M+st+j].emplace_back(now, x);
        }
    }
    /*
    for(int i=0;i<6*M;i++){
        printf("%d: ", i);
        for(auto [nxt, x] : nv[i]) printf("(%lld %lld) ", nxt, x);
        printf("\n");
    }
    */
}
ll d[6*mxN];
void dijk(int st){
    for(int i=0;i<6*M;i++) d[i]=INF;
    ll pred=0;
    priority_queue <pll> pq;
    queue <int> que;
    que.push(st);
    while(pq.size() || que.size()){
        int now;
        ll x;
        if(que.size()){
            now=que.front();
            x=pred;
            que.pop();
        }
        else{
            now=pq.top().se;
            x=-pq.top().fi;
            pq.pop();
        }
        if(d[now]!=INF) continue;
        d[now]=x;
        pred=x;
        for(auto [nxt, val] : nv[now]){
            if(val==0) que.push(nxt);
            else pq.emplace(-x-val, nxt);
        }
    }
    for(int i=0;i<2*M;i++) dist[st][i]=d[i];

}
void solvDist(){
    for(int i=0;i<2*M;i++){
        dijk(i);
    }
    /*
    for(int i=0;i<2*M;i++){
        for(int j=0;j<2*M;j++){
            printf("dist[%d][%d]=%lld\n", i, j, dist[i][j]);
        }
    }
    */
}
void print(node a){
    for(int i=0;i<5;i++){
        printf("%d: ", i);
        for(int j=0;j<3;j++){
            printf("%lld ", a[i][j]);
        }
        printf("\n");
    }
}
void makeD(){
    for(int z=0;z<5;z++){
        for(int i=1;i<=N;i++) for(int j=1;j<=N;j++){
            if(i==j) continue;
            D[i][j][z][0]=INF;
            for(auto [e1, x1, idx1] : v[i]) for(auto [s2, x2, idx2] : v[j]){
                int s1=i, e2=j;
                int i1=(s1<e1 ? idx1 : idx1+M), i2=(s2<e2 ? idx2 : idx2+M);
                bool ok=true;
                if(1<=z && z<=2 && D[i][j][0][1]==i1) ok=false;
                if(z==2 && D[i][j][1][2]==i2) ok=false;
                if(3<=z && z<=4 && D[i][j][0][2]==i2) ok=false;
                if(z==4 && D[i][j][3][1]==i1) ok=false;
                if(ok) D[i][j][z]=mn(D[i][j][z], p3{dist[i1][i2]+E[idx1][2], i1, i2});
                //if(ok) printf("i=%d, j=%d, i1=%d, i2=%d\n", i, j, i1, i2);
                //if(ok) printf("D[%d][%d][%d][0]=%lld\n", i, j, z, D[i][j][z][0]);
            }
            if(D[i][j][z][0]==INF) D[i][j][z][0]=-1;
        }
    }
    /*
    for(int i=1;i<=N;i++) for(int j=1;j<=N;j++){
        printf("%d %d: \n", i, j);
        print(D[i][j]);
    }
    */
}
node seg[4*mxQ];
void init(int idx, int s, int e){
    if(s==e){
        seg[idx]=D[A[s]][A[s+1]];
        //printf("s=%d, e=%d\n", s, e);
        //print(seg[idx]);
        return;
    }
    int mid=(s+e)/2;
    init(2*idx, s, mid);
    init(2*idx+1, mid+1, e);
    seg[idx]=mrg(seg[2*idx], seg[2*idx+1]);
    //printf("s=%d, e=%d\n", s, e);
    //print(seg[idx]);
}
void upd(int idx, int s, int e, int pos){
    if(s==e){
        seg[idx]=D[A[s]][A[s+1]];
        return;
    }
    int mid=(s+e)/2;
    if(pos<=mid) upd(2*idx, s, mid, pos);
    else upd(2*idx+1, mid+1, e, pos);
    seg[idx]=mrg(seg[2*idx], seg[2*idx+1]);
}
void sweep(){
    init(1, 1, L-1);
    for(int i=1;i<=Q;i++){
        A[qry[i].fi]=qry[i].se;
        if(qry[i].fi!=L) upd(1, 1, L-1, qry[i].fi);
        if(qry[i].fi!=1) upd(1, 1, L-1, qry[i].fi-1);
        cout << seg[1][0][0] << '\n';
    }
}
int main(){
    gibon
    input();
    makeGraph();
    solvDist();
    makeD();
    sweep();
}

Compilation message

wild_boar.cpp: In function 'void makeGraph()':
wild_boar.cpp:104:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |             if(ipos!=v[e].size()-1) nv[now].emplace_back(4*M+pdeg[e-1]+ipos+1, 0);
      |                ~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 9008 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 2 ms 8952 KB Output is correct
9 Correct 2 ms 8784 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8784 KB Output is correct
12 Correct 2 ms 8784 KB Output is correct
13 Correct 2 ms 8784 KB Output is correct
14 Correct 2 ms 8784 KB Output is correct
15 Correct 3 ms 8784 KB Output is correct
16 Correct 2 ms 8784 KB Output is correct
17 Correct 2 ms 8784 KB Output is correct
18 Correct 2 ms 8784 KB Output is correct
19 Correct 2 ms 10832 KB Output is correct
20 Correct 2 ms 10832 KB Output is correct
21 Correct 2 ms 8784 KB Output is correct
22 Correct 2 ms 8784 KB Output is correct
23 Correct 2 ms 8784 KB Output is correct
24 Correct 2 ms 8784 KB Output is correct
25 Correct 2 ms 8784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 9008 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 2 ms 8952 KB Output is correct
9 Correct 2 ms 8784 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8784 KB Output is correct
12 Correct 2 ms 8784 KB Output is correct
13 Correct 2 ms 8784 KB Output is correct
14 Correct 2 ms 8784 KB Output is correct
15 Correct 3 ms 8784 KB Output is correct
16 Correct 2 ms 8784 KB Output is correct
17 Correct 2 ms 8784 KB Output is correct
18 Correct 2 ms 8784 KB Output is correct
19 Correct 2 ms 10832 KB Output is correct
20 Correct 2 ms 10832 KB Output is correct
21 Correct 2 ms 8784 KB Output is correct
22 Correct 2 ms 8784 KB Output is correct
23 Correct 2 ms 8784 KB Output is correct
24 Correct 2 ms 8784 KB Output is correct
25 Correct 2 ms 8784 KB Output is correct
26 Correct 3 ms 15184 KB Output is correct
27 Correct 21 ms 68944 KB Output is correct
28 Correct 20 ms 68944 KB Output is correct
29 Correct 116 ms 81428 KB Output is correct
30 Correct 101 ms 81416 KB Output is correct
31 Correct 99 ms 81252 KB Output is correct
32 Correct 99 ms 81224 KB Output is correct
33 Correct 102 ms 93512 KB Output is correct
34 Correct 111 ms 93456 KB Output is correct
35 Correct 100 ms 93512 KB Output is correct
36 Correct 99 ms 93512 KB Output is correct
37 Correct 107 ms 93524 KB Output is correct
38 Correct 112 ms 103752 KB Output is correct
39 Correct 109 ms 105740 KB Output is correct
40 Correct 98 ms 105748 KB Output is correct
41 Correct 103 ms 105768 KB Output is correct
42 Correct 88 ms 111872 KB Output is correct
43 Correct 93 ms 116412 KB Output is correct
44 Correct 90 ms 116228 KB Output is correct
45 Correct 64 ms 118088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 9008 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 2 ms 8952 KB Output is correct
9 Correct 2 ms 8784 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8784 KB Output is correct
12 Correct 2 ms 8784 KB Output is correct
13 Correct 2 ms 8784 KB Output is correct
14 Correct 2 ms 8784 KB Output is correct
15 Correct 3 ms 8784 KB Output is correct
16 Correct 2 ms 8784 KB Output is correct
17 Correct 2 ms 8784 KB Output is correct
18 Correct 2 ms 8784 KB Output is correct
19 Correct 2 ms 10832 KB Output is correct
20 Correct 2 ms 10832 KB Output is correct
21 Correct 2 ms 8784 KB Output is correct
22 Correct 2 ms 8784 KB Output is correct
23 Correct 2 ms 8784 KB Output is correct
24 Correct 2 ms 8784 KB Output is correct
25 Correct 2 ms 8784 KB Output is correct
26 Correct 3 ms 15184 KB Output is correct
27 Correct 21 ms 68944 KB Output is correct
28 Correct 20 ms 68944 KB Output is correct
29 Correct 116 ms 81428 KB Output is correct
30 Correct 101 ms 81416 KB Output is correct
31 Correct 99 ms 81252 KB Output is correct
32 Correct 99 ms 81224 KB Output is correct
33 Correct 102 ms 93512 KB Output is correct
34 Correct 111 ms 93456 KB Output is correct
35 Correct 100 ms 93512 KB Output is correct
36 Correct 99 ms 93512 KB Output is correct
37 Correct 107 ms 93524 KB Output is correct
38 Correct 112 ms 103752 KB Output is correct
39 Correct 109 ms 105740 KB Output is correct
40 Correct 98 ms 105748 KB Output is correct
41 Correct 103 ms 105768 KB Output is correct
42 Correct 88 ms 111872 KB Output is correct
43 Correct 93 ms 116412 KB Output is correct
44 Correct 90 ms 116228 KB Output is correct
45 Correct 64 ms 118088 KB Output is correct
46 Correct 221 ms 89160 KB Output is correct
47 Correct 4166 ms 204440 KB Output is correct
48 Correct 3907 ms 248632 KB Output is correct
49 Correct 3657 ms 287824 KB Output is correct
50 Correct 3783 ms 288004 KB Output is correct
51 Correct 3579 ms 287896 KB Output is correct
52 Correct 4141 ms 288208 KB Output is correct
53 Correct 3937 ms 288128 KB Output is correct
54 Correct 4103 ms 288112 KB Output is correct
55 Correct 3976 ms 288208 KB Output is correct
56 Correct 3986 ms 313096 KB Output is correct
57 Correct 3664 ms 340440 KB Output is correct
58 Correct 3703 ms 370280 KB Output is correct
59 Correct 3549 ms 402276 KB Output is correct
60 Correct 3396 ms 436876 KB Output is correct
61 Correct 3321 ms 474024 KB Output is correct
62 Correct 3053 ms 512608 KB Output is correct
63 Correct 2674 ms 554208 KB Output is correct
64 Correct 1732 ms 643912 KB Output is correct
65 Correct 1783 ms 643956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8784 KB Output is correct
2 Correct 2 ms 8784 KB Output is correct
3 Correct 2 ms 8784 KB Output is correct
4 Correct 2 ms 9008 KB Output is correct
5 Correct 2 ms 8784 KB Output is correct
6 Correct 2 ms 8784 KB Output is correct
7 Correct 2 ms 8784 KB Output is correct
8 Correct 2 ms 8952 KB Output is correct
9 Correct 2 ms 8784 KB Output is correct
10 Correct 2 ms 8784 KB Output is correct
11 Correct 2 ms 8784 KB Output is correct
12 Correct 2 ms 8784 KB Output is correct
13 Correct 2 ms 8784 KB Output is correct
14 Correct 2 ms 8784 KB Output is correct
15 Correct 3 ms 8784 KB Output is correct
16 Correct 2 ms 8784 KB Output is correct
17 Correct 2 ms 8784 KB Output is correct
18 Correct 2 ms 8784 KB Output is correct
19 Correct 2 ms 10832 KB Output is correct
20 Correct 2 ms 10832 KB Output is correct
21 Correct 2 ms 8784 KB Output is correct
22 Correct 2 ms 8784 KB Output is correct
23 Correct 2 ms 8784 KB Output is correct
24 Correct 2 ms 8784 KB Output is correct
25 Correct 2 ms 8784 KB Output is correct
26 Correct 3 ms 15184 KB Output is correct
27 Correct 21 ms 68944 KB Output is correct
28 Correct 20 ms 68944 KB Output is correct
29 Correct 116 ms 81428 KB Output is correct
30 Correct 101 ms 81416 KB Output is correct
31 Correct 99 ms 81252 KB Output is correct
32 Correct 99 ms 81224 KB Output is correct
33 Correct 102 ms 93512 KB Output is correct
34 Correct 111 ms 93456 KB Output is correct
35 Correct 100 ms 93512 KB Output is correct
36 Correct 99 ms 93512 KB Output is correct
37 Correct 107 ms 93524 KB Output is correct
38 Correct 112 ms 103752 KB Output is correct
39 Correct 109 ms 105740 KB Output is correct
40 Correct 98 ms 105748 KB Output is correct
41 Correct 103 ms 105768 KB Output is correct
42 Correct 88 ms 111872 KB Output is correct
43 Correct 93 ms 116412 KB Output is correct
44 Correct 90 ms 116228 KB Output is correct
45 Correct 64 ms 118088 KB Output is correct
46 Correct 221 ms 89160 KB Output is correct
47 Correct 4166 ms 204440 KB Output is correct
48 Correct 3907 ms 248632 KB Output is correct
49 Correct 3657 ms 287824 KB Output is correct
50 Correct 3783 ms 288004 KB Output is correct
51 Correct 3579 ms 287896 KB Output is correct
52 Correct 4141 ms 288208 KB Output is correct
53 Correct 3937 ms 288128 KB Output is correct
54 Correct 4103 ms 288112 KB Output is correct
55 Correct 3976 ms 288208 KB Output is correct
56 Correct 3986 ms 313096 KB Output is correct
57 Correct 3664 ms 340440 KB Output is correct
58 Correct 3703 ms 370280 KB Output is correct
59 Correct 3549 ms 402276 KB Output is correct
60 Correct 3396 ms 436876 KB Output is correct
61 Correct 3321 ms 474024 KB Output is correct
62 Correct 3053 ms 512608 KB Output is correct
63 Correct 2674 ms 554208 KB Output is correct
64 Correct 1732 ms 643912 KB Output is correct
65 Correct 1783 ms 643956 KB Output is correct
66 Correct 29 ms 46664 KB Output is correct
67 Correct 455 ms 219720 KB Output is correct
68 Correct 1601 ms 612492 KB Output is correct
69 Correct 1537 ms 613436 KB Output is correct
70 Correct 1620 ms 645192 KB Output is correct
71 Correct 4983 ms 199956 KB Output is correct
72 Correct 4404 ms 251356 KB Output is correct
73 Correct 4753 ms 290760 KB Output is correct
74 Correct 4594 ms 290600 KB Output is correct
75 Correct 4717 ms 292612 KB Output is correct
76 Correct 4326 ms 290180 KB Output is correct
77 Correct 4302 ms 313416 KB Output is correct
78 Correct 4381 ms 292400 KB Output is correct
79 Correct 4691 ms 343236 KB Output is correct
80 Correct 4410 ms 375612 KB Output is correct
81 Correct 3751 ms 404464 KB Output is correct
82 Correct 4094 ms 439400 KB Output is correct
83 Correct 3663 ms 475644 KB Output is correct
84 Correct 3521 ms 556908 KB Output is correct
85 Correct 2233 ms 646820 KB Output is correct