QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#84073#5669. Traveling in Jade Cityskittles1412AC ✓2724ms154272kbC++174.8kb2023-03-05 08:47:402023-03-05 08:47:43

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-05 08:47:43]
  • 评测
  • 测评结果:AC
  • 用时:2724ms
  • 内存:154272kb
  • [2023-03-05 08:47:40]
  • 提交

answer

#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
    cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t << " | ";
    dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
         << ": ";                                          \
    dbgh(__VA_ARGS__)
#else
#define cerr   \
    if (false) \
    cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

constexpr int maxm = 2e6 + 5;
//constexpr int maxm = 1e4 + 5;

struct PSA {
    vector<int> psum;

    PSA(const vector<int>& arr) : psum(sz(arr) + 1) {
        partial_sum(begin(arr), end(arr), psum.begin() + 1);
    }

    int query(int l, int r) const {
        return psum[r] - psum[l];
    }
};

struct DS {
    int n, nind[maxm], eind[maxm];
    PSA ew;
    set<int> bad;
    bool blast = false;

    DS(const vector<int>& arr, const vector<pair<int, int>>& edges)
        : n(sz(arr)), ew({}) {
        memset(nind, -1, sizeof(nind));
        memset(eind, -1, sizeof(eind));
        vector<int> ews;
        for (int i = 0; i < n; i++) {
            nind[arr[i]] = i;
            auto& [u, w] = edges[i];
            eind[u] = i;
            ews.push_back(w);
        }
        ew = ews;
    }

    void toggle_edge(int e) {
        e = eind[e];
        if (e == -1) {
            return;
        }
        auto [it, inserted] = bad.insert(e);
        if (!inserted) {
            bad.erase(it);
        }
        blast ^= e == n - 1;
    }

    long query(int u, int v) {
        auto dist = [&](int u, int v) -> long {
            auto it = bad.lower_bound(u);
            if (it != bad.end() && *it < v) {
                return 1e18;
            }
            return ew.query(u, v);
        };
        u = nind[u];
        v = nind[v];
        if (u == -1 || v == -1) {
            return 1e18;
        }
        if (u > v) {
            swap(u, v);
        }
        long ans = dist(u, v);
        if (!blast) {
            ans = min(ans, dist(v, n - 1) + ew.query(n - 1, n) + dist(0, u));
        }
        return ans;
    }
};

void solve() {
    int n, m, kv, q;
    cin >> n >> kv >> m >> q;
    kv--;
    int arrw[n + m + 1];
    for (auto& a : arrw) {
        cin >> a;
    }
    vector<DS> dss;
    auto map_edges = [&](const vector<int>& edges) -> vector<pair<int, int>> {
        vector<pair<int, int>> ans;
        for (auto& a : edges) {
            ans.emplace_back(a, arrw[a]);
        }
        return ans;
    };
    {
        vector<int> nodes, edges;
        for (int i = 0; i < n; i++) {
            nodes.push_back(i);
            edges.push_back(i);
        }
        dss.emplace_back(nodes, map_edges(edges));
    }
    {
        vector<int> nodes {0}, edges {n};
        for (int i = 0; i < m; i++) {
            nodes.push_back(n + i);
            edges.push_back(n + i + 1);
        }
        for (int i = kv; i < n; i++) {
            nodes.push_back(i);
            edges.push_back(i);
        }
        dss.emplace_back(nodes, map_edges(edges));
    }
    {
        vector<int> nodes, edges;
        for (int i = 0; i < kv; i++) {
            nodes.push_back(i);
            edges.push_back(i);
        }
        nodes.push_back(kv);
        edges.push_back(n + m);
        for (int i = m - 1; i >= 0; i--) {
            nodes.push_back(n + i);
            edges.push_back(n + i);
        }
        dss.emplace_back(nodes, map_edges(edges));
    }
    while (q--) {
        char ty;
        cin >> ty;
        if (ty == 'q') {
            auto dist = [&](int u, int v) -> long {
                long ans = 1e18;
                for (auto& ds : dss) {
                    ans = min(ans, ds.query(u, v));
                }
                return ans;
            };
            int u, v;
            cin >> u >> v;
            u--;
            v--;
            long ans = dist(u, v);
            ans = min(ans, dist(u, 0) + dist(0, v));
            if (ans >= long(1e18)) {
                cout << "impossible" << endl;
            } else {
                cout << ans << endl;
            }
        } else if (ty == 'c') {
            int u;
            cin >> u;
            u--;
            for (auto& ds : dss) {
                ds.toggle_edge(u);
            }
        } else if (ty == 'x') {
            int u;
            cin >> u;
            for (auto& ds : dss) {
                ds.toggle_edge(u + n);
            }
        }
    }
}

int main() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 81172kb

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: 36ms
memory: 81124kb

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: 2208ms
memory: 154272kb

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: 2054ms
memory: 103960kb

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: 2724ms
memory: 148680kb

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: 2610ms
memory: 134784kb

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: 2162ms
memory: 114228kb

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: 2423ms
memory: 111024kb

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: 701ms
memory: 148728kb

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: 783ms
memory: 134680kb

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: 732ms
memory: 154220kb

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: 615ms
memory: 148652kb

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: 596ms
memory: 134780kb

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: 566ms
memory: 154268kb

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: 2388ms
memory: 148648kb

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: 2299ms
memory: 134740kb

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