洛谷 2574 XOR的艺术
【题解】
线段树维护区间中1的个数就好了。每次修改就打上标记并把区间的sum改为len-sum.
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
#define N 200010
#define rg register
#define ls (u<<1)
#define rs (u<<1|1)
#define mid ((a[u].l+a[u].r)>>1)
#define len(x) (a[x].r-a[x].l+1)
using namespace std;
int n,m;
struct tree{
int l,r,sum; bool mark;
}a[N<<];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
void build(int u,int l,int r){
a[u].l=l; a[u].r=r;
if(l<r)
build(ls,l,mid),build(rs,mid+,r),a[u].sum=a[ls].sum+a[rs].sum;
else{
char c=getchar();
while(c!=''&&c!='') c=getchar();
a[u].sum=c-'';
}
}
inline void pushdown(int u){
if(!a[u].mark) return; else a[u].mark^=;
a[ls].mark^=; a[ls].sum=len(ls)-a[ls].sum;
a[rs].mark^=; a[rs].sum=len(rs)-a[rs].sum;
}
void update(int u,int r,int l){
if(l<=a[u].l&&a[u].r<=r){
a[u].sum=len(u)-a[u].sum;
a[u].mark^=;
return;
}
pushdown(u);
if(l<=mid) update(ls,r,l);
if(r>mid) update(rs,r,l);
a[u].sum=a[ls].sum+a[rs].sum;
}
int query(int u,int r,int l){
if(l<=a[u].l&&a[u].r<=r) return a[u].sum;
pushdown(u); int ret=;
if(l<=mid) ret+=query(ls,r,l);
if(r>mid) ret+=query(rs,r,l);
return ret;
}
int main(){
n=read(); m=read(); build(,,n);
while(m--){
if(read()==) printf("%d\n",query(,read(),read()));
else update(,read(),read());
}
return ;
}
洛谷 2574 XOR的艺术的更多相关文章
- 洛谷 P2574 XOR的艺术(线段树 区间异或 区间求和)
To 洛谷.2574 XOR的艺术 题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的 ...
- 洛谷P2574 XOR的艺术
题目描述 \(AKN\)觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为\(n\)的\(01\)串. 2 ...
- 洛谷——P2574 XOR的艺术
P2574 XOR的艺术 很久之前就想挑战一下这道题了,线段树下传标记的入门题,跟区间加法下传标记类似. #include<bits/stdc++.h> #define N 1000005 ...
- 洛谷 P2574 XOR的艺术
刚刚学了,线段树,一道线段树入门题试试水 下面是题面 题目描述 AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个 ...
- 洛谷P2574 XOR的艺术(线段树)——Chemist
当线段树遇上无敌位运算! 还是老套路,线段树维护区间和,一个区间有几个"1"就是这个区间的区间和,同时支持区间修改区间查询,只不过操作从加法变成了异或.主要难点就在更新懒标记那里, ...
- 【洛谷】【线段树+位运算】P2574 XOR的艺术
[题目描述:] AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的01串. 2. 给定一个范围[ ...
- 【洛谷P2574】XOR的艺术
XOR的艺术 题目链接 用线段树维护sum, 修改时 tag[p]^=1; sum=r-l+1-sum; 详见代码 #include<iostream> #include<cstdi ...
- 洛谷 P4151 BZOJ 2115 [WC2011]最大XOR和路径
//bzoj上的题面太丑了,导致VJ的题面也很丑,于是这题用洛谷的题面 题面描述 XOR(异或)是一种二元逻辑运算,其运算结果当且仅当两个输入的布尔值不相等时才为真,否则为假. XOR 运算的真值表如 ...
- [bzoj3668][Noi2014]起床困难综合症/[洛谷3613]睡觉困难综合症
来自FallDream的博客,未经允许,请勿转载,谢谢. 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综 ...
随机推荐
- 830C
分块+二分 这道题思路很巧妙 我们大概可以推出一个式子sigma(d-[(ai-1)%d+1])<=k,要求求出d的最大值 然后我们化简一下,sigma(d-[(ai-1)-[(ai-1)/d] ...
- 【171】IDL读取HDF文件
;+ ;:Description: ; Describe the procedure. ; ; Author: DYQ 2009-7-19; ; ;- PRO TEST_READHDF COMPILE ...
- 【158】◀▶ Linux-Bash学习
鸟哥的 Linux 私房菜 Linux 的 26 个命令 Shell 脚本教程 Linux 命令大全 目录——按文件顺序: echo:显示变量内容 printf:格式化输 ...
- openssh常用命令记录
command description date ssh [user@]hostname[:port] 登录远程机器 2017-03-21 scp <local_file> <use ...
- bzoj 4809: 皇后【dfs】
爆搜卡线过 并不知道正解是啥 #include<iostream> #include<cstdio> using namespace std; const int N=40; ...
- bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】
洛谷数据好强啊,普通spfa开o2都过不了,要加双端队列优化 因为是双向边,所以dis(u,v)=dis(v,u),所以分别以pa1和pa2为起点spfa一遍,表示pb-->pa1-->p ...
- [Swift]Array数组的swapAt函数
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 洛谷 P2142 高精度减法(模板)
题目描述 高精度减法 输入输出格式 输入格式: 两个整数a,b(第二个可能比第一个大) 输出格式: 结果(是负数要输出负号) 输入输出样例 输入样例#1: 2 1 输出样例#1: 1 说明 20%数据 ...
- Android 性能优化(3)性能工具之「调试 GPU 过度绘制」Debug GPU Overdraw Walkthrough-查看哪些view过度绘制了
Debug GPU Overdraw Walkthrough 1.In this document Prerequisites Visualizing Overdraw You should also ...
- Spring加载applicationContext.xml实现spring容器管理的单例模式
package com.etc.pojo; import org.springframework.context.ApplicationContext; import org.springframew ...