QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#103788#5098. 第一代图灵机bashkort#20 384ms34596kbC++204.8kb2023-05-07 15:50:212024-05-26 02:51:04

Judging History

This is the latest submission verdict.

  • [2024-05-26 02:51:04]
  • Judged
  • Verdict: 20
  • Time: 384ms
  • Memory: 34596kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-07 15:50:21]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr ll inf = 3e18;

struct Info {
    ll ans = 0, mn = inf;
};

Info operator+(const Info &a, const Info &b) {
    return {max(a.ans, b.ans), min(a.mn, b.mn)};
}

vector<ll> tag;
vector<Info> t;
int sz = 1;


void apply(int x, ll tg) {
    tag[x] = tg;
    t[x].ans = tg - t[x].mn;
}

void push(int x) {
    if (tag[x] != -1) {
        apply(x << 1, tag[x]);
        apply(x << 1 | 1, tag[x]);
        tag[x] = -1;
    }
}

void pull(int x) {
    t[x] = t[x << 1] + t[x << 1 | 1];
}

void init(int n, vector<ll> mn) {
    sz = 1 << __lg(n) + !!(n & (n - 1));
    tag.assign(sz << 1, -1), t.assign(sz << 1, Info());

    for (int i = 0; i < n; ++i) {
        t[i + sz].mn = mn[i];
    }
    for (int i = sz - 1; i > 0; --i) {
        pull(i);
    }
}

Info rangeQuery(int l, int r, int x = 1, int lx = 0, int rx = sz) {
    if (l >= rx || lx >= r) {
        return {};
    }
    if (l <= lx && rx <= r) {
        return t[x];
    }
    int mid = lx + rx >> 1;
    push(x);
    return rangeQuery(l, r, x << 1, lx, mid) + rangeQuery(l, r, x << 1 | 1, mid, rx);
}

void rangeApply(int l, int r, ll tg, int x = 1, int lx = 0, int rx = sz) {
    if (l >= rx || lx >= r) {
        return;
    }
    if (l <= lx && rx <= r) {
        return apply(x, tg);
    }
    int mid = lx + rx >> 1;
    push(x);
    rangeApply(l, r, tg, x << 1, lx, mid), rangeApply(l, r, tg, x << 1 | 1, mid, rx);
    pull(x);
}

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 first 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);

    init(n, pref);

    for (int i = 0; i < n; ++i) {
        int r = SegmentTree::rangeMin(i, n);
        rangeApply(i, i + 1, pref[r]);
    }

    auto modify = [&](int x, int to) {
        SegmentTree::modify(x, to);

        int mn = SegmentTree::rangeMin(x, n);
        int j = SegmentTree::descend(mn) + 1;
        assert(j <= x);

        rangeApply(j, x + 1, pref[mn]);
    };

    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 != -1) {
            nxt[p] = npx;
            modify(p, npx);
        }

        if (np != -1) {
            nxt[np] = x;
            modify(np, x);
        }

        nxt[x] = nx;
        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];
        }
        ll ans = rangeQuery(l, i + 1).ans;
        ans = max(ans, pref[r] - pref[i + 1]);
        return ans;
    };

    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: 6ms
memory: 4176kb

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:

118571
90725
79596
95154
95154
94493
72411
100567
100567
100567
100567
90725
100567
100567
90725
118571
94493
95154
58191
118571
100567
100567
100567
39374
89208
118571
99923
100567
100567
95724
87252
100567
118571
100567
100567
100567
100567
100567
100567
26617
100567
99923
100567
118571
100567
100...

result:

wrong answer 1062nd lines differ - expected: '107128', found: '106137'

Subtask #2:

score: 0
Wrong Answer

Test #3:

score: 0
Wrong Answer
time: 384ms
memory: 31520kb

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:

1232419
1222519
1232419
1232419
1232419
1232419
1232419
1232419
1232419
1232419
1040511
1148070
1232419
1232419
1232419
1232419
1206368
1206368
1232419
1232419
1232419
1222519
1167757
1206368
1214212
1222519
1232419
1222519
1222519
1160928
1011843
1232419
1232419
1189403
1222519
1232419
1222519
1148...

result:

wrong answer 10234th lines differ - expected: '890306', found: '853340'

Subtask #3:

score: 20
Accepted

Test #5:

score: 20
Accepted
time: 281ms
memory: 34596kb

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:

46702944
46702944
38383720
38615532
38615532
42801975
39035571
46702944
46702944
46702944
27438528
38402892
46702944
46702944
42801975
42323113
39035571
42323113
46702944
46702944
46702944
41821993
46702944
34075405
46702944
38615532
46702944
28680653
46702944
42801975
42801975
38025842
46702944
467...

result:

ok 200000 lines

Test #6:

score: 20
Accepted
time: 277ms
memory: 34580kb

input:

200000 20000 200000
35105 74665 63960 162062 164953 63344 145369 115837 108866 61866 110690 123641 106889 65531 115933 163273 7531 128251 158561 169221 149787 40911 54465 92737 73473 10609 62701 89599 40007 40272 7318 129047 171198 90131 111281 85645 174001 140289 135851 26785 136485 31989 16313 888...

output:

43816163
35764822
45394839
45394839
45394839
43816163
45394839
43816163
40900280
38802753
45394839
45394839
43816163
34715395
45394839
43816163
43816163
45394839
43816163
45394839
45394839
43816163
35764822
45394839
43816163
43816163
16638306
45394839
35764822
45394839
34921501
45394839
45394839
409...

result:

ok 200000 lines

Test #7:

score: 20
Accepted
time: 304ms
memory: 34428kb

input:

200000 20000 200000
80203 178041 44172 21001 54489 120807 60663 147301 166763 49071 98913 115641 30627 164382 54165 165057 46105 9628 57953 86346 8273 137848 44871 119728 107309 132201 72483 198451 58505 185062 27039 49401 172444 101505 180973 59256 44859 53105 195233 161425 132423 2566 189331 15869...

output:

44318499
33827474
43556508
44318499
43556508
38914187
44318499
43556508
47858466
44318499
40709211
43556508
35706654
43556508
44318499
44318499
47858466
44318499
35359541
43556508
43556508
43556508
47858466
31755901
43556508
44318499
43556508
43556508
44318499
44318499
44318499
44318499
43556508
443...

result:

ok 200000 lines

Test #8:

score: 20
Accepted
time: 272ms
memory: 34404kb

input:

200000 20000 200000
69757 155771 81753 9285 168151 179881 122502 198324 140481 33185 155861 173423 117211 80727 63754 167913 121921 185921 182266 24801 167005 191511 77176 176741 117041 42534 10209 6241 83970 67652 164225 155249 125057 23841 71911 133150 79732 125061 7111 29841 142343 129299 155501 ...

output:

54623112
54623112
54623112
36983972
41889300
49604086
43664569
54623112
42438674
39404039
43664569
35418806
43664569
54623112
49604086
49604086
42438674
54623112
54623112
33869050
54623112
42438674
36847615
39404039
54623112
54623112
43664569
49604086
42438674
54623112
54623112
43664569
49604086
424...

result:

ok 200000 lines

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #4:

0%