QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#384410#6533. Traveling in Cellsmai123TL 846ms6112kbC++145.0kb2024-04-09 23:09:242024-04-09 23:09:25

Judging History

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

  • [2024-04-09 23:09:25]
  • 评测
  • 测评结果:TL
  • 用时:846ms
  • 内存:6112kb
  • [2024-04-09 23:09:24]
  • 提交

answer

#include<bits/stdc++.h>

#define int long long
#define ios ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
typedef pair<int, int> PII;
const int Maxn = 1e5 + 10;
int c[Maxn], v[Maxn];
unordered_map<int, unordered_set<int>> pos;//记录每个颜色的位置
// 树状数组维护区间和
int tr[Maxn];

int lowbit(int x) {
    return x & -x;
}

void add(int x, int d) {
    for (; x < Maxn; x += lowbit(x))tr[x] += d;
}

int sum(int x) {
    int res = 0;
    for (; x > 0; x -= lowbit(x))res += tr[x];
    return res;
}


struct Node {
    int l, r, val;
} T[Maxn << 2];

void pushUp(int node) {
    T[node].val = T[node << 1].val + T[node << 1 | 1].val;
}

void build(int node, int l, int r) {
    T[node] = {l, r, 0};
    if (l == r)return;
    int mid = (l + r) >> 1;
    build(node << 1, l, mid);
    build(node << 1 | 1, mid + 1, r);
    pushUp(node);
}

void update(int node, int l, int r, int d) {
    if (l <= T[node].l && T[node].r <= r) {
        T[node].val = d;
        return;
    }
    int mid = (T[node].l + T[node].r) >> 1;
    if (l <= mid)update(node << 1, l, r, d);
    if (mid < r)update(node << 1 | 1, l, r, d);
    pushUp(node);
}

// 查询[l,r]区间内,最左边的完全填充区
int query_left(int node, int l, int r) {
    int mid = (T[node].l + T[node].r) >> 1;
    // 该节点被查询区间完全包括
    if (l <= T[node].l && T[node].r <= r) {
        // 区间和 等于 长度,代表该区间被全覆盖
        if (T[node].val == T[node].r - T[node].l + 1)return T[node].l;
        // 区间未覆盖
        if (T[node].l == T[node].r)return T[node].r + 1;
        // 先查询靠近x的区域
        int left = query_left(node << 1 | 1, l, r);
        if (left == mid + 1) left = query_left(node << 1, l, r);
        return left;
    } else if (l <= mid && mid < r) {
        // 查询区间被当前节点包括,且中点被查询区间包括
        int left = query_left(node << 1 | 1, l, r);
        if (left == mid + 1)left = query_left(node << 1, l, r);
        return left;
    } else if (l <= mid)return query_left(node << 1, l, r);//查左子树
    else if (mid < r)return query_left(node << 1 | 1, l, r);//查右子树
    return INT_MAX;
}

// 查询[l,r]区间内,最右边的完全填充区
int query_right(int node, int l, int r) {
    int mid = (T[node].l + T[node].r) >> 1;
    // 该节点被查询区间完全包括
    if (l <= T[node].l && T[node].r <= r) {
        if (T[node].val == T[node].r - T[node].l + 1) {
            return T[node].r;
        }
        // 该区间未完全填充
        if (T[node].l == T[node].r)return T[node].l - 1;
        int right = query_right(node << 1, l, r);
        if (right == mid)right = query_right(node << 1 | 1, l, r);
        return right;
    } else if (l <= mid && mid < r) {
        // 查询区间被当前节点包括,且中点被查询区间包括
        int right = query_right(node << 1, l, r);
        if (right == mid)right = query_right(node << 1 | 1, l, r);
        return right;
    } else if (l <= mid)return query_right(node << 1, l, r);//查左子树
    else if (mid < r)return query_right(node << 1 | 1, l, r);//查右子树
    return INT_MIN;
}


signed main() {
    int t;
    scanf("%lld", &t);
    while (t--) {
        pos.clear();
        int n, q;
        scanf("%lld%lld", &n, &q);
        for (int i = 1; i <= n; i++)tr[i] = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%lld", &c[i]);
            pos[c[i]].insert(i);//记录每种颜色的所有位置
        }
        // 树状数组维护的是区间和
        for (int i = 1; i <= n; i++) {
            scanf("%lld", &v[i]);
            add(i, v[i]);
        }
        while (q--) {
            int op, p, x, k;
            scanf("%lld", &op);
            if (op == 1) {
                scanf("%lld%lld", &p, &x);
                // cp改成x
                pos[c[p]].erase(p);
                if (pos[c[p]].empty())pos.erase(c[p]);
                pos[x].insert(p);
                c[p] = x;
            } else if (op == 2) {
                scanf("%lld%lld",&p,&x);
                // vp改成x
                int diff = x - v[p];
                add(p, diff);
                v[p] = x;
            } else {
                build(1, 1, n);
                scanf("%lld%lld", &x, &k);
                for (int i = 0; i < k; i++) {
                    int a;
                    scanf("%lld", &a);
                    // 遍历颜色a的所有位置,并 +1
                    for (int c_pos: pos[a]) {
                        update(1, c_pos, c_pos, 1);
                    }
                }
                int left = query_left(1, 1, x);
                int right = query_right(1, x, n);
                int ans = 0;
                if (left <= x)ans -= sum(left - 1);
                if (x <= right)ans += sum(right);
                printf("%lld\n", ans);
            }
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6112kb

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:

100
110
1200
21211
100010
4000000

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 235ms
memory: 6068kb

input:

20000
15 15
1 1 3 3 2 3 3 3 3 3 2 3 2 3 3
634593973 158136379 707704004 998369647 831633445 263092797 937841412 451774682 552617416 483763379 50360475 794662797 74247465 537217773 901809831
3 6 4 1 3 5 10
3 5 7 1 2 3 4 5 9 10
3 4 3 3 8 9
2 13 608033129
3 15 2 3 5
1 9 3
3 8 4 1 3 7 10
2 6 727472865
3...

output:

2689089686
8377825475
1706073651
1439027604
2689089686
792730352
8904867081
8904867081
8270273108
831633445
692051585
2782432626
697783016
883944422
184057757
287523250
184057757
696388752
184057757
1675459344
2667693025
2614711258
4006992373
1767091974
5348541057
5348541057
390631780
2290216252
942...

result:

ok 200062 numbers

Test #3:

score: 0
Accepted
time: 846ms
memory: 5900kb

input:

2000
150 150
8 3 8 8 8 6 8 4 2 7 6 8 8 5 8 7 7 8 5 6 8 8 6 8 8 8 8 7 8 6 6 8 8 8 6 2 3 4 8 8 7 8 5 8 2 6 8 7 8 8 6 8 6 8 3 8 8 8 8 4 7 8 7 3 7 6 7 5 5 8 6 8 8 6 3 8 6 7 6 8 8 7 4 8 6 7 8 7 7 7 7 8 8 8 8 2 5 2 8 8 6 7 6 3 8 8 7 8 8 8 6 6 8 6 6 7 5 8 8 8 7 8 7 7 6 8 8 8 8 8 8 6 5 7 5 5 8 7 8 7 7 7 6 5...

output:

4449391171
3290849667
852793841
5178673994
995994209
11431868919
4327723427
5071541023
3032743466
962345334
2997656427
4534278452
3851900075
3611231417
5071541023
1477584218
1299005818
1299005818
2145605244
854143763
886347565
2081234124
2333808475
2455955801
4179722063
2328504333
1473735464
4107685...

result:

ok 199987 numbers

Test #4:

score: -100
Time Limit Exceeded

input:

10
30000 30000
3 4 2 4 4 4 4 3 4 3 4 3 4 3 4 4 2 4 4 4 4 4 3 3 3 4 3 4 3 4 3 3 4 2 4 3 3 3 3 4 3 4 4 4 4 2 3 3 4 2 3 4 4 4 4 1 4 4 4 4 4 4 4 4 3 3 3 4 4 4 4 4 2 3 4 4 4 4 3 4 4 3 3 3 4 4 3 4 4 2 3 4 4 4 4 3 2 4 3 4 3 2 4 4 3 4 2 2 4 4 4 4 2 4 3 2 4 4 3 4 4 4 2 4 4 3 2 3 2 3 3 3 4 2 4 3 4 1 4 3 4 4 4...

output:

6959437173
934970676
72461245502
8365928740
8384151048
984567228
12482909122
1904927816
15134139942
3759040688
92670874909
332468911
5936663371
3562978848
1300592004
10314009201
5581540905
131246926443
15087184135864
4077066271
1124704817
1520626740
4388174158
744377942
2770411457
6231852240
1508724...

result: