QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#703912#6533. Traveling in Cellslllei#RE 0ms0kbC++204.5kb2024-11-02 18:51:572024-11-02 18:52:02

Judging History

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

  • [2024-11-02 18:52:02]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-02 18:51:57]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using LL = long long;

constexpr int N = 3.5e6;
int info[N], ls[N], rs[N], tot;

void pull(int p) {
    info[p] = info[ls[p]] + info[rs[p]];
}

void modify(int &p, int l, int r, int x, int y) {
    if (p == 0) {
        p = ++tot;
    }
    if (l == r) {
        info[p] = y;
        return;
    } 
    int mid = (l + r) >> 1;
    if (x <= mid) {
        modify(ls[p], l, mid, x, y);
    } else {
        modify(rs[p], mid + 1, r, x, y);
    }
    pull(p);
}

int findFirst(const vector<int> &p, int l, int r, int x, int y, int &sum) {
    if (l > y || r < x) {
        return -1;
    }
    if (l >= x && r <= y) {
        int nxt = sum;
        for (auto q : p) {
            nxt += info[q];
        }
        if (nxt == r - x + 1) {
            sum = nxt;
            return -1;
        }
    }
    if (l == r) {
        return l;
    }
    int mid = (l + r) >> 1;
    vector<int> nxtP;
    for (auto x : p) {
        if (ls[x]) {
            nxtP.push_back(ls[x]);
        }
    }
    int res = findFirst(nxtP, l, mid, x, y, sum);
    if (res == -1) {
        nxtP.clear();
        for (auto x : p) {
            if (rs[x]) {
                nxtP.push_back(rs[x]);
            }
        }
        res = findFirst(nxtP, mid + 1, r, x, y, sum);
    }

    return res;
}

// zo
int findLast(const vector<int> &p, int l, int r, int x, int y, int &sum) {
    if (l > y || r < x) {
        return -1;
    }
    if (l >= x && r <= y) {
        int nxt = sum;
        for (auto q : p) {
            nxt += info[q];
        }
        if (nxt == y - l + 1) {
            sum = nxt;
            return -1;
        }
    }
    if (l == r) {
        return l;
    }
    int mid = (l + r) >> 1;
    vector<int> nxtP;
    for (auto x : p) {
        if (rs[x]) {
            nxtP.push_back(rs[x]);
        }
    }
    int res = findLast(nxtP, mid + 1, r, x, y, sum);
    if (res == -1) {
        nxtP.clear();
        for (auto x : p) {
            if (ls[x]) {
                nxtP.push_back(ls[x]);
            }
        }
        res = findLast(nxtP, l, mid, x, y, sum);
    }

    return res;
}

void solve() {
    int n, q;
    cin >> n >> q;

    vector<int> c(n), v(n), a(n);
    vector<int> rt(n);
    
    for (int i = 0; i < n; ++i) {
        cin >> c[i];
        --c[i];
        modify(rt[c[i]], 0, n - 1, i, 1);
    }
    vector<LL> tr(n + 1);

    auto add = [&](int x, LL y) {
        for (int i = x + 1; i <= n; i += i & -i) {
            tr[i] += y;
        }
    };

    auto query = [&](int x) {
        LL res = 0;
        for (int i = x + 1; i; i -= i & -i) {
            res += tr[i];
        }
        return res;
    };

    auto rangeSum = [&](int l, int r) {
        return query(r) - query(l - 1);
    };

    for (int i = 0; i < n;  ++i) {
        cin >> a[i];
        add(i, a[i]);
    }

    while (q--) {
        int op;
        cin >> op;
        if (op == 1) {
            int p, x;
            cin >> p >> x;
            p--, x--;
            modify(rt[c[p]], 0, n - 1, p, 0);
            c[p] = x;
            modify(rt[c[p]], 0, n - 1, p, 1);
        } else if (op == 2) {
            int p, x;
            cin >> p >> x;
            --p;
            add(p, x - a[p]);
            a[p] = x;
        } else {
            int x, k;
            cin >> x >> k;
            --x;
            vector<int> p;
            for (int i = 0; i < k; ++i) {
                int x;
                cin >> x;
                --x;
                if (rt[x]) {
                    p.push_back(rt[x]);
                }
            }
            int sum = 0;
            int l = findLast(p, 0, n - 1, 0, x, sum);
            l++;
            sum = 0;
            int r = findFirst(p, 0, n - 1, x, n - 1, sum);
            if (r == -1) {
                r = n - 1;
            } else {
                r--;
            }
            cout << rangeSum(l, r) << '\n';
            // cout << l + 1 << ' ' << r + 1 << '\n';
        }
    }

    for (int i = 1; i <= tot; ++i) {
        ls[i] = rt[i] = info[i] = 0;
    }
    tot = 0;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

/*
2
5 10
1 2 3 1 2
1 10 100 1000 10000
3 3 1 3
3 3 2 2 3
2 5 20000
2 3 200
3 3 2 1 3
3 3 3 1 2 3
1 3 4
2 1 100000
1 2 2
3 1 2 1 2
4 1
1 2 3 4
1000000 1000000 1000000 1000000
3 4 4 1 2 3 4
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

2
5 10
1 2 3 1 2
1 10 100 1000 10000
3 3 1 3
3 3 2 2 3
2 5 20000
2 3 200
3 3 2 1 3
3 3 3 1 2 3
1 3 4
2 1 100000
1 2 2
3 1 2 1 2
4 1
1 2 3 4
1000000 1000000 1000000 1000000
3 4 4 1 2 3 4

output:


result: