当前位置:网站首页>[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).
边栏推荐
- New knowledge in big homework
- NFT的几种发行方式你都了解过吗?不同的发行方式有什么优缺点?
- Unnamed Article 33
- 域名解析过程全分析,就着文字理解更佳
- Textfield and password input box that are more flexible and easy to use in compose
- Calculate the curvature of discrete points (matlab)
- 2022 Hangdian multi school DOS card (line segment tree)
- 数据库启动报:ORA-29702: error occurred in Cluster Group Service
- C language -- string function, memory function collection and Simulation Implementation
- IEC61131 数据类型与 C#数据类型的对应
猜你喜欢

公交站间的距离 : 简单模拟题

mysql函数汇总之日期和时间函数

C language - pointer one touch ※

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

分布式ID的常用解决方案-一把拿下

Molecular skeleton transition tool -delinker introduction

MySQL八股知识点:从入门到删库

Calculate the curvature of discrete points (matlab)
![[cloud native | 17] four network modes of container](/img/06/01204436c27b69a5ae6fcb04642c2a.jpg)
[cloud native | 17] four network modes of container

What is the real HTAP? (1) Background article
随机推荐
[enterprise micro procedure]
[weekly translation go] how to write your first program with go
2022 Henan Mengxin League game (3): Henan University B - reverse pair count
Seata两阶段提交AT详解
五个维度着手MySQL的优化,我和面试官都聊嗨了
5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
What is the difference between asynchronous and synchronous transmission signals (electronic hardware)
Recursive implementation of exponential enumeration
嵌入式分享合集21
C语言——字符串函数,内存函数集锦以及模拟实现
Ansible tutorial
Wsl2 best practices, eliminate difficult xshell and finalshell
IEC61131 数据类型与 C#数据类型的对应
软考回顾及计划
阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
地球系统模式(CESM)实践技术
Customer service relationship management based on SQL net enterprise messenger enterprise communications
[mathematical modeling] analytic hierarchy process (AHP)
科技论文翻译,俄语文档的语法有何特点
你对“happen-before原则”的理解可能是错的?