每一位维护一颗线段树

(-1)^1 =-2

(-2)^1=-1

#include <cstdio>
#include<iostream>
using namespace std;
//#define int long long
#define si signed
#define sc(x) scanf("%d", &x);
#define P pair<int, int>
int lazy[][];
int sum[][];
int A[];
int n, m;
void pushdown(int id, int x, int l, int r)
{
if (lazy[id][x] == )
{
lazy[id][x] = ;
int mid = l + r >> ;
lazy[id][x << ] ^= ;
sum[id][x << ] = (mid - l + ) - sum[id][x << ];
lazy[id][x << | ] ^= ;
sum[id][x << | ] = (r - mid) - sum[id][x << | ];
}
else if (lazy[id][x] == -)
{
lazy[id][x] = ;
int mid = l + r >> ;
lazy[id][x << ] = -;
sum[id][x << ] = (mid - l + );
lazy[id][x << | ] = -;
sum[id][x << | ] = (r - mid);
}
else if (lazy[id][x] == -)
{
lazy[id][x] = ;
int mid = l + r >> ;
lazy[id][x << ] = -;
sum[id][x << ] = ;
lazy[id][x << | ] = -;
sum[id][x << | ] = ;
}
}
void pushup(int id, int x)
{
sum[id][x] = sum[id][x << ] + sum[id][x << | ];
}
void build(int id, int l, int r, int x)
{
lazy[id][x] = ;
if (l == r)
{
sum[id][x] = ((A[l] >> id) & );
return;
}
int mid = (l + r) / ;
build(id, l, mid, x << );
build(id, mid + , r, x << | );
pushup(id, x);
}
void update(int id, int l, int r, int x, int type, int L, int R)
{
if (type == )
{
// cout<<type<<"type"<<endl; if (l >= L && r <= R)
{
lazy[id][x] ^= ;
sum[id][x] = (r - l + ) - sum[id][x];
//cout<<L<<' '<<R<<endl;
return;
}
pushdown(id, x, l, r);
int mid = (l + r) >> ;
if (L <= mid)
update(id, l, mid, x << , type, L, R);
if (R > mid)
update(id, mid + , r, x << | , type, L, R);
pushup(id, x);
}
else if (type == )
{ if (l >= L && r <= R)
{
lazy[id][x] = -;
sum[id][x] = ;
return;
}
pushdown(id, x, l, r);
int mid = (l + r) >> ;
if (L <= mid)
update(id, l, mid, x << , type, L, R);
if (R > mid)
update(id, mid + , r, x << | , type, L, R);
pushup(id, x);
}
else
{ if (l >= L && r <= R)
{
lazy[id][x] = -;
sum[id][x] = r - l + ;
return;
}
pushdown(id, x, l, r);
int mid = (l + r) >> ;
if (L <= mid)
update(id, l, mid, x << , type, L, R);
if (R > mid)
update(id, mid + , r, x << | , type, L, R);
pushup(id, x);
}
}
int query(int id, int l, int r, int x, int L, int R)
{
int ans = ;
if (L <= l && r <= R)
{
return sum[id][x];
}
pushdown(id, x, l, r);
int mid = l + r >> ;
if (L <= mid)
ans += query(id, l, mid, x << , L, R);
if (R > mid)
ans += query(id, mid + , r, x << | , L, R);
return ans;
}
si main()
{
int T;
sc(T)
string s;
while (T--)
{
sc(n) sc(m);
for (int i = ; i <= n; i++)
sc(A[i])
for (int i = ; i <; i++)
{
build(i, , n-, );
} int l,r,x;
while (m--)
{
cin >> s;
if (s[] == 'S')
{
sc(l) sc(r)
int ans=;
for(int i=;i<;i++){
ans +=query(i,,n-,,l,r)*(<<i);
}
cout<<ans<<'\n';
}
else if (s[] == 'X')
{
sc(x)
sc(l) sc(r) for(int i=;i<;i++){
if((x>>i)&){
update(i,,n-,,,l,r);
}
}
}
else if (s[] == 'O')
{
sc(x)
sc(l) sc(r) for(int i=;i<;i++){
if((x>>i)&){
update(i,,n-,,,l,r);
}
}
}
else
{
sc(x)
sc(l) sc(r) for(int i=;i<;i++){
if(!((x>>i)&)){
update(i,,n-,,,l,r);
}
}
}
}
} //system("pause");
}

xor or and 线段树的更多相关文章

  1. luogu P2574 XOR的艺术 (线段树)

    luogu P2574 XOR的艺术 (线段树) 算是比较简单的线段树. 当区间修改时.\(1 xor 1 = 0,0 xor 1 = 1\)所以就是区间元素个数减去以前的\(1\)的个数就是现在\( ...

  2. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  3. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  4. codeforces 242E. XOR on Segment 线段树

    题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...

  5. 洛谷 P2574 XOR的艺术(线段树 区间异或 区间求和)

    To 洛谷.2574 XOR的艺术 题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的 ...

  6. 2018.08.22 hyc的xor/mex(线段树/01trie)

    hyc的xor/mex 描述 NOIP2017就要来了,备战太累,不如做做hyc的新题? 找回自信吧! 一句话题意:n个数,m个操作 操作具体来讲分两步 1.读入x,把n个数全部xor上x 2.询问当 ...

  7. XOR on segment(线段树区间异或更新)

    原题传送门 本题大意:给定n个数字和m个操作,操作共有两种,第一种是求解区间l到r上元素的和,第二种是将区间l到r的元素都异或一个x,作为某个位置的新值. 很容易想到线段树维护区间和,但是我们发现,在 ...

  8. 「 Luogu P2574 」 XOR的艺术——线段树

    # 解题思路 这题不难,但是原谅我一开始的傻逼想法,一会儿再给大家透露透露. 先说怎么做这题. 显然对于 $0$ 和 $1$ 来说,异或无非也就只有两种变化 异或了奇数次,$0$ 就会变成 $1$,$ ...

  9. CodeForces 242E - XOR on Segment 二维线段树?

    今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...

随机推荐

  1. [转帖]影驰首发PCIe 4.0 SSD:群联AMD合作主控飚出5GB/s

    影驰首发PCIe 4.0 SSD:群联AMD合作主控飚出5GB/s https://www.cnbeta.com/articles/tech/851275.htm 硬件发展的真快.. AMD刚刚发布的 ...

  2. MySQL-线上数据迁移实战记录

    1. 迁移背景和限制条件 随着功能的迭代或者数据表中数据量的增加,将现有数据进行迁移已是工作中经常遇到的事情.通常我们在平时迁移数据数据的时候,只需要用mysqldump.mysqlimport指令就 ...

  3. Spark架构角色及基本运行流程

    1. 集群角色 Application:基于spark的用户程序,包含了一个Driver program 和集群中多个Executor Driver Program:运行application的mai ...

  4. 洛谷 P1578 奶牛浴场 题解

    题面 1.定义有效子矩形为内部不包含任何障碍点且边界与坐标轴平行的子矩形.如图所示,第一个是有效子矩形(尽管边界上有障碍点),第二个不是有效子矩形(因为内部含有障碍点). 2.极大有效子矩形:一个有效 ...

  5. .net core 2.2.0 SOAP踩坑

    首先确认下面几个程序集是最新版本: <PackageReference Include="System.ServiceModel.Http" Version="4. ...

  6. .Net Core 认证系统源码解析

    不知不觉.Net Core已经推出到3.1了,大多数以.Net为技术栈的公司也开始逐步的切换到了Core,从业也快3年多了,一直坚持着.不管环境怎么变,坚持自己的当初的选择,坚持信仰 .Net Cor ...

  7. SpringMVC基础01——SpringMVC的知识结构体系

    1.前言 目前在各大互联网公司使用最热门的技术莫过于SpringBoot以及在其基础之上的SpringCloud,然而学习这些技术的基础在于学好Spring和SpringMVC,准确来说SpringM ...

  8. 点亮指路灯led

    为什么要使用LED? (bootloader,kernel)开发初期,由于串口等硬件尚未被初始化,因此调试手段相当有限,这时通常会采用LED来做为程序调试的重要手段. LED驱动设计: 1.设置GPI ...

  9. HMC版本支持

      Target Version Upgrade From Upgrade Instructions Updates Date Available End of Service Models supp ...

  10. puppet自动化安装服务

    puppet自动化部署 主机环境: server(master)端:172.25.7.1(server1.example.com) client(agent)端:172.25.7.2 172.25.7 ...