QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#726084#6533. Traveling in CellsHe_ngTL 1158ms515516kbC++205.1kb2024-11-08 21:33:392024-11-08 21:33:39

Judging History

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

  • [2024-11-08 21:33:39]
  • 评测
  • 测评结果:TL
  • 用时:1158ms
  • 内存:515516kb
  • [2024-11-08 21:33:39]
  • 提交

answer

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define int long long
#define pii pair<int, int>
const int N = 1e5 + 10;
const int M = 2e3;
const int MAXA = 1e5 + 5;
#define endl '\n'

// 快读部分
inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + ch - '0';
        ch = getchar();
    }
    return f * x;
}

struct DynamicSegtree {
    struct node {
        int data, tag;
        node *lc, *rc;
        node(int data_ = 0) : data(data_), tag(0), lc(nullptr), rc(nullptr) {}
        void up() {
            data = 0;
            if (lc != nullptr)
                data += lc->data;
            if (rc != nullptr)
                data += rc->data;
            return;
        }
        void down(int l, int r) {
            if (lc == nullptr)
                lc = new node;
            if (rc == nullptr)
                rc = new node;
            int mid = (l + r) >> 1;
            lc->data += (mid - l + 1) * tag;
            lc->tag += tag;
            rc->data += (r - mid) * tag;
            rc->tag += tag;
            tag = 0;
        }
    } *rt = new node(0);

    void modify(node *rt, int L, int R, int l, int r, int val) {
        if (l <= L && R <= r) {
            rt->data += (R - L + 1) * val;
            rt->tag += val;
            return;
        }
        rt->down(L, R);
        int mid = (L + R) >> 1;
        if (l <= mid)
            modify(rt->lc, L, mid, l, r, val);
        if (r > mid)
            modify(rt->rc, mid + 1, R, l, r, val);
        rt->up();
        return;
    }

    int query(node *rt, int L, int R, int l, int r) {
        if (l <= L && R <= r) {
            return rt->data;
        }
        rt->down(L, R);
        int mid = (L + R) >> 1;
        int ret = 0;
        if (l <= mid)
            ret += query(rt->lc, L, mid, l, r);
        if (r > mid)
            ret += query(rt->rc, mid + 1, R, l, r);
        return ret;
    }
};

int n, q;
int val[N];

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

void update1(int r, int k) {
    for (int i = r; i <= n; i += lowbit(i))
        val[i] += k;
}

int get_sum(int l, int r) {
    int ans = 0;
    for (int i = r; i; i -= lowbit(i))
        ans += val[i];
    for (int i = l - 1; i; i -= lowbit(i))
        ans -= val[i];
    return ans;
}

void solve() {
    n = read();
    q = read();
    vector<DynamicSegtree> tree(n + 1);
    vector<int> c(n + 1);
    vector<int> v(n + 1);

    for (int i = 1; i <= n; i++) {
        c[i] = read();
        tree[c[i]].modify(tree[c[i]].rt, 1, MAXA, i, i, 1);
        val[i] = 0;
    }
    
    for (int i = 1; i <= n; i++) {
        v[i] = read();
        update1(i, v[i]);
    }
    
    while (q--) {
        int x = read();
        if (x == 1) {
            int p = read(), y = read();
            tree[c[p]].modify(tree[c[p]].rt, 1, MAXA, p, p, -1);
            tree[y].modify(tree[y].rt, 1, MAXA, p, p, 1);
            c[p] = y;
        } else if (x == 2) {
            int p = read(), y = read();
            update1(p, y - v[p]);
            v[p] = y;
        } else if (x == 3) {
            int st = read(), k = read();
            vector<int> a(k + 1);
            for (int i = 1; i <= k; i++) {
                a[i] = read();
            }
            sort(a.begin(), a.end());
            a.erase(unique(a.begin() + 1, a.end()), a.end());
            k = a.size() - 1;

            int l = 1, r = st;
            auto check1 = [&](int mid, const vector<int>& a, int st) -> bool {
                int sum = 0;
                for (int i = 1; i <= k; i++) {
                    int cnt = tree[a[i]].query(tree[a[i]].rt, 1, MAXA, mid, st);
                    sum += cnt;
                }
                return sum == st - mid + 1;
            };
            auto check2 = [&](int mid, const vector<int>& a, int st) -> bool {
                int sum = 0;
                for (int i = 1; i <= k; i++) {
                    int cnt = tree[a[i]].query(tree[a[i]].rt, 1, MAXA, st, mid);
                    sum += cnt;
                }
                return sum == mid - st + 1;
            };
            
            while (l <= r) {
                int mid = (l + r) >> 1;
                if (check1(mid, a, st))
                    r = mid - 1;
                else
                    l = mid + 1;
            }
            int ansl = l;
            l = st;
            r = n;
            while (l <= r) {
                int mid = (l + r + 1) >> 1;
                if (check2(mid, a, st))
                    l = mid + 1;
                else
                    r = mid - 1;
            }
            int ansr = r;
            printf("%lld\n", get_sum(ansl, ansr));
        }
    }
}

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

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3780kb

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: 710ms
memory: 515516kb

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: 1158ms
memory: 312824kb

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: