[Codeforces 242.E] XOR on Segment(线段树)

E. XOR on Segment

time limit per test:4 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

You’ve got an array aa, consisting of nn integers a1,a2,…,an. You are allowed to perform two operations on this array:

  1. Calculate the sum of current array elements on the segment [l,r], that is, count value al+al+1+…+ar.
  2. Apply the xor operation with a given number xx to each array element on the segment [l,r][l,r], that is, execute al=al⊕x,al+1=al+1⊕x,…,ar=ar⊕x. This operation changes exactly r−l+1 array elements.

Expression x⊕y means applying bitwise xor operation to numbers x and y. The given operation exists in all modern programming languages, for example in language C++ and Java it is marked as “^”, in Pascal — as “xor”.

You’ve got a list of m operations of the indicated type. Your task is to perform all given operations, for each sum query you should print the result you get.

Input

The first line contains integer nn (1≤n≤105) — the size of the array. The second line contains space-separated integers a1,a2,…,an (0≤ai≤106)— the original array.

The third line contains integer mm (1≤m≤5⋅104) — the number of operations with the array. The ii-th of the following mm lines first contains an integer titi (1≤ti≤2) — the type of the i-th query. If ti=1, then this is the query of the sum, if ti=2, then this is the query to change array elements. If the ii-th operation is of type 1, then next follow two integers li,ri (1≤li≤ri≤n). If the i-th operation is of type 2, then next follow three integers li,ri,xi (1≤li≤ri≤n,1≤xi≤106). The numbers on the lines are separated by single spaces.

Output

For each query of type 1 print in a single line the sum of numbers on the given segment. Print the answers to the queries in the order in which the queries go in the input.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.

Examples

inputoutput
5
4 10 3 13 7
8
1 2 4
2 1 3 3
1 2 4
1 3 3
2 2 5 5
1 1 5
2 1 2 10
1 2 3
26
22
0
34
11
6
4 7 4 0 7 3
5
2 2 3 8
1 1 5
2 3 5 1
2 4 5 6
1 2 3
38
28

题意

给出一段序列,每次有两个操作

1 l r 查询[l,r]的和

2 l r x将[l,r]异或x

解题思路

由于 x⩽106,即 x 不超过20位,可以每一位都开一个线段树,总共开二十个线段树。异或操作即是区间取反(0 变成 1,1变成 0),询问操作转化为求区间1的个数。
如图,以样例1为例,竖着一个虚线框即开成一个线段树。

 

#include <bits/stdc++.h>
#define FOR(i,s,t) for(int i=(s);i<=(t);i++)
#define ROF(i,s,t) for(int i=(s);i>=(t);i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn = 1e5 + 6;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int readInt(){
    int x=0;
    bool sign=false;
    char c=getchar();
    while(!isdigit(c)){
        sign=c=='-';
        c=getchar();
    }
    while(isdigit(c)){
        x=x*10+c-'0';
        c=getchar();
    }
    return sign?-x:x;
}
ll readLong(){
    ll x=0;
    bool sign=false;
    char c=getchar();
    while(!isdigit(c)){
        sign=c=='-';
        c=getchar();
    }
    while(isdigit(c)){
        x=x*10+c-'0';
        c=getchar();
    }
    return sign?-x:x;
}
string readString(){
    string s;
    char c=getchar();
    while(isspace(c)){
        c=getchar();
    }
    while(!isspace(c)){
        s+=c;
        c=getchar();
    }
    return s;
}

ll pow2[maxn];
int n;
int tr[maxn <<2][21];
int tag[maxn << 2][21];
int a[maxn];
void gather(int pos, int p){
    tr[p][pos] = tr[p << 1][pos] + tr[p << 1 | 1][pos];
}

void pushdown(int pos, int p, int l, int r){
    if (l == r) return;
    if (tag[p][pos]){
        tag[p<<1][pos] ^= 1;
        tag[p<<1|1][pos] ^= 1;
        int mid = (l + r) / 2;
        tr[p<<1][pos] = (mid - l + 1) - tr[p<<1][pos];
        tr[p<<1|1][pos] =(r - mid) - tr[p<<1|1][pos];
        tag[p][pos] = 0;
    }
}

void build(int pos, int p, int l, int r ){
    if (l > r) return ;
    if (l == r){
        tr[p][pos] = ((a[l] >> pos) & 1);
        return ;
    }
    int mid = (l + r) / 2;
    build(pos, p << 1, l , mid);
    build(pos, p << 1 | 1, mid + 1, r);
    gather(pos, p);
}

int query(int pos ,int p, int l , int r, int ql, int qr){
    if (l > r) return 0;
    if (l > qr || r < ql) return 0;
    pushdown(pos, p, l, r);
    if (l >= ql && r <= qr){
        return tr[p][pos];
    }
    int mid = (l + r) / 2;
    return query(pos, p << 1, l, mid, ql, qr) + query(pos, p << 1 | 1, mid + 1, r, ql, qr);
    gather(pos, p);
}

void update(int pos, int p, int l, int r, int ql, int qr){
    if (l > r) return ;
    if(l >qr || r <ql) return ;
    pushdown(pos, p, l, r);
    if (l >= ql && r <= qr){
        tag[p][pos] ^= 1;
        tr[p][pos] = (r - l + 1) - tr[p][pos];
        //cout << l << " " << r << " " << tr[p][pos] << endl;
        return;
    }
    int mid = (l + r) / 2;
    update(pos, p << 1, l , mid, ql, qr);
    update(pos, p << 1 | 1, mid + 1, r, ql,qr);
    gather(pos , p);
}

int main(){
    pow2[0] = 1;
    FOR(i,1,20) pow2[i] = pow2[i-1] * 2ll;
    n = readInt();
    FOR(i,1,n) a[i] = readInt();
    FOR(i,0,20){
        build(i,1,1,n);
    }
    int m = readInt();
    while (m--){
        int ope = readInt();
        int ql = readInt();
        int qr = readInt();
        if (ope == 1){
            ll ans = 0;
            FOR(i,0,20){
                ans += 1ll * query(i, 1, 1, n, ql, qr) * pow2[i];
            }
            printf("%lld\n", ans);
        }
        else {
            int qx = readInt();
            FOR(i, 0, 20){
                if(((qx >> i) & 1)) update(i, 1, 1, n, ql, qr);
            }
        }
    }
    return 0;
}

 

### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值