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"就是这个区间的区间和,同时支持区间修改区间查询,只不过操作从加法变成了异或.主要难点就在更新懒标记那里, ...
随机推荐
- mysql jdbc操作
import java.sql.*; import java.util.Map; public class Mysql { public static void main(String[] args) ...
- 面向切面编程--AOP(转)
add by zhj:面向切面编程就是在不修改函数A的前提下,在函数A前后插入业务逻辑B, C, D...这其实算是功能分解,将大模块S=A+B+C+D+……分解为独立的小功能A,B,C,D……,模块 ...
- requests库的小技巧
#coding:utf-8 import requests # url = 'http://www.baidu.com' # response = requests.get(url) # print ...
- 飞跃平野(sdut1124)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1124 飞跃原野 Time Limit: 500 ...
- dfs模板(真心不会深搜)
栈 #include <stdio.h> #include <string.h> ][]; ][]; ,-, , }; , ,-, }; int Min; void dfs(i ...
- 浙大 PAT 乙级 1001-1075 目录索引
1001. 害死人不偿命的(3n+1)猜想 1002. 写出这个数 1003. 我要通过! 1004. 成绩排名 1005. 继续(3n+1)猜想 1006. 换个格式输出整数 1007. 素数对猜想 ...
- c#实现图片二值化例子(黑白效果)
C#将图片2值化示例代码,原图及二值化后的图片如下: 原图: 二值化后的图像: 实现代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ...
- chrome正受到自动测试软件的控制-----web自动化测试如何去掉这段提示
本文为原创文章 在web自动化测试的时候,特别是在用chrome浏览器的时候经常会出现 “chrome正受到自动测试软件的控制” 这样的一句提示, 这是因为安装chrome浏览器的时候没有设置允许调 ...
- react-native 0.57 run-ios 失败解决办法
React Native 日常报错 'config.h' file not found 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_ ...
- js判断两个日期是否严格相差整年(合同日期常用)
1.var beginDate = new Date($("#InvoiceStartTime").val()); var endDate = new Date($("# ...