当前位置:网站首页>Luogu: P2574 XOR的艺术 [线段树]
Luogu: P2574 XOR的艺术 [线段树]
2022-08-05 08:11:00 【9ack!?】
题目
题目给出一串01组成的字符串,有多种操作,可以对指定的区间的01进行翻转,也可以询问指定区间的01串中有多少个1。这道题也挺基本,相对上一个题来说只是多了一个维护标记的操作。
代码
#include <cstdio>
using namespace std;
#define lc k<<1
#define rc k<<1|1
const int maxn = 3e5+5;
int f[maxn<<2], a[maxn];
bool v[maxn<<2];
inline void build_tree(int k, int l, int r) {
v[k] = 0;
if(l == r) {
f[k] = a[l]; return; }
int m = (l+r) >> 1;
build_tree(lc, l, m);
build_tree(rc, m+1, r);
f[k] = f[lc]+f[rc];
}
inline void push_down(int k, int l, int r) {
int m = (l+r) >> 1;
if(v[k]) {
v[lc] ^= 1;
v[rc] ^= 1;
v[k] = 0;
f[lc] = (m-l+1)-f[lc];
f[rc] = (r-m)-f[rc];
}
}
inline void update(int k, int l, int r, int x, int y) {
push_down(k, l, r);
if(l == x && r == y) {
v[k] ^= 1;
f[k] = r-l+1-f[k];
return;
}
int m = (l+r) >> 1;
if(y <= m) {
update(lc, l, m, x, y);
} else if(x > m) {
update(rc, m+1, r, x, y);
} else {
update(lc, l, m, x, m);
update(rc, m+1, r, m+1, y);
}
f[k] = f[lc] + f[rc];
}
inline int query(int k, int l, int r, int x, int y) {
if(l == x && r == y) {
return f[k]; }
push_down(k, l, r);
/* if(v[k]) { v[lc] ^= 1; v[rc] ^= 1; v[k] = 0; } */
int m = (l+r) >> 1;
int ans;
if(y <= m)
ans = query(lc, l, m, x, y);
else if(x > m)
ans = query(rc, m+1, r, x, y);
else ans = query(lc, l, m, x, m) + query(rc, m+1, r, m+1, y);
f[k] = f[lc] + f[rc];
return ans;
}
int n, m;
int main(void)
{
/* freopen("in.txt", "r", stdin); */
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) {
scanf(" %1d", &a[i]);
}
build_tree(1, 1, n);
int op, x, y;
while(m--) {
scanf("%d%d%d", &op, &x, &y);
if(op == 1) {
printf("%d\n", query(1, 1, n, x, y));
} else {
update(1, 1, n, x, y);
}
}
return 0;
}
边栏推荐
- 双向循环带头链表
- 【 a daily topic 】 1403. The increasing order of the sequence, boy
- The toss of MM before going to the street (interesting)
- 【结构体内功修炼】枚举和联合的奥秘(三)
- Thinking after writing a code with a very high CPU usage
- 餐饮大单品「真香」,却没有穿透周期的能力
- Detailed explanation of DNS query principle
- php向mysql写入数据失败
- Ethernet Principle
- DataFrame insert row and column at specified position
猜你喜欢
ps怎么把图片变清晰,自学ps软件photoshop2022,简单快速用ps让照片更清晰更有质感
链表专项之环形链表
MVCC of Google's Fragmented Notes (Draft)
D2--FPGA SPI interface communication2022-08-03
DataFrame insert row and column at specified position
线性代数对角化
DNS 查询原理详解
Support touch screen slider carousel plugin
最 Cool 的 Kubernetes 网络方案 Cilium 入门教程
谷歌零碎笔记之MVCC(草稿)
随机推荐
【结构体内功修炼】枚举和联合的奥秘(三)
C语言制作-QQ聊天室
利用Jenkins的持续集成
Pagoda measurement - building small and medium-sized homestay hotel management source code
tear apart loneliness
Constellation ideal lover
行走社会100绝招
导出SQLServer数据到Excel中
unity 头发的渲染
love is a sad song
MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
双向循环带头链表
Detailed explanation of DNS query principle
餐饮大单品「真香」,却没有穿透周期的能力
Fiddler tool explanation
ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果
[Repost] Marry a man must marry a man whose salary is at least 3571.4 yuan higher than yours
行业应用软件项目经理三步曲
【结构体内功修炼】结构体实现位段(二)
支持触屏slider轮播插件