【luogu2574】xor的艺术
一道无聊的线段树题,写着玩玩而已……
#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的艺术的更多相关文章
- Luogu2574 XOR的艺术 (分块)
本是要练线段树的,却手贱打了个分块 //#include <iostream> #include <cstdio> #include <cstring> #incl ...
- 洛谷 P2574 XOR的艺术(线段树 区间异或 区间求和)
To 洛谷.2574 XOR的艺术 题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的 ...
- 【洛谷】【线段树+位运算】P2574 XOR的艺术
[题目描述:] AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的01串. 2. 给定一个范围[ ...
- 【洛谷P2574】XOR的艺术
XOR的艺术 题目链接 用线段树维护sum, 修改时 tag[p]^=1; sum=r-l+1-sum; 详见代码 #include<iostream> #include<cstdi ...
- luogu P2574 XOR的艺术 (线段树)
luogu P2574 XOR的艺术 (线段树) 算是比较简单的线段树. 当区间修改时.\(1 xor 1 = 0,0 xor 1 = 1\)所以就是区间元素个数减去以前的\(1\)的个数就是现在\( ...
- 洛谷——P2574 XOR的艺术
P2574 XOR的艺术 很久之前就想挑战一下这道题了,线段树下传标记的入门题,跟区间加法下传标记类似. #include<bits/stdc++.h> #define N 1000005 ...
- 洛谷 P2574 XOR的艺术
刚刚学了,线段树,一道线段树入门题试试水 下面是题面 题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个 ...
- P2574 XOR的艺术
题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的01串. 2. 给定一个范围[l,r ...
- 洛谷P2574 XOR的艺术
题目描述 \(AKN\)觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为\(n\)的\(01\)串. 2 ...
随机推荐
- UGUI 代码 动态添加 Event Trigger 的事件
Additionally, if you need more than just the events provided by default, I'd suggest instead attachi ...
- 深度优先搜索(DFS),逃离迷宫
[原创] 今天来说说深度优先搜索,深度优先是图论中的内容,大意是从某一点出发,沿着一个方向搜索下去,并伴随着有回退的特点,通常用来判断某一解是否存在,不用来寻找最优解:这里来看一个非常有意思的题目: ...
- 文件系统之 stat与access
stat命令 stat既有命令也有同名函数,用来获取文件Inode里主要信息,所以stat命令的输出信息比ls命令的输出信息要更详细,stat 跟踪符号链接,lstat不跟踪符号链接,其中会输出对应文 ...
- EXT.NET各个版本下载地址
官网下载地址为:http://www.nuget.org/packages/Ext.NET
- nginx开机自启动
配置步骤:1 . vi /etc/init.d/nginx2. chkconfig --level nginx 2345 on --重点 --------------------以下为nginx配置文 ...
- [洛谷P4568][JLOI2011]飞行路线
题目大意:最短路,可以有$k$条边无费用 题解:分层图最短路,建成$k$层,层与层之间的边费用为$0$ 卡点:空间计算出错,建边写错 C++ Code: #include <cstdio> ...
- [51nod1503]猪和回文 DP
---题面--- 题解: 首先观察到题目要求的是合法回文串的个数,而回文串要求从前往后和从后往前是一样的,因此我们假设有两只猪,分别从左上和右下开始走,走相同的步数最后相遇,那么它们走的路能拼在一起构 ...
- ZOJ 3496 Assignment | 二分+有上下界网络流
题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3496 大概意思:给你一个网络,有源汇,在保证最大流的情况下求下面两 ...
- [Leetcode] scramble string 乱串
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- [Leetcode] Path Sum II路径和
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...