QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#288500#5669. Traveling in Jade CityVinhLuuAC ✓1924ms218812kbC++237.4kb2023-12-22 19:23:292023-12-22 19:23:30

Judging History

你现在查看的是最新测评结果

  • [2023-12-22 19:23:30]
  • 评测
  • 测评结果:AC
  • 用时:1924ms
  • 内存:218812kb
  • [2023-12-22 19:23:29]
  • 提交

answer

#include <bits/stdc++.h>
//#define int long long
//#define pb push_back
using namespace std;

typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 2e6 + 5;
const int oo = 3e8;

char Q[N];
int U[N], V[N];

int n, mx, k, m, q, gc[N], gx[N], f[N], c[N], x[N], sum, s[N], R[N], g[N], TX, t[N], NG;
vector<tp> p[N];

int st[5][N << 2], lz[5][N << 2];

void dolz(int j,int i){
    if(lz[j][i] == 0) return;
    int x = lz[j][i];
    lz[j][i] = 0;
    lz[j][i << 1] += x;
    lz[j][i << 1|1] += x;
    st[j][i << 1] += x;
    st[j][i << 1|1] += x;
}

void update(int j,int i,int l,int r,int u,int v,int x){
    if(l > r || l > v || r < u) return;
    if(u <= l && r <= v) {
        st[j][i] += x;
        lz[j][i] += x;
        return;
    }
    dolz(j, i);
    int mid = (l + r) >> 1;
    update(j, i << 1, l, mid, u, v, x);
    update(j, i << 1|1, mid + 1, r, u, v, x);
}

int get(int j,int i,int l,int r,int u){
    if(l > r || l > u || r < u || u == 0 || u == n) return 0;
    if(l == r) return st[j][i];
    dolz(j, i);
    int mid = (l + r) >> 1;
    return get(j, i << 1, l, mid, u) + get(j, i << 1|1, mid + 1, r, u);
}

int C(int u,int v){
    if(u == v) return 0;
    if(u > v) swap(u, v);
    int L1 = (get(0, 1, 1, mx, v - 1) - get(0, 1, 1, mx, u - 1) != 0 ? oo : s[v] - s[u]);
    int L2 = (g[n] || NG - (get(0, 1, 1, mx, v - 1) - get(0, 1, 1, mx, u - 1)) != 0 ? oo : sum - (s[v] - s[u]));
//    cout << u << " " << v << " " << L1 << " " << L2 << " " << get(0, 1, 1, mx, u - 1) << " r\n";
    return min(L1, L2);
}

int X(int u,int v){
    if(u == v) return 0;
    if(R[u] > R[v]) swap(u, v);
    if(u == 1 && v == k){
        if(gx[0] || gx[m]) return oo;
        if(get(1, 1, 1, mx, n + m - 1) - get(1, 1, 1, mx, n) != 0) return oo;
        return TX;
    }
    if(u == 1){
        if(get(1, 1, 1, mx, v - 1) - get(1, 1, 1, mx, n) != 0 || gx[0] == true) return oo;
        return t[v] - t[u];
    }
    if(v == k){
        if(get(1, 1, 1, mx, n + m - 1) - get(1, 1, 1, mx, u - 1) != 0 || gx[m] == true) return oo;
        return t[v] - t[u];
    }
    if(get(1, 1, 1, mx, v - 1) - get(1, 1, 1, mx, u - 1) != 0) return oo;
    return t[v] - t[u];
}

void sub3(){

    mx = n + m;

    for(int i = 2; i <= n; i ++) s[i] = s[i - 1] + c[i - 1];

    R[1] = 0;
    int cnt = 0;
    for(int i = n + 1; i <= n + m; i ++){
        g[i] = 1;
        R[i] = ++cnt;
        t[i] = t[i - 1] + x[i - n - 1];
    }
    R[k] = ++cnt;
    t[k] = t[n + m] + x[m];

    for(int j = 1; j <= q; j ++){
        char type = Q[j];
        if(type == 'q'){
            int u = U[j], v = V[j];
            int kq = oo;
            if(g[u] == g[v]){
                if(g[u] == 1){
                    kq = min({X(u, v), X(u, 1) + X(v, k) + C(1, k), C(1, k) + X(u, k) + X(v, 1)});
                }else{
                    kq = min({C(u, v), C(1, u) + C(k, v) + X(1, k), C(k, u) + C(1, v) + X(1, k)});
                }
            }else{
                if(g[u] == 1){
                    kq = min({X(1, u) + C(1, v), X(k, u) + C(k, v)}) ;
                }else{
                    kq = min({X(k, v) + C(k, u), X(1, v) + C(1, u)});
                }
            }
            if(kq >= oo) cout << "impossible\n";
            else cout << kq << "\n";
        }else if(type == 'c'){
            int i = U[j];
            if(gc[i] == 0){
                NG++;
                update(0, 1, 1, mx, i, n, 1);
                gc[i] = 1;
            }else{
                NG--;
                gc[i] = 0;
                update(0, 1, 1, mx, i, n, -1);
            }
        }else{
            int i = U[j];
            if(gx[i] == 0){
                if(i != 0 && i != m) update(1, 1, 1, mx, n + i, n + m, 1);
                gx[i] = 1;
            }else{
                gx[i] = 0;
                if(i != 0 && i != m) update(1, 1, 1, mx, n + i, n + m, -1);
            }
        }
    }
}

int dij(int u,int v){
    for(int i = 1; i <= n + m; i ++) f[i] = oo;
    priority_queue<pii,vector<pii>,greater<pii>> pq;
    pq.push({f[u] = 0, u});
    while(!pq.empty()){
        int w, u; tie(w, u) = pq.top();
        pq.pop();
        if(w > f[u]) continue;
        for(auto tt : p[u]){
            int j, v, e; tie(j, v, e) = tt;
            if(e == 0){
                if(gc[v] == 1) continue;
                if(f[j] > w + c[v]) pq.push({f[j] = w + c[v], j});
            }else{
                if(gx[v] == 1) continue;
                if(f[j] > w + x[v]) pq.push({f[j] = w + x[v], j});
            }
        }
    }
    if(f[v] == oo) return -1;
    else return f[v];
}

void sub1(){
    while(q--){
        char type; cin >> type;
        if(type == 'q'){
            int u, v; cin >> u >> v;
            int what = dij(u, v);
            if(what == -1) cout << "impossible\n";
            else cout << what << "\n";
        }else if(type == 'c'){
            int i; cin >> i;
            gc[i] = 1 - gc[i];
        }else{
            int i; cin >> i;
            gx[i] = 1 - gx[i];
        }
    }
}

int calc(int u,int v){
    if(u == v) return 0;
    if(u > v) swap(u, v);
    return min(s[v] - s[u], sum - (s[v] - s[u]));
}

int calx(int u,int v){
    if(u == v) return 0;
    if(R[u] > R[v]) swap(u, v);
    return t[v] - t[u];
}

void sub2(){

    for(int i = 2; i <= n; i ++) s[i] = s[i - 1] + c[i - 1];

    R[1] = 0;
    int cnt = 0;
    for(int i = n + 1; i <= n + m; i ++){
        g[i] = 1;
        R[i] = ++cnt;
        t[i] = t[i - 1] + x[i - n - 1];
    }
    R[k] = ++cnt;
    t[k] = t[n + m] + x[m];

   for(int i = 1; i <= q; i ++){

        char type = Q[i];
        int u = U[i], v = V[i];
        if(g[u] == g[v]){
            if(g[u] == 1){
                cout << min({calx(u, v), calx(u, 1) + calx(v, k) + calc(1, k), calc(1, k) + calx(u, k) + calx(v, 1)}) << "\n";
            }else{
                cout << min({calc(u, v), calc(1, u) + calc(k, v) + TX, calc(k, u) + calc(1, v) + TX}) << "\n";
            }
        }else{
            if(g[u] == 1){
                cout << min({calx(1, u) + calc(1, v), calx(k, u) + calc(k, v)}) << "\n";
            }else{
                cout << min({calx(k, v) + calc(k, u), calx(1, v) + calc(1, u)}) << "\n";
            }
        }
    }
}


signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if(fopen("train.inp","r")){
        freopen("train.inp","r",stdin);
        freopen("train.out","w",stdout);
    }
    cin >> n >> k >> m >> q;
    for(int i = 1; i <= n; i ++){
        cin >> c[i];
        sum += c[i];
        #define pb push_back
        p[i].push_back({i % n + 1, i, 0});
        p[i % n + 1].pb({i, i, 0});
    }
    for(int i = 0; i <= m; i ++){
        cin >> x[i];
        TX += x[i];
        if(i == 0){
            p[1].pb({n + 1, i, 1});
            p[n + 1].pb({1, i, 1});
        }
        else if(i == m){
            p[n + m].pb({k, i, 1});
            p[k].pb({n + m, i, 1});
        }
        else{
            p[n + i].pb({n + i + 1, i, 1});
            p[n + i + 1].pb({n + i, i, 1});
        }
    }
//    if(n <= 2000 && q <= 2000 && k <= 2000 && m <= 2000){
//        sub1();
//        return 0;
//    }
    bool ff = true;
    for(int i = 1; i <= q; i ++){
        cin >> Q[i];
        if(Q[i] == 'q') cin >> U[i] >> V[i];
        else{
            ff = false;
            cin >> U[i];
        }
    }
    sub3();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 67084kb

input:

4 3 1 9
2 3 8 4
1 1
q 3 4
x 0
q 3 4
c 3
q 3 4
c 1
q 3 4
x 0
q 3 4

output:

6
8
9
impossible
6

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 7ms
memory: 65012kb

input:

4 3 1 9
2 3 8 4
1 1
q 3 4
x 0
q 3 4
c 3
q 3 4
c 1
q 3 4
x 0
q 3 4

output:

6
8
9
impossible
6

result:

ok 5 lines

Test #3:

score: 0
Accepted
time: 1314ms
memory: 198240kb

input:

1000000 999999 1000000 1000000
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 2...

output:

177406400
122264600
70328400
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
29295200
impossible
22163200
impossible
impossible
impossible
11422200
impossible
62579800
impossible
35339400
impossible
20157200
impossible
impossible
impossible
impossib...

result:

ok 500003 lines

Test #4:

score: 0
Accepted
time: 1056ms
memory: 133788kb

input:

100000 25123 500000 1000000
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 ...

output:

4243800
37968200
35427000
52078200
5074200
70821200
13901400
13614600
8774800
68958800
49822200
31077400
impossible
45392400
48946000
76885400
37532600
34416200
impossible
20744200
71652000
21288000
7501600
70315400
14721800
impossible
12981800
81039600
9506800
impossible
33487200
53520200
impossibl...

result:

ok 500004 lines

Test #5:

score: 0
Accepted
time: 1541ms
memory: 218812kb

input:

1000000 543210 999999 1000000
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 20...

output:

43962400
803800
153423600
impossible
impossible
impossible
impossible
impossible
191566200
impossible
impossible
impossible
impossible
84524200
impossible
8790000
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
86439200
20798400
impossibl...

result:

ok 666666 lines

Test #6:

score: 0
Accepted
time: 1563ms
memory: 211884kb

input:

999999 2 1000000 1000000
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200...

output:

103577000
40419800
44334400
117613400
27695600
101675800
93767600
impossible
impossible
41683400
58145400
impossible
impossible
38841000
impossible
impossible
60723200
impossible
impossible
impossible
35102200
360200
impossible
64912000
48484000
impossible
impossible
impossible
impossible
impossible...

result:

ok 666666 lines

Test #7:

score: 0
Accepted
time: 1379ms
memory: 147876kb

input:

10 5 1000000 1000000
200 200 200 200 200 200 200 200 200 200
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200...

output:

94819200
65730000
72994800
49117800
104138800
186947800
44801800
49390200
165897000
78473600
43124000
7660200
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
imp...

result:

ok 666666 lines

Test #8:

score: 0
Accepted
time: 1924ms
memory: 152396kb

input:

1000000 371220 10 1000000
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 20...

output:

20563800
35454600
impossible
impossible
impossible
787600
impossible
34108400
impossible
impossible
impossible
18207600
impossible
impossible
impossible
55803600
impossible
impossible
2024800
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossibl...

result:

ok 833321 lines

Test #9:

score: 0
Accepted
time: 1838ms
memory: 163900kb

input:

1000000 543210 1000000 1000000
55 172 71 52 118 20 70 172 64 49 84 89 145 22 84 186 131 52 18 28 59 73 88 52 82 11 75 157 71 55 184 129 87 109 153 139 121 184 37 96 123 102 186 99 191 116 28 45 198 166 103 164 171 149 66 193 65 110 191 123 51 138 100 146 139 129 168 127 125 55 178 27 80 87 101 21 13...

output:

30670961
48913371
7774973
27843322
64930666
19787521
32236183
33937440
21950724
29313510
69061178
48818521
12208541
65243879
41433227
67580303
14884583
56319432
47932125
69665033
29946609
71525011
9854513
34362272
26512727
21612559
10235684
36689531
31170755
61421169
9720654
34948295
29798373
623856...

result:

ok 1000000 lines

Test #10:

score: 0
Accepted
time: 1847ms
memory: 157528kb

input:

1000000 2 1000000 1000000
100 127 50 199 57 114 160 124 165 155 33 154 123 91 19 71 64 123 45 151 166 164 129 191 112 27 141 18 2 57 138 161 78 144 194 35 35 75 67 58 13 43 190 134 189 17 173 181 106 179 122 62 95 121 46 20 73 132 67 45 8 169 34 185 84 67 37 167 40 74 106 1 136 60 151 96 138 186 7 1...

output:

55941508
37798287
42008436
15042425
39982088
386496
4884841
46962447
36087996
53611931
55134253
33845621
29760296
83855351
50503009
8571638
9529701
60699338
60672059
38265301
23699094
45110337
18969905
13079954
70485346
18147717
92355546
63022315
42678137
18708201
27186314
21036147
38401966
52389150...

result:

ok 1000000 lines

Test #11:

score: 0
Accepted
time: 1829ms
memory: 163840kb

input:

1000000 999999 1000000 1000000
85 186 187 158 55 167 113 77 157 200 191 98 86 163 150 190 73 123 41 57 195 12 90 31 147 38 185 159 37 18 36 169 7 194 171 0 7 194 132 41 172 71 79 104 123 111 28 79 182 183 68 10 138 157 172 62 121 114 120 161 145 103 23 49 130 12 64 195 138 91 187 151 118 125 76 27 7...

output:

13489183
31135048
27765845
7900657
2121940
629199
43535831
17309804
30184842
28161975
51347708
84517299
44153340
20933265
65976605
45362434
68344874
28396225
56606740
59137812
33734547
37006720
51189172
47429362
65030264
49797155
6134840
71465610
42976326
23944443
7691098
36442345
49070551
49199373
...

result:

ok 1000000 lines

Test #12:

score: 0
Accepted
time: 1296ms
memory: 197788kb

input:

1000000 543210 1000000 1000000
137 99 98 193 46 124 140 117 70 135 164 100 171 122 191 48 40 200 7 77 14 81 118 30 81 34 23 10 161 167 196 90 2 50 49 87 37 122 189 138 121 197 136 38 127 98 56 142 159 171 108 164 182 157 86 184 13 173 70 101 4 139 44 149 91 58 188 103 131 119 110 29 133 119 145 47 2...

output:

23018826
4232727
68737556
47572442
112566841
50136715
3484804
38100504
10744409
impossible
19681253
29910453
impossible
impossible
impossible
impossible
impossible
impossible
28093345
27225933
1768859
120769627
37514339
17905742
15661811
44404605
38736310
55328501
45088710
53035483
61459784
48357562...

result:

ok 499733 lines

Test #13:

score: 0
Accepted
time: 1291ms
memory: 203536kb

input:

1000000 2 1000000 1000000
35 163 110 68 40 141 191 144 117 31 68 56 96 147 162 40 29 170 168 61 93 109 99 94 82 184 190 160 169 33 153 200 157 180 58 101 177 136 197 129 2 36 37 21 180 80 70 57 160 29 149 112 80 63 40 185 196 97 112 101 158 136 108 62 87 72 111 142 179 80 158 69 108 146 115 76 158 1...

output:

1510974
34176489
24428849
53679059
28247032
34748061
44791635
12572651
35359520
14906746
8359501
25638338
63934318
impossible
impossible
impossible
14602405
impossible
impossible
impossible
impossible
14612398
impossible
31431308
impossible
65183851
66229738
11284891
impossible
19660789
54661830
267...

result:

ok 499682 lines

Test #14:

score: 0
Accepted
time: 1274ms
memory: 201460kb

input:

1000000 999999 1000000 1000000
132 188 185 84 49 87 48 43 141 128 98 20 163 76 66 171 84 49 15 196 141 98 106 188 155 98 130 89 85 167 35 80 0 132 43 112 23 67 143 14 165 111 101 159 22 79 142 128 37 180 57 100 47 44 156 25 66 168 5 192 149 182 75 63 159 120 23 61 146 34 26 179 82 107 96 109 4 1 5 5...

output:

46463849
40534360
41086190
37304061
58942042
23166991
14348824
69723956
49777712
7487019
109114020
41235300
137548894
41500520
impossible
2068501
6597550
31310053
48381630
26335454
19269986
20351533
36142189
31875914
33417005
33985956
20593003
impossible
57402024
52010429
40443037
10020959
43025286
...

result:

ok 499132 lines

Test #15:

score: 0
Accepted
time: 1302ms
memory: 199752kb

input:

1000000 543210 1000000 1000000
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 2...

output:

17513600
16528400
impossible
70980800
impossible
210032800
193761600
103759000
impossible
impossible
128696400
impossible
impossible
impossible
66319400
194986200
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
2912800
73051000
impossible
214394200
impossible
...

result:

ok 500003 lines

Test #16:

score: 0
Accepted
time: 1305ms
memory: 191720kb

input:

1000000 2 1000000 1000000
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 20...

output:

91870000
64635800
103695000
impossible
impossible
9130200
impossible
impossible
impossible
impossible
impossible
52899800
impossible
impossible
impossible
impossible
impossible
impossible
37007000
impossible
impossible
7473800
impossible
43119800
impossible
35068400
6268800
35519800
impossible
57900...

result:

ok 500003 lines