当前位置:网站首页>【ACWing】1268. 简单题
【ACWing】1268. 简单题
2022-07-26 04:58:00 【记录算法题解】
题目地址:
https://www.acwing.com/problem/content/1270/
有一个 n n n个元素的数组,每个元素初始均为 0 0 0。有 m m m条指令,要么让其中一段连续序列数字反转—— 0 0 0变 1 1 1, 1 1 1变 0 0 0(操作 1 1 1),要么询问某个元素的值(操作 2 2 2)。例如当 n = 20 n=20 n=20时, 10 10 10条指令如下:
输入格式:
第一行包含两个整数 n , m n,m n,m,表示数组的长度和指令的条数;
以下 m m m行,每行的第一个数 t t t表示操作的种类:
若 t = 1 t=1 t=1,则接下来有两个数 L , R L,R L,R,表示区间 [ L , R ] [L,R] [L,R]的每个数均反转;
若 t = 2 t=2 t=2,则接下来只有一个数 i i i,表示询问的下标(数组下标从 1 1 1开始)。
输出格式:
每个操作 2 2 2输出一行(非 0 0 0即 1 1 1),表示每次操作 2 2 2的回答。
数据范围:
1 ≤ n ≤ 1 0 5 1≤n≤10^5 1≤n≤105
1 ≤ m ≤ 5 × 1 0 5 1≤m≤5×10^5 1≤m≤5×105
保证 L ≤ R L≤R L≤R
由于翻转等价于不进位加法,从而等价于异或,所以我们考虑一个差分数组,并用树状数组维护之。这个差分数组的差分取的是异或(即 A A A的差分数组 c [ i ] = A [ i ] ∧ A [ i − 1 ] c[i]=A[i]\land A[i-1] c[i]=A[i]∧A[i−1])。那么翻转 A [ l : r ] A[l:r] A[l:r]等价于 c [ l ] = c [ l ] ∧ 1 , c [ r + 1 ] = c [ r + 1 ] ∧ 1 c[l]=c[l]\land 1, c[r+1]=c[r+1]\land 1 c[l]=c[l]∧1,c[r+1]=c[r+1]∧1,询问等价于求前缀异或和。代码如下:
#include <iostream>
#define lowbit(x) (x & -x)
using namespace std;
const int N = 1e5 + 10;
int n, m;
int tr[N];
void add(int k, int x) {
for (; k <= n; k += lowbit(k)) tr[k] ^= x;
}
int sum(int k) {
int res = 0;
for (; k; k -= lowbit(k)) res ^= tr[k];
return res;
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int t, l, r, k;
scanf("%d", &t);
if (t == 1) {
scanf("%d%d", &l, &r);
add(l, 1);
add(r + 1, 1);
} else {
scanf("%d", &k);
printf("%d\n", sum(k));
}
}
}
每次操作时间复杂度 O ( log n ) O(\log n) O(logn),空间 O ( n ) O(n) O(n)。
边栏推荐
- autocomplete禁止表单自动填充
- There was an unexpected error (type=Method Not Allowed, status=405).记录报错
- columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by mysql8.0解决办法
- Add and modify the verification logic, and use -validation- to complete the group verification
- Axi protocol (4): signals on the Axi channel
- 2022杭电多校 DOS Card(线段树)
- Yapi installation
- STM32 development | ad7606 parallel multi-channel data acquisition
- 异步时父子线程间的ThreadLocal传递问题
- Kubernetes advanced training camp scheduler
猜你喜欢

自动化测试框架该如何搭建?

「游戏引擎 浅入浅出」4. 着色器

Can serial port can 232 can 485 serial port to CANbus bus gateway module can232/485mb converter cancom

7、 Restful

Seata两阶段提交AT详解

快恢复二极管工作原理及使用

ES6 modularization +commonjs

What are the characteristics of the grammar of Russian documents in the translation of scientific papers

Database startup message: ora-29702: error occurred in cluster group service

Switch and router technology: dynamic routing protocol, rip routing protocol and OSPF routing protocol
随机推荐
Database startup message: ora-29702: error occurred in cluster group service
Embedded practice -- CPU utilization statistics based on rt1170 FreeRTOS (24)
UE4 controls the rotation of objects by pressing keys
五、域对象共享数据
QT compilation error sorting and remote module Download
自动化测试框架该如何搭建?
Array sort 2
Two ways to create MySQL database
二、国际知名项目-HelloWorld
2022河南萌新联赛第(三)场:河南大学 J - 神奇数字
UE4 displays text when it is close to the object, and disappears when it is far away
Working principle and application of fast recovery diode
Phaser (I): platform jumping collection game
时代潮流-云原生数据库的崛起
有ggjj看看这个问题没,是否缓存导致跨域问题?
What points should be paid attention to in the selection of project management system?
SQL encryption and decryption injection details
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
批量将PPM格式图片转化为JPG格式
"Game engine light in light out" 4. shader