一道无聊的线段树题,写着玩玩而已……

#include<bits/stdc++.h>
#define N 1000010
#define lson (o<<1)
#define rson (o<<1|1)
using namespace std;
int n,m,a[N];
struct Segment_Tree{
int sumv[N<<],xorv[N<<],lenv[N];
inline void pushup(int o){sumv[o]=sumv[lson]+sumv[rson];}
inline void pushdown(int o){
if(!xorv[o])return;
xorv[o]=;
xorv[lson]=!xorv[lson];xorv[rson]=!xorv[rson];
sumv[lson]=lenv[lson]-sumv[lson];
sumv[rson]=lenv[rson]-sumv[rson];
}
void build(int o,int l,int r){
lenv[o]=r-l+;
if(l==r){sumv[o]=a[l];return;}
int mid=(l+r)>>;
build(lson,l,mid);build(rson,mid+,r);
pushup(o);
}
void change(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr){
xorv[o]=!xorv[o];sumv[o]=lenv[o]-sumv[o];
return;
}
int mid=(l+r)>>;pushdown(o);
if(ql<=mid)change(lson,l,mid,ql,qr);
if(qr>mid)change(rson,mid+,r,ql,qr);
pushup(o);
}
int querysum(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr)return sumv[o];
int mid=(l+r)>>,ans=;pushdown(o);
if(ql<=mid)ans+=querysum(lson,l,mid,ql,qr);
if(qr>mid)ans+=querysum(rson,mid+,r,ql,qr);
return ans;
}
}T;
inline int read(){
int f=,x=;char ch;
do{ch=getchar();if(ch=='-')f=-;}while(ch<''||ch>'');
do{x=x*+ch-'';ch=getchar();}while(ch>=''&&ch<='');
return f*x;
}
int main(){
n=read();m=read();
for(int i=;i<=n;++i)scanf("%1d",&a[i]);
T.build(,,n);
while(m--){
int opt=read(),l=read(),r=read();
if(opt==)T.change(,,n,l,r);
else printf("%d\n",T.querysum(,,n,l,r));
}
return ;
}
 

【luogu2574】xor的艺术的更多相关文章

  1. Luogu2574 XOR的艺术 (分块)

    本是要练线段树的,却手贱打了个分块 //#include <iostream> #include <cstdio> #include <cstring> #incl ...

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

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

  3. 【洛谷】【线段树+位运算】P2574 XOR的艺术

    [题目描述:] AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的01串. 2. 给定一个范围[ ...

  4. 【洛谷P2574】XOR的艺术

    XOR的艺术 题目链接 用线段树维护sum, 修改时 tag[p]^=1; sum=r-l+1-sum; 详见代码 #include<iostream> #include<cstdi ...

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

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

  6. 洛谷——P2574 XOR的艺术

    P2574 XOR的艺术 很久之前就想挑战一下这道题了,线段树下传标记的入门题,跟区间加法下传标记类似. #include<bits/stdc++.h> #define N 1000005 ...

  7. 洛谷 P2574 XOR的艺术

    刚刚学了,线段树,一道线段树入门题试试水 下面是题面 题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个 ...

  8. P2574 XOR的艺术

    题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的01串. 2. 给定一个范围[l,r ...

  9. 洛谷P2574 XOR的艺术

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

随机推荐

  1. [转载]压力测试工具siege的用法

    压力测试工具siege 原文:http://blog.csdn.net/qingye2008/article/details/34500949 Siege是Linux下的一个web系统的压力测试工具, ...

  2. Linux pthread 线程池实现

    基于pthread封装了一个简易的ThreadPool,具有以下特性: 1.具有优先级的任务队列 2.线程池大小可以二次调整,增加线程或者删除空闲线程 3.任务两种重写方式,重写run或者使用函数回调 ...

  3. Git&GitHub&GitBook

    一.定义 Git(分布式版本控制系统) GitHub gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub. gitHub于2008年4 ...

  4. 在Android Studio中创建(或添加)第一个Hello World应用程序

    下面我们将使用Android Studio创建第一个简单的Hello World应用程序. 1.打开Android Studio,加载画面如下图所示:   2.选择”Start a new Andro ...

  5. ng2模板语法/内置指令速查表

    https://www.angular.cn/docs/ts/latest/guide/cheatsheet.html

  6. MySql数据库插入或更新报错:Cannot add or update a child row: a foreign key constraint fails

    具体报错信息: Cannot add or update a child row: a foreign key constraint fails (`xxx`.`AAA`, CONSTRAINT `t ...

  7. js 给某个div增加class 样式(三种方式)

    第一种:      el.setAttribute('class','abc'); <!DOCTYPE HTML> <HTML> <HEAD> <meta c ...

  8. 当网卡收到的包的目的地址是主机上另一个网卡的地址.arp总结

    2019/01/13 今天测试发现结果不符合预期呀,发现设置了arp_filter之后,仍然是能ping通主机上的另外一张网卡.但是现在的问题是 -------------------- 内核中是如何 ...

  9. 【SQLAlchemy】SQLAlchemy技术文档(中文版)(中)

    10.建立联系(外键) 是时候考虑怎样映射和查询一个和Users表关联的第二张表了.假设我们系统的用户可以存储任意数量的email地址.我们需要定义一个新表Address与User相关联. from ...

  10. P1559 运动员最佳匹配问题

    题目描述 羽毛球队有男女运动员各n人.给定2 个n×n矩阵P和Q.P[i][j]是男运动员i和女运动员j配对组成混合双打的男运动员竞赛优势:Q[i][j]是女运动员i和男运动员j配合的女运动员竞赛优势 ...