洛谷P5057[CQOI2006]简单题

差分

树状数组基本操作不说了,主要想记录一下异或下的差分

a数组为每一位的真实值(假设\(a[0]=0\)),t为差分后的数组

则\(t[i]=a[i]\)^\(a[i-1]\)

所以我们如果想要查询\(a[i]\)

则\(a[i]=a[0]\)$a[1]$\(a[1]\)$a[2]$\(a[2]\)$\dots$\(a[i]\)$a[i]$=$t[1]$\(t[2]\)$\dots$\(t[i]\)

所以在树状数组中记录t数组,修改时指修改(异或上1)\(l\)和\(r+1\)的值就行

查询就是异或和

当然这个题的数据范围也可以分块做

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#define R register
#define EN std::puts("")
#define LL long long
inline int read(){
int x=0,y=1;
char c=std::getchar();
while(c<'0'||c>'9'){if(c=='-') y=0;c=std::getchar();}
while(c>='0'&&c<='9'){x=x*10+(c^48);c=std::getchar();}
return y?x:-x;
}
int n;
inline int lowbit(int x){return x&(-x);}
int tree[100006];
inline int fq(int pos){
R int ans=0;
for(;pos;pos-=lowbit(pos)) ans^=tree[pos];
return ans;
}
inline void add(int pos){
for(;pos<=n;pos+=lowbit(pos)) tree[pos]^=1;
}
int main(){
n=read();int m=read();
while(m--){
int op=read();
if(op==1){
int l=read(),r=read();
add(l);add(r+1);
}
else std::printf("%d\n",fq(read()));
}
return 0;
}

最新文章

  1. Angular企业级开发(5)-项目框架搭建
  2. asp.net记录错误日志的方法
  3. sharepoint 计算列 年龄
  4. [转]oracle 实施方法论学习心得
  5. 02.JavaScript 面向对象精要--函数
  6. c++ 调用模板函数时加template什么意思?
  7. LeetCode Maximum Size Subarray Sum Equals k
  8. JavaScript的for循环编写九九乘法表
  9. VmWare Workstation 10 安装 Ubuntu 14.04 问题解决
  10. sql 通过游标 拆分xml结构
  11. 奶牛通讯 usaco 网络流
  12. python列表sort方法的两个参数key, reverse
  13. NodeJS学习目录
  14. Android数据绑定技二,企业级开发
  15. Spider爬虫 の 事
  16. 使用Js进行linq处理
  17. Command &quot;python setup.py egg_info&quot; failed with error code 1 in c:\users\w5659\appdata\local\temp\pip-build-fs2yzl\ipython\
  18. netty的HelloWorld演示
  19. [原]在使用ubuntu14.04,安装devstack的时候报错./stack.sh: line 463: generate-subunit: command not found
  20. Linux基本的命令使用2018-4-20 18:47:28

热门文章

  1. python 函数--递归函数
  2. flask 入门之 logging
  3. django-&gt;基本操作和新建项目常用配置
  4. json === dict
  5. 区间dp入门+例题
  6. 大数据及hadoop简要概念
  7. canvas 实现光线沿不规则路径运动
  8. mysql的事务四个特性以及事务的四个隔离级别
  9. CentOS 配置OOM监控报警
  10. L1-L11 jupter notebook 文件