QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#104414#5098. 第一代图灵机bashkort0 111ms3812kbC++204.8kb2023-05-10 17:14:362023-05-10 17:14:36

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-10 17:14:36]
  • Judged
  • Verdict: 0
  • Time: 111ms
  • Memory: 3812kb
  • [2023-05-10 17:14:36]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr ll inf = 3e18;

namespace SegmentTree {
    vector<int> t;
    int sz = 1;

    void init(int n, vector<int> nxt) {
        sz = 1 << __lg(n) + !!(n & (n - 1));
        t.assign(sz << 1, 1e9);
        for (int i = 0; i < n; ++i) {
            t[i + sz] = nxt[i];
        }
        for (int i = sz - 1; i > 0; --i) {
            t[i] = min(t[i << 1], t[i << 1 | 1]);
        }
    }

    void modify(int x, int v) {
        t[x += sz] = v;
        while (x >>= 1) {
            t[x] = min(t[x << 1], t[x << 1 | 1]);
        }
    }

    int rangeMin(int l, int r) {
        int ans = 1e9;
        for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
            if (l & 1) ans = min(ans, t[l++]);
            if (r & 1) ans = min(ans, t[--r]);
        }
        return ans;
    }

    int descend(int mn) { //find max i such as nxt[i] < mn
        if (t[1] >= mn) {
            return -1;
        }

        int x = 1;

        while (x < sz) {
            if (t[x << 1 | 1] < mn) {
                x = x << 1 | 1;
            } else {
                x = x << 1;
            }
        }

        return x - sz;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, q;
    cin >> n >> m >> q;

    vector<ll> pref(n + 1);

    for (int i = 1; i <= n; ++i) {
        cin >> pref[i];
        pref[i] += pref[i - 1];
    }

    vector<int> c(n), nxt(n, n), last(m, -1);
    vector<set<int>> st(m);

    for (int i = 0; i < m; ++i) {
        st[i].insert(-1);
        st[i].insert(n);
    }

    for (int i = 0; i < n; ++i) {
        cin >> c[i];
        c[i] -= 1;
        st[c[i]].insert(i);

        if (last[c[i]] != -1) {
            nxt[last[c[i]]] = i;
        }
        last[c[i]] = i;
    }

    SegmentTree::init(n, nxt);

    constexpr int B = 500;

    vector<ll> blockPref(n);
    vector<int> blockBest(n);

    const int S = (n + B - 1) / B;
    vector<vector<int>> blockStk(S);

    auto update = [&](int b) { //update block b
        int l = b * B, r = min(n, (b + 1) * B);
        if (l >= n) {
            return;
        }

        vector<int> &stk = blockStk[b];
        stk.clear();

        for (int i = r - 1; i >= l; --i) {
            while (!stk.empty() && nxt[stk.back()] > nxt[i]) {
                stk.pop_back();
            }
            stk.push_back(i);
        }

        for (int i = l; i < r; ++i) {
            blockBest[i] = -1;
            blockPref[i] = -1;
        }

        ll ans = 0;
        for (int i = 0; i < stk.size(); ++i) {
            blockBest[stk[i]] = stk[0];
            if (i > 0) {
                ans = max(ans, pref[nxt[stk[i]]] - pref[stk[i - 1] + 1]);
            }
            blockPref[stk[i]] = ans;
        }
    };

    auto modify = [&](int x, int to) {
        nxt[x] = to;
        SegmentTree::modify(x, to);
        update(x / B);
    };

    auto replace = [&](int x, int col) {
        if (c[x] == col) {
            return;
        }

        st[c[x]].erase(x);
        st[col].insert(x);

        int p = *prev(st[c[x]].lower_bound(x));
        int np = *prev(st[col].lower_bound(x));
        int nx = *st[col].upper_bound(x);
        int npx = *st[c[x]].upper_bound(p);

        if (p < np) {
            if (p != -1) {
                modify(p, npx);
            }
            if (np != -1) {
                modify(np, x);
            }
        } else {
            if (np != -1) {
                modify(np, x);
            }
            if (p != -1) {
                modify(p, npx);
            }
        }
        modify(x, nx);

        c[x] = col;
    };

    auto query = [&](int l, int r) { // [l, r)
        int i = SegmentTree::descend(r);
        if (i < l) {
            return pref[r] - pref[l];
        }
        assert(SegmentTree::rangeMin(i, n) == nxt[i]);

        ll ans = pref[r] - pref[i + 1];
        r = i;

        while (r - l >= B) {
//            assert(blockBest[r] != -1);
            ans = max(ans, blockPref[r]);
            i = SegmentTree::descend(nxt[r]);
            if (i + 1 <= l) {
                ans = max(ans, pref[nxt[r]] - pref[i + 1]);
            }
            r = i;
        }

        while (r >= l) {
            ans = max(ans, pref[SegmentTree::rangeMin(r, n)] - pref[r]);
            r -= 1;
        }

        return ans;
    };

    for (int i = 0; i * B < n; ++i) {
        update(i);
    }

    while (q--) {
        int tp, a, b;
        cin >> tp >> a >> b;
        a -= 1, b -= 1;

        if (tp == 1) {
            cout << query(a, b + 1) << '\n';
        } else {
            replace(a, b);
        }
    }

    return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 111ms
memory: 3812kb

input:

5000 200 5000
2315 3433 1793 4621 4627 4561 289 4399 3822 2392 392 4581 2643 2441 4572 4649 2981 3094 4206 2057 761 2516 2849 3509 3033 658 4965 3316 3269 4284 4961 753 1187 2515 1377 1725 4743 4761 3823 3464 4859 989 2401 953 875 1481 2181 103 2067 2625 3296 4721 61 3843 1607 997 4385 1284 4299 441...

output:

100567
90725
79596
79590
79590
94493
72411
90725
99923
99923
100567
90725
100567
90725
90725
102564
94493
95154
58191
81248
99923
100567
87252
39374
89208
89208
99923
89208
87252
95724
87252
85822
89208
90725
90725
87252
99923
100567
90725
26617
90725
79596
99923
94493
100567
89208
99923
100567
6073...

result:

wrong answer 1st lines differ - expected: '118571', found: '100567'

Subtask #2:

score: 0
Time Limit Exceeded

Test #3:

score: 0
Time Limit Exceeded

input:

200000 10 200000
55651 97298 108697 86619 60721 199951 10610 162267 154301 138848 39191 18605 101369 57073 34977 101576 71252 143401 89587 160521 166491 38442 150761 35579 25571 121311 38033 38483 144639 41401 179161 54872 157905 137601 46863 187656 171901 43715 41036 150741 69057 102031 130561 4772...

output:

1071065
925319
1088761
978215
941017
1019563
1083160
884170
948011
907312
1019832
950246
842178
952021
1167757
869651
901001
1024244
1165805
953682
938236
1025201
859725
875030
997209
894382
965219
924176
989066
847677
922187
1040451
841868
937351
851076
1016087
922591
915389
1030453
985855
1002718
...

result:


Subtask #3:

score: 0
Time Limit Exceeded

Test #5:

score: 0
Time Limit Exceeded

input:

200000 20000 200000
30681 32496 35471 48191 159123 69792 120915 150673 187226 158493 36275 26856 107976 124777 145229 69745 183961 14497 144808 153612 185893 137681 66417 46802 19345 113322 168046 128149 191001 135433 13201 139214 59489 81178 42343 163158 110121 119201 97501 53079 158755 192241 1132...

output:

28987966
19681585
28061745
27191419
16944222
30668597
27755020
27884098
20726277
19148920
27438528
19749782
24543233
22005963
24702398
17089398
30182286
25224658
19968177
21489137
31572450
28894318
20126280
20320838
23685755
30606653
27305672
20126305
24857502
37191041
29440731
26185203
32747517
245...

result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #4:

0%