QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#658265#5669. Traveling in Jade Cityucup-team5226#WA 996ms67168kbC++205.3kb2024-10-19 16:30:212024-10-19 16:30:24

Judging History

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

  • [2024-10-19 16:30:24]
  • 评测
  • 测评结果:WA
  • 用时:996ms
  • 内存:67168kb
  • [2024-10-19 16:30:21]
  • 提交

answer

#include <bits/stdc++.h>

constexpr long long INF = 1LL << 50;
using namespace std;
template <typename T>
struct RangeMinQuery {
   private:
    std::vector<T> data;
    int sz;
    constexpr static T e = T(INF);
    void update(int i) {
        data[i] = std::min(data[2 * i], data[2 * i + 1]);
    }

   public:
    RangeMinQuery(int n) {
        assert(n >= 1);
        sz = 1;
        while (sz < n) {
            sz <<= 1;
        }
        data.assign(2 * sz, e);
    }
    RangeMinQuery(std::vector<T> init) : RangeMinQuery((int)init.size()) {
        for (int i = 0; i < (int)init.size(); i++) {
            data[sz + i] = init[i];
        }
        for (int i = sz - 1; i >= 1; i--) {
            update(i);
        }
    }
    T get(int i) const {
        assert(0 <= i && i < sz);
        return data[i + sz];
    }
    void set(int i, T x) {
        assert(0 <= i && i < sz);
        i += sz;
        data[i] = x;
        i >>= 1;
        while (i) {
            update(i);
            i >>= 1;
        }
    }
    T prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= sz);
        l += sz;
        r += sz;
        T sml = e, smr = e;
        while (l < r) {
            if (l & 1) sml = std::min(sml, data[l++]);
            if (r & 1) smr = std::min(data[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return std::min(sml, smr);
    }
    T all_plod() const {
        return data[1];
    }
};

using namespace std;
using ll = long long;
using vl = vector<ll>;
using str = string;
#define reps(i, a, n) for (ll i = (a); i < ll(n); i++)
#define rep(i, n) reps(i, 0, n)

bool chmin(ll& a, ll b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

void solve() {
    ll N, K, M, Q;
    cin >> N >> K >> M >> Q;
    K--;
    RangeMinQuery<ll> segC(vl(N, 1));
    RangeMinQuery<ll> segX(vl(M + 1, 1));
    vl C(N);
    vl X(M + 1);
    for (auto& a : C) cin >> a;
    for (auto& a : X) cin >> a;
    vl accC(C.size() + 1);
    rep(i, C.size()) accC[i + 1] = C[i] + accC[i];
    vl accX(X.size() + 1);
    rep(i, X.size()) accX[i + 1] = X[i] + accX[i];
    while (Q--) {
        char q;
        cin >> q;
        if (q == 'q') {
            ll u, v;
            cin >> u >> v;
            u--;
            v--;
            if (u > v) std::swap(u, v);
            ll ans = INF;
            auto f1 = [&](ll x) {
                ll res = INF;
                if (x <= K) {
                    if (segC.prod(0, x)) {
                        chmin(res, accC[x]);
                    }
                } else if (x < N) {
                    if (segC.prod(x, N)) {
                        chmin(res, accC[N] - accC[x]);
                    }
                } else {
                    if (segX.prod(0, x - N + 1)) {
                        chmin(res, accX[x - N + 1]);
                    }
                }
                // cerr << "f1(" << x << ") = " << res << endl;
                return res;
            };
            auto fK = [&](ll x) {
                ll res = INF;
                if (x <= K) {
                    if (segC.prod(x, K)) {
                        chmin(res, accC[K] - accC[x]);
                    }
                } else if (x < N) {
                    if (segC.prod(K, x)) {
                        chmin(res, accC[x] - accC[K]);
                    }
                } else {
                    if (segX.prod(x - N + 1, M + 1)) {
                        chmin(res, accX[M + 1] - accX[x - N + 1]);
                    }
                }
                // cerr << "fK(" << x << ") = " << res << endl;
                return res;
            };
            if (u < N && v < N) {
                if (segC.prod(u, v)) {
                    chmin(ans, accC[v] - accC[u]);
                }
            }
            if (u >= N && v >= N) {
                if (segX.prod(u - N + 1, v - N + 1)) {
                    chmin(ans, accX[v - N + 1] - accX[u - N + 1]);
                }
            }
            const ll f1u = f1(u);
            const ll f1v = f1(v);
            const ll fKu = fK(u);
            const ll fKv = fK(v);
            chmin(ans, f1u + f1v);
            chmin(ans, fKv + fKv);
            if (segX.all_plod()) {
                chmin(ans, f1u + fKv + accX.back());
                chmin(ans, fKu + f1v + accX.back());
            }
            if (segC.prod(0, K)) {
                chmin(ans, f1u + fKv + accC[K]);
                chmin(ans, fKu + f1v + accC[K]);
            }
            if (segC.prod(K, N)) {
                chmin(ans, f1u + fKv + accC.back() - accC[K]);
                chmin(ans, fKu + f1v + accC.back() - accC[K]);
            }
            if (ans > (1LL << 40)) {
                cout << "impossible" << endl;
            } else {
                cout << ans << endl;
            }
        } else if (q == 'c') {
            ll c;
            cin >> c;
            c--;
            segC.set(c, segC.get(c) ^ 1);
        } else if (q == 'x') {
            ll x;
            cin >> x;
            segX.set(x, segX.get(x) ^ 1);
        } else {
            assert(false);
        }
    }
}

int main() {
    ll t = 1;
    // cin >> t;
    rep(i, t) solve();
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3612kb

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: 0ms
memory: 3580kb

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: -100
Wrong Answer
time: 996ms
memory: 67168kb

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
144866800
70328400
5884000
impossible
impossible
impossible
243059200
77857600
155079200
139435600
41242000
29295200
326024400
22163200
219201200
40877200
98338800
11422200
186296800
62579800
398861200
35339400
impossible
20157200
250730400
390558800
310668000
impossible
93805600
73560200
...

result:

wrong answer 2nd lines differ - expected: '122264600', found: '144866800'