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

#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. c free 使用MSDN library定制

    为了不使用vc6但是还要使用visual assist的各种自动功能,决定使用c free ,但是怎么调用微软的MSDN library呢,我目前使用的版本是MSDN 1.5精简版bing自动翻译的. ...

  2. asm和file system之间数据文件的转换

    How to move a datafile from a file system to ASMMoving a datafile from the file system can be achive ...

  3. 核方法(Kernel Methods)

    核方法(Kernel Methods) 支持向量机(SVM)是机器学习中一个常见的算法,通过最大间隔的思想去求解一个优化问题,得到一个分类超平面.对于非线性问题,则是通过引入核函数,对特征进行映射(通 ...

  4. URAL 1741 Communication Fiend(最短路径)

    Description Kolya has returned from a summer camp and now he's a real communication fiend. He spends ...

  5. MongoDB Linux下的安装和启动

    1. 下载MongoDB,此处下载的版本是:mongodb-linux-i686-1.8.1.tgz.tar. http://fastdl.mongodb.org/linux/mongodb-linu ...

  6. springboot 学习笔记(二)--- properties 配置

    springboot可以提供了多种方式配置properties. 一.Java System.setProperty(k, v) System.setProperty("myname&quo ...

  7. Visio中的Undo和Redo

    1.Visio默认Undo和Redo操作是可用的,Appliacation中的UndoEnabled标志Undo和Redo操作是否可用. m_Visio.Window.Application.Undo ...

  8. OpenJudge百炼-2747-数字方格-C语言-枚举

    描述:如上图,有3个方格,每个方格里面都有一个整数a1,a2,a3.已知0 <= a1, a2, a3 <= n,而且a1 + a2是2的倍数,a2 + a3是3的倍数, a1 + a2 ...

  9. Codeforces Round #328 (Div. 2) A

    A. PawnChess time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  10. Linux设置虚拟内存-创建和启用Swap交换区

    如果你的服务器的总是报告内存不足,并且时常因为内存不足而引发服务被强制kill的话,在不增加物理内存的情况下,启用swap交换区作为虚拟内存是一个不错的选择,如果是SSD硬盘,正常读写速度都在300M ...