luoguP2574 XOR的艺术
思路
01串的区间求和,区间翻转 lazy%20 则不用翻转,lazt%21则要翻转
模板题
代码
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define ls rt<<1
#define rs rt<<1|1
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int maxn = 2e5 + 7;
int read() {
int x = 0, f = 1; char s = getchar();
for (; s < '0' || s > '9'; s = getchar()) if (s == '-') f = -1;
for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
}
int n, m,a[maxn];
struct node{
int l,r,size,sum,lazy;
}e[maxn<<2];
void pushup(int rt) {
e[rt].sum=e[ls].sum+e[rs].sum;
}
void pushdown(int rt) {
if(e[rt].lazy%2) {
e[ls].lazy++;
e[rs].lazy++;
e[ls].sum=e[ls].size-e[ls].sum;
e[rs].sum=e[rs].size-e[rs].sum;
e[rt].lazy=0;
}
}
void build(int l,int r,int rt) {
e[rt].l=l,e[rt].r=r,e[rt].size=r-l+1;
if(l==r) {
e[rt].sum=a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,ls);
build(mid+1,r,rs);
pushup(rt);
}
void modfity(int L,int R,int rt) {
if(L<=e[rt].l&&e[rt].r<=R) {
e[rt].sum=e[rt].size-e[rt].sum;
e[rt].lazy++;
return;
}
pushdown(rt);
int mid=(e[rt].l+e[rt].r)>>1;
if(L<=mid) modfity(L,R,ls);
if(R>mid) modfity(L,R,rs);
pushup(rt);
}
int query(int L,int R,int rt) {
if(L<=e[rt].l&&e[rt].r<=R) {
return e[rt].sum;
}
pushdown(rt);
int mid=(e[rt].l+e[rt].r)>>1,ans=0;
if(L<=mid) ans+=query(L,R,ls);
if(R>mid) ans+=query(L,R,rs);
return ans;
}
int main() {
n=read(),m=read();
FOR(i,1,n) scanf("%1d",&a[i]);
build(1,n,1);
FOR(i,1,m) {
int p=read(),x=read(),y=read();
if(p) {
printf("%d\n",query(x,y,1));
} else {
modfity(x,y,1);
}
}
return 0;
}
luoguP2574 XOR的艺术的更多相关文章
- 洛谷 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 ...
- 洛谷P2574 XOR的艺术(线段树)——Chemist
当线段树遇上无敌位运算! 还是老套路,线段树维护区间和,一个区间有几个"1"就是这个区间的区间和,同时支持区间修改区间查询,只不过操作从加法变成了异或.主要难点就在更新懒标记那里, ...
随机推荐
- JAVA 的wait(), notify()与synchronized同步机制
转自:http://blog.csdn.net/zyplus/article/details/6672775 在JAVA中,是没有类似于PV操作.进程互斥等相关的方法的.JAVA的进程同步是通过syn ...
- Assign the task---hdu3974(线段树优化+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意就是:公司有n个员工,关系有n-1个,T x y 代表把工作y交给员工x: 员工可以把工作交 ...
- 详解回调函数——以JS为例解读异步、回调和EventLoop
回调,是非常基本的概念,尤其在现今NodeJS诞生与蓬勃发展中变得更加被人们重视.很多朋友学NodeJS,学很久一直摸不着门道,觉得最后在用Express写Web程序,有这样的感觉只能说明没有学懂 ...
- kubernetes实战(十三):k8s使用helm持久化部署harbor集成openLDAP登录
1.基本概念 上节在k8s中部署了harbor和ldap,本节将部署harbor使用openLDAP验证,部署方式与之前相同,只是改了adminserver-cm.yaml的AUTH_MODE: &q ...
- mysql 使用存储引擎
三 使用存储引擎 方法1:建表时指定引擎 指定innodb,不写默认也是innodb use 数据库先 create table innodb_t1(id int,name char)engine=i ...
- Android中 Application的使用
Application全局唯一,如果需要放置全局的变量,需要用到Application,类似于OC中的单例类,获者OC中的AppDelegate 第一步:创建一个AppContext继承Applica ...
- XMind思维导图自定义图标/图片
- vue使用resource传参数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- LVS + Keepalived 实现高可用、负载均衡 Web 集群
简介: LVS 是 Linux Virtual Server 的简写,Linux 虚拟服务器的意思,是一个虚拟的服务器集群系统,此项目由章文嵩博士于 1998 年 5 月成立,是中国最早出现的自由软件 ...
- Linux文本编辑器快捷方式