2021HDU多校第二场 1004 I love counting 树状数组套01trie

Posted kaka0010

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021HDU多校第二场 1004 I love counting 树状数组套01trie相关的知识,希望对你有一定的参考价值。

原题链接:https://acm.hdu.edu.cn/showproblem.php?pid=6964

题意

有一个长度为n的序列,每个元素有一个权值c,问在[l,r]区间内,有多少种类的c满足 c ⨁ a ≤ b c\\bigoplus a≤b cab

分析

发现和前一场的06和10非常像,像是两题的结合体。对于统计合法c,比较经典的操作就是在字典树上跑,我们分类讨论一下

  1. b = 1, a = 0 这时c选0的话,可以直接加上左边子树的所有值,因为b已经确保大于等于a。如果c选1,接着往右走就可以了
  2. b = 1, a = 1 同理上面
  3. b = 0, a = 1 这时c没有选择,因此只能往右走
  4. b = 0, a = 0 同理上面

接下来还有如何去重的问题,我们设计一个前驱节点,如果这个颜色的权值已经被统计过了,那么就删除这个颜色的贡献。因此可以先离线处理一下答案,可以用树状数组维护前i个元素的贡献,当处理到右边界时直接处理答案就可以了。本质上就是树状数组套了一个trie来统计合法数量。

Code

#include <bits/stdc++.h>
#define lowbit(i) i & -i
#define Debug(x) cout << x << endl
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const ll INF = 1e18;
const int N = 1e5 + 10;
const int M = 1e6 + 10;
const int MOD = 998244353;
int c[N], tot, pre[N], n, rt[N];
int trie[N*400][2], sum[N*400], ans[N];
struct Query {
    int l, a, b, id;
};
vector<Query> q[N];
void insert(int &now, int val, int x) {
    if (!now) now = ++tot;
    int p = now;
    for (int i = 20; ~i; i--) {
        int bit = x >> i & 1;
        if (!trie[p][bit]) trie[p][bit] = ++tot;
        p = trie[p][bit];
        sum[p] += val;
    }
}

void sub(int p, int x) {
    for (int i = p; i <= n; i += lowbit(i)) insert(rt[i], -1, x);
}
void add(int p, int x) {
    for (int i = p; i <= n; i += lowbit(i)) insert(rt[i], 1, x);
}
int query(int now, int a, int b) {
    if (!now) return 0;
    int res = 0;
    for (int i = 20; ~i; i--) {
        int bit1 = a >> i & 1;
        int bit2 = b >> i & 1;
        if (bit2 == 1) {
            if (bit1 == 0) {
                res += sum[trie[now][0]];
                now = trie[now][1];
            } else {
                res += sum[trie[now][1]];
                now = trie[now][0];
            }
        } else {
            if (bit1 == 0) {
                now = trie[now][0];
            } else {
                now = trie[now][1];
            }
        }
        if (!now) break;
    }
    return res + sum[now];
}
int ask(int p, int a, int b) {
    int res = 0;
    for (int i = p; i > 0; i -= lowbit(i)) res += query(rt[i], a, b);
    return res;
}
void solve() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
    int Q; scanf("%d", &Q);
    for (int i = 1; i <= Q; i++) {
        int l, r, a, b; scanf("%d%d%d%d", &l, &r, &a, &b);
        q[r].push_back({l, a, b, i});
    }
    for (int i = 1; i <= n; i++) {
        if (pre[c[i]]) sub(pre[c[i]], c[i]);
        add(i, c[i]);
        pre[c[i]] = i;

        for (auto it : q[i]) {
            ans[it.id] = ask(i, it.a, it.b) - ask(it.l-1, it.a, it.b);
        }
    }
    for (int i = 1; i <= Q; i++) printf("%d\\n", ans[i]);
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}

以上是关于2021HDU多校第二场 1004 I love counting 树状数组套01trie的主要内容,如果未能解决你的问题,请参考以下文章

HDU6312 Game (多校第二场1004) 简单博弈

多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)

杭州多校第二场-I love exam(背包DP)

杭州多校第二场-I love exam(背包DP)

hdu_6047: Maximum Sequence (2017 多校第二场 1003)贪心

hdu_6055 : Regular polygon (2017 多校第二场 1011) 计算几何