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;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值