当前位置:网站首页>[acwing] 1268. Simple questions
[acwing] 1268. Simple questions
2022-07-26 05:03:00 【Record algorithm solution】
Title address :
https://www.acwing.com/problem/content/1270/
There is one n n n Array of elements , Each element is initially 0 0 0. Yes m m m Orders , Or reverse one of the consecutive numbers —— 0 0 0 change 1 1 1, 1 1 1 change 0 0 0( operation 1 1 1), Or ask for the value of an element ( operation 2 2 2). For example, when n = 20 n=20 n=20 when , 10 10 10 The instructions are as follows :
Input format :
The first line contains two integers n , m n,m n,m, Represents the length of the array and the number of instructions ;
following m m m That's ok , The first number of each line t t t Indicates the type of operation :
if t = 1 t=1 t=1, Then there are two numbers L , R L,R L,R, Indicates the interval [ L , R ] [L,R] [L,R] Every number of is reversed ;
if t = 2 t=2 t=2, Then there is only one number i i i, Subscript indicating inquiry ( Array index from 1 1 1 Start ).
Output format :
Each operation 2 2 2 Output one line ( Not 0 0 0 namely 1 1 1), Indicates each operation 2 2 2 Answer .
Data range :
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
Guarantee L ≤ R L≤R L≤R
Because flipping is equivalent to non carry addition , Thus equivalent to XOR , So let's consider a differential array , And maintain it with a tree array . The difference of this difference array is XOR ( namely A A A The differential array of c [ i ] = A [ i ] ∧ A [ i − 1 ] c[i]=A[i]\land A[i-1] c[i]=A[i]∧A[i−1]). So flip A [ l : r ] A[l:r] A[l:r] Equivalent to 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, Asking is equivalent to finding prefix XOR and . The code is as follows :
#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));
}
}
}
Time complexity of each operation O ( log n ) O(\log n) O(logn), Space O ( n ) O(n) O(n).
边栏推荐
- Torch slice maintenance
- 新导则下的防洪评价报告编制方法及洪水建模
- Use field parameters for report translation
- The pit of history can only be filled up as far as possible
- Switch to router technology: OSPF single zone configuration, OSPF multi zone and end zone
- Principle of image nonlocal mean filtering
- What are the well-known to-do apps at home and abroad
- Character function and string function (I)
- Excel VBA:实现自动下拉填充公式至最后一行
- 5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
猜你喜欢

How many holes have you stepped on in BigDecimal?

Wsl2 best practices, eliminate difficult xshell and finalshell

vector详解和迭代器失效问题

minipcie接口CAN卡解决工控机扩展CAN通道的难题 minipcie CAN

注解@Autowired如何自动装配
![[weekly translation go] how to write your first program with go](/img/77/cf77a46340a39797382fd7b60517d5.png)
[weekly translation go] how to write your first program with go

常函数const的学习

Nacos 介绍和部署

C语言——字符串函数,内存函数集锦以及模拟实现

can 串口 can 232 can 485 串口转CANbus总线网关模块CAN232/485MB转换器CANCOM
随机推荐
面试之请详细说下synchronized的实现原理以及相关的锁
Niuke-top101-bm32
C language -- string function, memory function collection and Simulation Implementation
Correspondence between IEC61131 data type and C data type
C language lseek() function: move the read and write location of the file
Working principle and application of fast recovery diode
I talked with the interviewer about MySQL optimization in five dimensions
Axi protocol (4): signals on the Axi channel
批量将PPM格式图片转化为JPG格式
Alibaba cloud industrial vision intelligent engineer ACP certification - Preparation
Customer service relationship management based on SQL net enterprise messenger enterprise communications
异步时父子线程间的ThreadLocal传递问题
STM32 development | ad7606 parallel multi-channel data acquisition
Soft exam review and plan
Icml2022 | imitation learning by evaluating the professional knowledge of the presenter
Database startup message: ora-29702: error occurred in cluster group service
Axi protocol (5): burst mechanism of Axi protocol
[enterprise micro procedure]
Nacos 介绍和部署
columns in GROUP BY clause; this is incompatible with sql_ mode=only_ full_ group_ By mysql8.0 solution