QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#658178#5669. Traveling in Jade Cityucup-team5226#WA 1323ms67296kbC++207.0kb2024-10-19 16:18:532024-10-19 16:18:53

Judging History

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

  • [2024-10-19 16:18:53]
  • 评测
  • 测评结果:WA
  • 用时:1323ms
  • 内存:67296kb
  • [2024-10-19 16:18:53]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
template <typename T>
struct RangeMinQuery {
   private:
    std::vector<T> data;
    int sz;
    constexpr static T e = T(1LL << 61);
    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 = 1LL << 61;
            auto f1 = [&](ll x) {
                ll res = 1LL << 61;
                if (x <= K) {
                    if (segC.prod(0, x)) {
                        chmin(res, accC[x]);
                    }
                    if (segC.prod(x, N)) {
                        chmin(res, accC[N] - accC[x]);
                    }
                    if (segX.all_plod() && segC.prod(x, K)) {
                        chmin(res, accX.back() + accC[K] - accC[x]);
                    }
                } else if (x < N) {
                    if (segC.prod(0, x)) {
                        chmin(res, accC[x]);
                    }
                    if (segC.prod(x, N)) {
                        chmin(res, accC[N] - accC[x]);
                    }
                    if (segX.all_plod() && segC.prod(K, x)) {
                        chmin(res, accX.back() + accC[x] - accC[K]);
                    }
                } else {
                    if (segX.prod(0, x - N + 1)) {
                        chmin(res, accX[x - N + 1]);
                    }
                    if (segC.prod(0, K) && segX.prod(x - N + 1, M + 1)) {
                        chmin(res, accC[K] + accX[x - N + 1] - accX[M + 1]);
                    }
                    if (segC.prod(K, N) && segX.prod(x - N + 1, M + 1)) {
                        chmin(res, accC.back() - accC[K] + accX[x - N + 1] - accX[M + 1]);
                    }
                }
                // cerr << "f1(" << x << ") = " << res << endl;
                return res;
            };
            auto fK = [&](ll x) {
                ll res = 1LL << 61;
                if (x <= K) {
                    if (segC.prod(x, K)) {
                        chmin(res, accC[K] - accC[x]);
                    }
                    if (segC.prod(0, x) && segC.prod(K, N)) {
                        chmin(res, accC[N] - accC[K] + accC[x]);
                    }
                    if (segX.all_plod() && segC.prod(0, x)) {
                        chmin(res, accX.back() + accC[x]);
                    }
                } else if (x < N) {
                    if (segC.prod(K, x)) {
                        if (chmin(res, accC[x] - accC[K])) {
                            // cerr << "fK(" << x << "): K~x : " << res << endl;
                        }
                    }
                    if (segC.prod(x, N) && segC.prod(0, K)) {
                        if (chmin(res, accC[N] - accC[x] + accC[K])) {
                            // cerr << "fK(" << x << "): K~1~x : " << res << endl;
                        }
                    }
                    if (segX.all_plod() && segC.prod(x, N)) {
                        if (chmin(res, accX.back() + accC[N] - accC[x])) {
                            // cerr << "fK(" << x << "): K-1~x : " << res << endl;
                        }
                    }
                } else {
                    if (segX.prod(x - N + 1, M + 1)) {
                        chmin(res, accX[M + 1] - accX[x - N + 1]);
                    }
                    if (segC.prod(0, K) && segX.prod(0, x - N + 1)) {
                        chmin(res, accC[K] + accX[x - N + 1]);
                    }
                    if (segC.prod(K, N) && segX.prod(0, x - N + 1)) {
                        chmin(res, accC.back() - accC[K] + 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]);
                }
            }
            chmin(ans, f1(u) + f1(v));
            chmin(ans, fK(u) + fK(v));
            if (segX.all_plod()) {
                chmin(ans, f1(u) + fK(v) + accX.back());
                chmin(ans, fK(u) + f1(v) + accX.back());
            }
            if (ans > (1LL << 60)) {
                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: 3544kb

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: 3452kb

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: 1323ms
memory: 67296kb

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
-55133000
-18309200
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
-170704600
impossible
-177836600
impossible
impossible
impossible
-287117800
impossible
-257229400
impossible
-212730400
impossible
-137781200
impossible
impossible
imposs...

result:

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