FZU2105 Digits Count --线段树(区间与、或、异或)

博客探讨了FZU2105问题,涉及区间与、或、异或操作。由于数值范围限制,可以使用四位二进制表示。通过为每位建立线段树,更新对应位的答案。线段树节点存储区间一的个数(sum)、是否完全被0或1覆盖(cov)和异或懒标记(XOR)。区间与操作对应区间置0,区间或操作对应区间置1,区间异或操作改变异或标记。
摘要由CSDN通过智能技术生成

FZU2105 Digits Count

Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations:

Operation 1: AND opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] AND opn (here "AND" is bitwise operation).

Operation 2: OR opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] OR opn (here "OR" is bitwise operation).

Operation 3: XOR opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] XOR opn (here "XOR" is bitwise operation).

Operation 4: SUM L R

We want to know the result of A[L]+A[L+1]+...+A[R].

Now can you solve this easy problem?

Input
The first line of the input contains an integer T, indicating the number of test cases. (T≤100)

Then T cases, for any case, the first line has two integers n and m (1≤n≤1,000,000, 1≤m≤100,000), indicating the number of elements in A and the number of operations.

Then one line follows n integers A[0], A[1], ..., A[n-1] (0≤A[i]<16,0≤i<n).

Then m lines, each line must be one of the 4 operations above. (0≤opn≤15)

Output
For each test case and for each "SUM" operation, please output the result with a single line.
Sample Input
1
4 4
1 2 4 7
SUM 0 2
XOR 5 0 0
OR 6 0 3
SUM 0 2
Sample Output
7
18
Hint
A = [1 2 4 7]

SUM 0 2, result=1+2+4=7;

XOR 5 0 0, A=[4 2 4 7];

OR 6 0 3, A=[6 6 6 7];

SUM 0 2, result=6+6+6=18.

题意:
给出区间与、或、异或x操作,还有询问区间和。
思路:
因为给定数的范围只有15,对应二进制可以用4位表示
我们给每一位建线段树,这样每次只要更新对应位的答案
在这里插入图片描述

线段树节点维护三个重要信息
sum:区间内一的个数
cov:区间是否完全被0、1覆盖 -1 代表为被覆盖
XOR:异或懒标记
对于区间 与 来说只有当数为0时会产生影响,相当于区间置0
对于区间 或、异或 来说只有当数为1时才会产生影响
对于区间 或 来说相当于区间置1
对于区间 异或 来说改变区间的异或标记即可

#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
template<class...Args>
void debug(Args... args) {
   //Parameter pack
    auto tmp = {
    (cout << args << ' ', 0)... };
    cout << "\n";
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>pll;
typedef pair<int, int>pii;
const ll N = 5e5 + 5;
const ll INF = 0x7fffffff;
const ll MOD = 1e9 + 7;

int a[N];
struct node {
   
    int l, r;
    int sum;  //区间内一的个数
    int cov;  //区间是否完全被0、1覆盖 -1 代表为被覆盖
    int XOR;  //异或懒标记
    int len() {
    return r - l + 1; }
};
struct Segtree{
   
    node tree[N << 2];
    void push_up(int root) {
   
        tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum;
    }
    void build(int root, int l, int
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值